diff --git a/.ddev/commands/web/phpunit b/.ddev/commands/web/phpunit new file mode 100755 index 00000000000..40352e7b153 --- /dev/null +++ b/.ddev/commands/web/phpunit @@ -0,0 +1,7 @@ +#!/bin/bash + +## Description: run PHPUnit +## Usage: phpunit +## Example: ddev phpunit + +php vendor/bin/phpunit --no-coverage "$@" --testdox diff --git a/.ddev/commands/web/phpunit-coverage b/.ddev/commands/web/phpunit-coverage new file mode 100755 index 00000000000..c0971c51e6c --- /dev/null +++ b/.ddev/commands/web/phpunit-coverage @@ -0,0 +1,9 @@ +#!/bin/bash + +## Description: run PHPUnit with coverage +## Usage: phpunit-coverage +## Example: ddev phpunit-coverage + +enable_xdebug +XDEBUG_MODE=coverage php vendor/bin/phpunit --testdox +disable_xdebug \ No newline at end of file diff --git a/.ddev/commands/web/phpunit-coverage-local b/.ddev/commands/web/phpunit-coverage-local new file mode 100755 index 00000000000..ce6c0987a5c --- /dev/null +++ b/.ddev/commands/web/phpunit-coverage-local @@ -0,0 +1,9 @@ +#!/bin/bash + +## Description: run PHPUnit with local HTML coverage +## Usage: phpunit-coverage-local +## Example: ddev phpunit-coverage-local + +enable_xdebug +XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html build/coverage --testdox +disable_xdebug \ No newline at end of file diff --git a/.gitattributes b/.gitattributes index a54ca6bcb41..33a3b1e2c65 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14,9 +14,8 @@ /.phpcs.php.xml.dist export-ignore /.phpcs.xml.dist export-ignore /.phpmd.dist.xml export-ignore -/phpstan.dist.baseline.neon export-ignore -/phpstan.dist.issues.neon export-ignore -/phpstan.dist.neon export-ignore +/.phpstan.dist.baseline.neon export-ignore +/.phpstan.dist.neon export-ignore /README.md export-ignore diff --git a/.github/workflows/check-files.yml b/.github/workflows/check-files.yml index b1470d0ea4e..a83d663a780 100644 --- a/.github/workflows/check-files.yml +++ b/.github/workflows/check-files.yml @@ -33,9 +33,6 @@ on: phpunit: description: "Count changed PhpUnit files" value: ${{ jobs.check.outputs.phpunit }} - sonar: - description: "Count changed Sonar files" - value: ${{ jobs.check.outputs.sonar }} # Allow manually triggering the workflow. workflow_dispatch: @@ -54,7 +51,6 @@ jobs: phpstan: ${{ steps.changes-phpstan.outputs.phpstan }} phpunit-test: ${{ steps.changes-phpunit-test.outputs.phpunit-test }} phpunit: ${{ steps.changes-phpunit.outputs.phpunit }} - sonar: ${{ steps.changes-sonar.outputs.sonar }} steps: - name: Checkout code @@ -87,9 +83,8 @@ jobs: **phpcs** **php-cs-fixer** **phpstan** - dev/tests/ - dev/phpunit* - dev/sonar* + tests/ + phpunit* - name: Check if composer files changed id: changes-composer @@ -161,7 +156,7 @@ jobs: id: changes-phpunit-test if: steps.changed-files-specific.outputs.any_modified == 'true' run: | - count="$(grep -oE "dev/tests/" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)" + count="$(grep -oE "tests/" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)" echo "$count UnitTest test file(s) changed" echo "phpunit-test=$count" >> $GITHUB_OUTPUT @@ -169,14 +164,6 @@ jobs: id: changes-phpunit if: steps.changed-files-specific.outputs.any_modified == 'true' run: | - count="$(grep -oE "dev/phpunit*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)" + count="$(grep -oE "phpunit*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)" echo "$count PHPUnit file(s) changed" echo "phpunit=$count" >> $GITHUB_OUTPUT - - - name: Check if Sonar files changed - id: changes-sonar - if: steps.changed-files-specific.outputs.any_modified == 'true' - run: | - count="$(grep -oE "dev/sonar*" <<< "${{ steps.changed-files-specific.outputs.all_modified_files }}" | wc -l)" - echo "$count Sonar file(s) changed" - echo "sonar=$count" >> $GITHUB_OUTPUT diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index b4db7974e5c..d9142354e3b 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -10,16 +10,39 @@ on: jobs: unit-tests: - runs-on: [ubuntu-latest] + runs-on: ${{ matrix.operating-system }} + strategy: + matrix: + operating-system: [ubuntu-latest] + php-versions: ['7.4', '8.3'] + mysql-version: ['5.7', '8.0'] + + services: + mysql: + image: mysql:${{ matrix.mysql-version }} + env: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: db + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 steps: - - uses: actions/checkout@v4 + - name: Validate mysql service + run: | + echo "Checking mysql service" + sudo apt-get install -y mysql-client + mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uroot -proot -e "SHOW DATABASES" + + - name: Setup PHP + uses: shivammathur/setup-php@v2 with: - repository: OpenMage/Testfield - path: ./ + php-version: ${{ matrix.php-versions }} + coverage: pcov #optional, setup coverage driver + env: + COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Validate composer - run: composer validate + - uses: actions/checkout@v4 - name: Get composer cache directory id: composer-cache @@ -33,24 +56,61 @@ jobs: restore-keys: ${{ runner.os }}-composer- - name: Install dependencies - run: composer install --prefer-dist --no-progress --ignore-platform-reqs - - - name: Checkout OpenMage repo - uses: actions/checkout@v4 - with: - path: openmage + run: composer install --prefer-dist --no-progress --ignore-platform-req=ext-* - - name: Install OpenMage dependencies - working-directory: ./openmage - run: composer install --prefer-dist --no-progress --ignore-platform-reqs --no-dev + - name: Install OpenMage + run: | + php -f install.php -- \ + --license_agreement_accepted 'yes' \ + --locale 'en_US' \ + --timezone 'America/New_York' \ + --db_host '127.0.0.1' \ + --db_name 'db' \ + --db_user 'root' \ + --db_pass 'root' \ + --db_prefix '' \ + --url 'http://openmage.local' \ + --use_rewrites 'yes' \ + --use_secure 'yes' \ + --secure_base_url 'http://openmage.local' \ + --use_secure_admin 'yes' \ + --admin_username 'admin' \ + --admin_lastname 'Administrator' \ + --admin_firstname 'OpenMage' \ + --admin_email 'admin@example.com' \ + --admin_password 'veryl0ngpassw0rd' \ + --session_save 'files' \ + --admin_frontname 'admin' \ + --backend_frontname 'admin' \ + --default_currency 'USD' \ + --enable_charts 'yes' \ + --skip_url_validation 'yes' - - name: run phpUnit - run: bash ./run_unit_tests.sh + - name: Run phpUnit + run: php -f vendor/bin/phpunit - name: Publish Unit Test Results - uses: EnricoMi/publish-unit-test-result-action@v2.7 + uses: EnricoMi/publish-unit-test-result-action@v2 if: always() - continue-on-error: true with: github_token: ${{ secrets.GITHUB_TOKEN }} - files: output/*.xml + files: tests/logging/*.xml + + - name: prepare SonarCloud Scan Data + if: ${{ (matrix.php-versions == '7.4') && (matrix.mysql-version == '5.7') }} + run: | + head tests/coverage/clover.xml + sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' tests/logging/junit.xml + sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' tests/coverage/clover.xml + head ./tests/coverage/clover.xml + + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + continue-on-error: true + if: ${{ (matrix.php-versions == '7.4') && (matrix.mysql-version == '5.7') }} && SONAR_TOKEN + with: + args: > + -Dproject.settings=tests/sonar-project.properties + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml deleted file mode 100644 index cc2ffcb5d43..00000000000 --- a/.github/workflows/sonar.yml +++ /dev/null @@ -1,71 +0,0 @@ -name: Sonar - -on: - # Run automatically every Monday on midnight. - schedule: - - cron: '0 0 * * 1' - workflow_call: - # Allow manually triggering the workflow. - workflow_dispatch: - -jobs: - unit: - name: Unit Tests on ${{ matrix.php }} - runs-on: ${{ matrix.os }} - strategy: - max-parallel: 5 - matrix: - os: [ubuntu-latest] - php: ['7.4', '8.1'] - steps: - - uses: actions/checkout@v4 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - extensions: mbstring - tools: composer, pecl, phpcs, phpstan, phpunit:9.5 - ini-values: pcov.directory=api, post_max_size=256M, short_open_tag=On #optional, setup php.ini configuration - coverage: pcov #optional, setup coverage driver - env: - COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Get composer cache directory - id: composer-cache - run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - - - name: Cache dependencies - uses: actions/cache@v4 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} - restore-keys: ${{ runner.os }}-composer- - - - name: Install dependencies - run: composer install --dev -n --prefer-source --ignore-platform-req=php+ - - - name: Run Unit Tests - run: phpunit --configuration ./dev/phpunit.xml.dist --testsuite=Unit; - - - name: prepare SonarCloud Scan Data - continue-on-error: true - if: ${{ matrix.php == '8.1' }} - run: | - echo $PWD - ls -la - head ./dev/tests/clover.xml - sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' ./dev/tests/junit.xml - sed -i 's@'$GITHUB_WORKSPACE'/@/github/workspace/@g' ./dev/tests/clover.xml - head ./dev/tests/clover.xml - ls -la - - - name: SonarCloud Scan - uses: SonarSource/sonarcloud-github-action@master - continue-on-error: true - if: ${{ matrix.php == '8.1' }} && SONAR_TOKEN - with: - args: > - -Dproject.settings=dev/sonar-project.properties - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 68400099a6c..669207a35ec 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -93,10 +93,10 @@ jobs: uses: ./.github/workflows/phpstan.yml # DOES NOT run by default - # runs on schedule or when worklfow changed + # runs on schedule or when workflow changed syntax_php: name: PHP Syntax - needs: [check, phpcs, php-cs-fixer] + needs: [check, php-cs-fixer] if: needs.check.outputs.workflow > 0 uses: ./.github/workflows/syntax-php.yml @@ -107,23 +107,12 @@ jobs: uses: ./.github/workflows/syntax-xml.yml # DOES NOT run by default - # runs on schedule or when worklfow or unit tests changed - sonar: - name: Unit Tests (Sonar) - needs: [check, phpcs, php-cs-fixer] - if: | - needs.check.outputs.phpunit-test > 0 || - needs.check.outputs.phpunit > 0 || - needs.check.outputs.sonar > 0 || - needs.check.outputs.workflow > 0 - uses: ./.github/workflows/sonar.yml - - # DOES NOT run by default - # runs on schedule or when worklfow or unit tests changed + # runs on schedule or when workflow or unit tests changed unit_tests: name: Unit Tests (OpenMage) - needs: [check, sonar] + needs: [check, php-cs-fixer] if: | + needs.check.outputs.php > 0 || needs.check.outputs.phpunit-test > 0 || needs.check.outputs.phpunit > 0 || needs.check.outputs.workflow > 0 diff --git a/.gitignore b/.gitignore index e5aaad872a9..cef12b3e51c 100644 --- a/.gitignore +++ b/.gitignore @@ -23,13 +23,6 @@ /app/etc/modules/Cm_RedisSession.xml /lib/Credis -# Add a base setup for running unit Tests with code coverage and send them to SonarCloud -# https://github.com/OpenMage/magento-lts/pull/1836 -/dev/testfield -/dev/tests/clover.xml -/dev/tests/crap4j.xml -/dev/tests/junit.xml - # Add Gitpod online IDE config # https://github.com/OpenMage/magento-lts/pull/1836 /dev/gitpod/docker-magento @@ -76,6 +69,16 @@ phpstan*.neon !.phpstan.dist.neon !.phpstan.dist.*.neon +# PhpUnit +tests/coverage +tests/logging +.phpunit.result.cache +phpunit.xml +!phpunit.xml.dist + +# build +/build + # dev scripts loaded via composer /shell/update-copyright.php /shell/translations.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index c5dce8dc29c..8924d1fc496 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -114,6 +114,7 @@ 'lib/Magento/', 'lib/Varien/', 'shell/', + 'tests/unit/', ]) ->name('*.php') ->ignoreDotFiles(true) diff --git a/.phpcs.php.xml.dist b/.phpcs.php.xml.dist index aefb549779a..fe91de479b7 100644 --- a/.phpcs.php.xml.dist +++ b/.phpcs.php.xml.dist @@ -12,6 +12,7 @@ lib/Magento/ lib/Varien/ shell/ + tests/unit/ */Varien/Object.php* diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index cfa926c52f2..19a8ceb29dd 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -12,6 +12,7 @@ lib/Magento/ lib/Varien/ shell/ + tests/unit/ diff --git a/.phpstan.dist.neon b/.phpstan.dist.neon index 3a65bcd553a..77ead18bbde 100644 --- a/.phpstan.dist.neon +++ b/.phpstan.dist.neon @@ -17,6 +17,7 @@ parameters: - lib/Magento - lib/Varien - shell + - tests/unit excludePaths: #incompatible interfaces - app/code/core/Mage/Admin/Model/Acl/Assert/Ip.php diff --git a/composer.json b/composer.json index 078f224d5cf..3fc9d0344f9 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "symfony/polyfill-php82": "^1.29" }, "require-dev": { + "ext-xmlreader": "*", "dealerdirect/phpcodesniffer-composer-installer": "^1.0.0", "friendsofphp/php-cs-fixer": "^3.4", "macopedia/phpstan-magento1": "^1.0.5", @@ -88,7 +89,7 @@ ], "autoload-dev": { "psr-4": { - "OpenMage\\Tests\\Unit\\": "dev/tests/unit" + "OpenMage\\Tests\\Unit\\": "tests/unit" } }, "extra": { @@ -126,5 +127,10 @@ "php": "7.4" }, "sort-packages": true + }, + "scripts": { + "phpunit:test": "vendor/bin/phpunit --no-coverage \"$@\" --testdox", + "phpunit:coverage": "XDEBUG_MODE=coverage php vendor/bin/phpunit --testdox", + "phpunit:coverage-local": "XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html build/coverage --testdox" } } diff --git a/composer.lock b/composer.lock index 423ac6a51c9..bc3e26c386d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e1e458ca3048c0e7e7ac5e2b6041c47f", + "content-hash": "9a0f70859a6272156d00c0b32fc48cff", "packages": [ { "name": "colinmollenhour/cache-backend-redis", @@ -6408,7 +6408,9 @@ "ext-soap": "*", "ext-zlib": "*" }, - "platform-dev": [], + "platform-dev": { + "ext-xmlreader": "*" + }, "platform-overrides": { "php": "7.4" }, diff --git a/dev/phpunit.xml.dist b/dev/phpunit.xml.dist deleted file mode 100644 index b10c60d7938..00000000000 --- a/dev/phpunit.xml.dist +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - ./testfield/tests/Unit - - - ./testfield/tests/MagentoHackathon/Composer/FullStack - - - ./tests/unit - - - - - - ../app/code - ../lib - - - ../app/bootstrap.php - ../app/code/core/Mage/Admin/Model/Acl/Assert/Ip.php - ../app/code/core/Mage/Admin/Model/Acl/Assert/Time.php - - - - - - - - - - diff --git a/dev/tests/bootstrap.php b/dev/tests/bootstrap.php deleted file mode 100644 index 83a24baba35..00000000000 --- a/dev/tests/bootstrap.php +++ /dev/null @@ -1,6 +0,0 @@ -assertTrue(class_exists('Mage')); - $this->assertTrue(class_exists('Mage_Eav_Model_Entity_Increment_Numeric')); - } - - public function testClassDoesNotExists() - { - $this->assertFalse(class_exists('Mage_Non_Existent')); - } -} diff --git a/dev/tests/unit/Base/XmlFileLoadingTest.php b/dev/tests/unit/Base/XmlFileLoadingTest.php deleted file mode 100644 index 0f8b6b50fd4..00000000000 --- a/dev/tests/unit/Base/XmlFileLoadingTest.php +++ /dev/null @@ -1,52 +0,0 @@ -assertNotEmpty($simplexml->asXML()); - } - - /** - * - * @dataProvider provideXmlFiles - * @param $filepath - * @return void - */ - public function testXmlReaderIsValid($filepath): void - { - $xml = \XMLReader::open($filepath); - $xml->setParserProperty(\XMLReader::VALIDATE, true); - $this->assertTrue($xml->isValid()); - } -} diff --git a/dev/tests/unit/Mage/Core/Helper/Security.php b/dev/tests/unit/Mage/Core/Helper/Security.php deleted file mode 100644 index f0b11f593e6..00000000000 --- a/dev/tests/unit/Mage/Core/Helper/Security.php +++ /dev/null @@ -1,97 +0,0 @@ -validateAgainstBlockMethodBlacklist($block, $method, $args); - } - - - public function forbiddenBlockMethodsDataProvider() - { - $topmenu = new \Mage_Page_Block_Html_Topmenu_Renderer(); - $template = new \Mage_Core_Block_Template(); - - return [ - [ - $template, - 'fetchView', - [] - ], - [ - $topmenu, - 'fetchView', - [] - ], - [ - $topmenu, - 'render', - [] - ], - [ - $template, - 'Mage_Core_Block_Template::fetchView', - [] - ], - [ - $topmenu, - 'Mage_Page_Block_Html_Topmenu_Renderer::fetchView', - [] - ], - 'parent class name is passed as second arg' => [ - $topmenu, - 'Mage_Core_Block_Template::fetchView', - [] - ], - 'parent class name is passed as second arg2' => [ - $topmenu, - 'Mage_Core_Block_Template::render', - [] - ], - ]; - } - - /** - * @dataProvider forbiddenBlockMethodsDataProvider - * @return void - */ - public function testValidateAgainstBlockMethodBlacklistThrowsException($block, $method, $args) - { - $this->expectExceptionMessage(\sprintf('Action with combination block %s and method %s is forbidden.', get_class($block), $method)); - - $securityHelper = new \Mage_Core_Helper_Security(); - $securityHelper->validateAgainstBlockMethodBlacklist($block, $method, $args); - } -} \ No newline at end of file diff --git a/dev/tests/unit/Mage/Core/Helper/StringTest.php b/dev/tests/unit/Mage/Core/Helper/StringTest.php deleted file mode 100644 index 72148df7079..00000000000 --- a/dev/tests/unit/Mage/Core/Helper/StringTest.php +++ /dev/null @@ -1,50 +0,0 @@ -substr( - self::TEST_STRING_1, - 5, - 5 - ); - $this->assertEquals( - '12345', - $resultString - ); - } - - public function testTruncate() - { - $subject = new Mage_Core_Helper_String(); - $resultString = $subject->truncate( - self::TEST_STRING_1, - 13, - '###' - ); - $this->assertEquals( - 'Test 12345###', - $resultString - ); - - } - - public function testStrlen() - { - $subject = new Mage_Core_Helper_String(); - $this->assertEquals( - 26, - $subject->strlen(self::TEST_STRING_1) - ); - } -} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 00000000000..306b6bcc934 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,54 @@ + + + + + + tests/unit/Base + + + tests/unit/Mage + + + tests/unit/Varien + + + + + + app/code + lib + + + app/bootstrap.php + app/code/core/Mage/Admin/Model/Acl/Assert/Ip.php + app/code/core/Mage/Admin/Model/Acl/Assert/Time.php + app/code/community + app/code/local/ + app/code/core/Mage/*/data + app/code/core/Mage/*/sql + vendor + + + + + + + + + + diff --git a/tests/.htaccess b/tests/.htaccess new file mode 100644 index 00000000000..93169e4eb44 --- /dev/null +++ b/tests/.htaccess @@ -0,0 +1,2 @@ +Order deny,allow +Deny from all diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000000..2d096b78be5 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,6 @@ +assertEquals($expectedResult, class_exists($class)); + } + + /** + * @return array> + */ + public function provideClassExistsData(): array + { + return [ + 'class exists #1' => [ + true, + 'Mage' + ], + 'class exists #2' => [ + true, + 'Mage_Eav_Model_Entity_Increment_Numeric' + ], + 'class not exists' => [ + false, + 'Mage_Non_Existent' + ], + ]; + } +} diff --git a/tests/unit/Base/XmlFileLoadingTest.php b/tests/unit/Base/XmlFileLoadingTest.php new file mode 100644 index 00000000000..8cca409ddc2 --- /dev/null +++ b/tests/unit/Base/XmlFileLoadingTest.php @@ -0,0 +1,71 @@ +assertNotEmpty($simplexml->asXML()); + } + + /** + * + * @dataProvider provideXmlFiles + * @param string $filepath + * @return void + */ + public function testXmlReaderIsValid(string $filepath): void + { + /** @var XMLReader $xml */ + $xml = XMLReader::open($filepath); + $xml->setParserProperty(XMLReader::VALIDATE, true); + $this->assertTrue($xml->isValid()); + } + + /** + * @return string[][] + */ + public function provideXmlFiles(): array + { + // phpcs:ignore Ecg.Security.ForbiddenFunction.Found + $root = realpath(__DIR__ . '/../../../') . '/'; + + return [ + 'file from vendor directory' => [ + $root . 'vendor/shardj/zf1-future/library/Zend/Locale/Data/es_419.xml' + ], + ]; + } +} diff --git a/tests/unit/Mage/Admin/Helper/BlockTest.php b/tests/unit/Mage/Admin/Helper/BlockTest.php new file mode 100644 index 00000000000..84e2acef6b7 --- /dev/null +++ b/tests/unit/Mage/Admin/Helper/BlockTest.php @@ -0,0 +1,54 @@ +subject = Mage::helper('admin/block'); + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Helper + */ + public function testIsTypeAllowed(): void + { + $this->assertIsBool($this->subject->isTypeAllowed('some-type')); + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Helper + */ + public function testGetDisallowedBlockNames(): void + { + $this->assertIsArray($this->subject->getDisallowedBlockNames()); + } +} diff --git a/tests/unit/Mage/Admin/Helper/DataTest.php b/tests/unit/Mage/Admin/Helper/DataTest.php new file mode 100644 index 00000000000..62941ebe628 --- /dev/null +++ b/tests/unit/Mage/Admin/Helper/DataTest.php @@ -0,0 +1,54 @@ +subject = Mage::helper('admin/data'); + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Helper + */ + public function testGenerateResetPasswordLinkToken(): void + { + $this->assertIsString($this->subject->generateResetPasswordLinkToken()); + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Helper + */ + public function testGetResetPasswordLinkExpirationPeriod(): void + { + $this->assertIsInt($this->subject->getResetPasswordLinkExpirationPeriod()); + } +} diff --git a/tests/unit/Mage/Admin/Model/UserTest.php b/tests/unit/Mage/Admin/Model/UserTest.php new file mode 100644 index 00000000000..fb891d31869 --- /dev/null +++ b/tests/unit/Mage/Admin/Model/UserTest.php @@ -0,0 +1,151 @@ +subject = Mage::getModel('admin/user'); + } + + /** + * @dataProvider provideValidateData + * @param array|true $expectedResult + * @param array $methods + * @return void + * + * @group Mage_Admin + * @group Mage_Admin_Model + */ + public function testValidate($expectedResult, array $methods): void + { + $mock = $this->getMockBuilder(Mage_Admin_Model_User::class) + ->setMethods([ + 'hasNewPassword', + 'getNewPassword', + 'hasPassword', + 'getPassword', + ]) + ->getMock(); + + $mock->expects($this->any())->method('hasNewPassword')->willReturn($methods['hasNewPassword']); + $mock->expects($this->any())->method('getNewPassword')->willReturn($methods['getNewPassword']); + $mock->expects($this->any())->method('hasPassword')->willReturn($methods['hasPassword']); + $mock->expects($this->any())->method('getPassword')->willReturn($methods['getPassword']); + // phpcs:ignore Ecg.Security.ForbiddenFunction.Found + $this->assertEquals($expectedResult, $mock->validate()); + } + + /** + * @return array> + */ + public function provideValidateData(): array + { + return [ + 'test_fails_1' => [ + [ + 0 => 'User Name is required field.', + 1 => 'First Name is required field.', + 2 => 'Last Name is required field.', + 3 => 'Please enter a valid email.', + 4 => 'Password must be at least of 14 characters.', + 5 => 'Password must include both numeric and alphabetic characters.', + ], + [ + 'hasNewPassword' => true, + 'getNewPassword' => '123', + 'hasPassword' => false, + 'getPassword' => '456', + ] + ], + 'test_fails_2' => [ + [ + 0 => 'User Name is required field.', + 1 => 'First Name is required field.', + 2 => 'Last Name is required field.', + 3 => 'Please enter a valid email.', + 4 => 'Password must be at least of 14 characters.', + 5 => 'Password must include both numeric and alphabetic characters.', + ], + [ + 'hasNewPassword' => false, + 'getNewPassword' => '123', + 'hasPassword' => true, + 'getPassword' => '456', + ] + ], + ]; + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Model + */ + public function testValidateCurrentPassword(): void + { + $this->assertIsArray($this->subject->validateCurrentPassword('')); + $this->assertIsArray($this->subject->validateCurrentPassword('123')); + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Model + */ + public function testLoadByUsername(): void + { + $this->assertInstanceOf(Mage_Admin_Model_User::class, $this->subject->loadByUsername('invalid-user')); + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Model + */ + public function testChangeResetPasswordLinkToken(): void + { + $this->assertInstanceOf(Mage_Admin_Model_User::class, $this->subject->changeResetPasswordLinkToken('123')); + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Model + */ + public function testIsResetPasswordLinkTokenExpired(): void + { + $this->assertIsBool($this->subject->isResetPasswordLinkTokenExpired()); + } + + /** + * @group Mage_Admin + * @group Mage_Admin_Model + */ + public function testSendPasswordResetConfirmationEmail(): void + { + $this->assertInstanceOf(Mage_Admin_Model_User::class, $this->subject->sendPasswordResetConfirmationEmail()); + } +} diff --git a/tests/unit/Mage/Admin/Model/VariableTest.php b/tests/unit/Mage/Admin/Model/VariableTest.php new file mode 100644 index 00000000000..a9e30854137 --- /dev/null +++ b/tests/unit/Mage/Admin/Model/VariableTest.php @@ -0,0 +1,90 @@ +subject = Mage::getModel('admin/variable'); + } + + /** + * @dataProvider provideValidateData + * @param array|true $expectedResult + * @param string $variableName + * @param string $isAllowed + * @return void + * + * @group Mage_Admin + */ + public function testValidate($expectedResult, string $variableName, string $isAllowed): void + { + $mock = $this->getMockBuilder(Mage_Admin_Model_Variable::class) + ->setMethods(['getVariableName', 'getIsAllowed']) + ->getMock(); + + $mock->expects($this->any())->method('getVariableName')->willReturn($variableName); + $mock->expects($this->any())->method('getIsAllowed')->willReturn($isAllowed); + $this->assertEquals($expectedResult, $mock->validate()); + } + + /** + * @return array> + */ + public function provideValidateData(): array + { + return [ + 'test_passes' => [ + true, + 'test', + '1' + ], + 'test_error_empty' => [ + [0 => 'Variable Name is required field.'], + '', + '1' + ], + 'test_error_regex' => [ + [0 => 'Variable Name is incorrect.'], + '#invalid-name#', + '1' + ], + 'test_error_allowed' => [ + [0 => 'Is Allowed is required field.'], + 'test', + 'invalid' + ], + ]; + } + + public function testIsPathAllowed(): void + { + $this->assertIsBool($this->subject->isPathAllowed('invalid-path')); + } +} diff --git a/tests/unit/Mage/Adminhtml/Block/Cms/PageTest.php.tmp b/tests/unit/Mage/Adminhtml/Block/Cms/PageTest.php.tmp new file mode 100644 index 00000000000..1922944509f --- /dev/null +++ b/tests/unit/Mage/Adminhtml/Block/Cms/PageTest.php.tmp @@ -0,0 +1,48 @@ +subject = new Mage_Adminhtml_Block_Cms_Page(); + } + + /** + * @return void + * + * @group Mage_Adminhtml + * @group Mage_Adminhtml_Block + */ + public function testGetHeaderCssClass(): void + { + #$this->assertSame(Mage_Adminhtml_Block_Cms_Page::class, $this->subject->getHeaderCssClass()); + } +} diff --git a/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/AccountTest.php b/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/AccountTest.php new file mode 100644 index 00000000000..f2e6d98b7da --- /dev/null +++ b/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/AccountTest.php @@ -0,0 +1,60 @@ +subject = new Mage_Adminhtml_Block_Customer_Edit_Tab_Account(); + } + + /** + * @return void + * @throws Mage_Core_Exception + * + * @group Mage_Adminhtml + * @group Mage_Adminhtml_Block + */ +// public function testInitForm(): void +// { +// $mock = $this->getMockBuilder(Mage_Adminhtml_Block_Customer_Edit_Tab_Account::class) +// ->setMethods(['getRegistryCurrentCustomer']) +// ->getMock(); +// +// $mock->expects($this->any()) +// ->method('getRegistryCurrentCustomer') +// // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation +// ->willReturn(new Mage_Customer_Model_Customer); +// +// $this->assertInstanceOf(Mage_Adminhtml_Block_Customer_Edit_Tab_Account::class, $mock->initForm()); +// } +} diff --git a/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/AddressesTest.php b/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/AddressesTest.php new file mode 100644 index 00000000000..b3d49beae6e --- /dev/null +++ b/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/AddressesTest.php @@ -0,0 +1,57 @@ +subject = new Mage_Adminhtml_Block_Customer_Edit_Tab_Addresses(); + } + + /** + * @return void + * + * @group Mage_Adminhtml + */ +// public function testInitForm(): void +// { +// $mock = $this->getMockBuilder(Mage_Adminhtml_Block_Customer_Edit_Tab_Addresses::class) +// ->setMethods(['getRegistryCurrentCustomer']) +// ->getMock(); +// +// $mock->expects($this->any()) +// ->method('getRegistryCurrentCustomer') +// // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation +// ->willReturn(new Mage_Customer_Model_Customer()); +// +// $this->assertInstanceOf(Mage_Adminhtml_Block_Customer_Edit_Tab_Addresses::class, $mock->initForm()); +// } +} diff --git a/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/NewsletterTest.php b/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/NewsletterTest.php new file mode 100644 index 00000000000..39c500f99c4 --- /dev/null +++ b/tests/unit/Mage/Adminhtml/Block/Customer/Edit/Tab/NewsletterTest.php @@ -0,0 +1,57 @@ +subject = new Mage_Adminhtml_Block_Customer_Edit_Tab_Newsletter(); + } + + /** + * @return void + * + * @group Mage_Adminhtml + */ +// public function testInitForm(): void +// { +// $mock = $this->getMockBuilder(Mage_Adminhtml_Block_Customer_Edit_Tab_Newsletter::class) +// ->setMethods(['getRegistryCurrentCustomer']) +// ->getMock(); +// +// $mock->expects($this->any()) +// ->method('getRegistryCurrentCustomer') +// // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation +// ->willReturn(new Mage_Customer_Model_Customer()); +// +// $this->assertInstanceOf(Mage_Adminhtml_Block_Customer_Edit_Tab_Newsletter::class, $mock->initForm()); +// } +} diff --git a/tests/unit/Mage/Adminhtml/Block/System/Cache/Form/FormTest.php b/tests/unit/Mage/Adminhtml/Block/System/Cache/Form/FormTest.php new file mode 100644 index 00000000000..3f7fd007991 --- /dev/null +++ b/tests/unit/Mage/Adminhtml/Block/System/Cache/Form/FormTest.php @@ -0,0 +1,47 @@ +subject = new Mage_Adminhtml_Block_System_Cache_Form(); + } + + /** + * @return void + * + * @group Mage_Adminhtml + */ + public function testInitForm(): void + { + $this->assertInstanceOf(Mage_Adminhtml_Block_System_Cache_Form::class, $this->subject->initForm()); + } +} diff --git a/tests/unit/Mage/Adminhtml/Block/System/Config/Form/FormTest.php b/tests/unit/Mage/Adminhtml/Block/System/Config/Form/FormTest.php new file mode 100644 index 00000000000..dc6d49b52e5 --- /dev/null +++ b/tests/unit/Mage/Adminhtml/Block/System/Config/Form/FormTest.php @@ -0,0 +1,47 @@ +subject = new Mage_Adminhtml_Block_System_Config_Form(); + } + + /** + * @return void + * + * @group Mage_Adminhtml + */ + public function testInitForm(): void + { + $this->assertInstanceOf(Mage_Adminhtml_Block_System_Config_Form::class, $this->subject->initForm()); + } +} diff --git a/tests/unit/Mage/Adminhtml/Block/System/Convert/Gui/Edit/Tab/ViewTest.php b/tests/unit/Mage/Adminhtml/Block/System/Convert/Gui/Edit/Tab/ViewTest.php new file mode 100644 index 00000000000..2311d15cec8 --- /dev/null +++ b/tests/unit/Mage/Adminhtml/Block/System/Convert/Gui/Edit/Tab/ViewTest.php @@ -0,0 +1,47 @@ +subject = new Mage_Adminhtml_Block_System_Convert_Gui_Edit_Tab_View(); + } + + /** + * @return void + * + * @group Mage_Adminhtml + */ +// public function testInitForm(): void +// { +// $this->assertInstanceOf(Mage_Adminhtml_Block_System_Convert_Gui_Edit_Tab_View::class, $this->subject->initForm()); +// } +} diff --git a/tests/unit/Mage/Adminhtml/Block/System/Convert/Profile/Edit/Tab/EditTest.php b/tests/unit/Mage/Adminhtml/Block/System/Convert/Profile/Edit/Tab/EditTest.php new file mode 100644 index 00000000000..1d925f86dce --- /dev/null +++ b/tests/unit/Mage/Adminhtml/Block/System/Convert/Profile/Edit/Tab/EditTest.php @@ -0,0 +1,47 @@ +subject = new Mage_Adminhtml_Block_System_Convert_Profile_Edit_Tab_Edit(); + } + + /** + * @return void + * + * @group Mage_Adminhtml + */ +// public function testInitForm(): void +// { +// $this->assertInstanceOf(Mage_Adminhtml_Block_System_Convert_Profile_Edit_Tab_Edit::class, $this->subject->initForm()); +// } +} diff --git a/tests/unit/Mage/Cms/Helper/DataTest.php b/tests/unit/Mage/Cms/Helper/DataTest.php new file mode 100644 index 00000000000..7b5f09ab4b6 --- /dev/null +++ b/tests/unit/Mage/Cms/Helper/DataTest.php @@ -0,0 +1,75 @@ +subject = Mage::helper('cms/data'); + } + + /** + * @group Mage_Cms + * @group Mage_Cms_Helper + */ + public function testGetAllowedStreamWrappers(): void + { + $this->assertIsArray($this->subject->getAllowedStreamWrappers()); + } + + /** + * @group Mage_Cms + * @group Mage_Cms_Helper + */ + public function testGetBlockTemplateProcessor(): void + { + $this->assertInstanceOf(Varien_Filter_Template::class, $this->subject->getBlockTemplateProcessor()); + } + + /** + * @group Mage_Cms + * @group Mage_Cms_Helper + */ + public function testGetPageTemplateProcessor(): void + { + $this->assertInstanceOf(Varien_Filter_Template::class, $this->subject->getPageTemplateProcessor()); + } + + /** + * @group Mage_Cms + * @group Mage_Cms_Helper + */ + public function testIsSwfDisabled(): void + { + $this->assertTrue($this->subject->isSwfDisabled()); + } +} diff --git a/tests/unit/Mage/Core/Helper/CookieTest.php b/tests/unit/Mage/Core/Helper/CookieTest.php new file mode 100644 index 00000000000..c54173cce3b --- /dev/null +++ b/tests/unit/Mage/Core/Helper/CookieTest.php @@ -0,0 +1,72 @@ +subject = Mage::helper('core/cookie'); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testIsUserNotAllowSaveCookie(): void + { + $this->assertIsBool($this->subject->isUserNotAllowSaveCookie()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetAcceptedSaveCookiesWebsiteIds(): void + { + $this->assertIsString($this->subject->getAcceptedSaveCookiesWebsiteIds()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetCookieRestrictionLifetime(): void + { + $this->assertIsInt($this->subject->getCookieRestrictionLifetime()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetCookieRestrictionNoticeCmsBlockIdentifier(): void + { + $this->assertIsString($this->subject->getCookieRestrictionNoticeCmsBlockIdentifier()); + } +} diff --git a/tests/unit/Mage/Core/Helper/DataTest.php b/tests/unit/Mage/Core/Helper/DataTest.php new file mode 100644 index 00000000000..0f628c9451c --- /dev/null +++ b/tests/unit/Mage/Core/Helper/DataTest.php @@ -0,0 +1,140 @@ +subject = Mage::helper('core/data'); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetEncryptor(): void + { + $this->assertInstanceOf(Mage_Core_Model_Encryption::class, $this->subject->getEncryptor()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testEncrypt(): void + { + $this->assertIsString($this->subject->encrypt('test')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testDecrypt(): void + { + $this->assertIsString($this->subject->decrypt('test')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testValidateKey(): void + { + $this->assertInstanceOf(Varien_Crypt_Mcrypt::class, $this->subject->validateKey('test')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetRandomString(): void + { + $this->assertIsString($this->subject->getRandomString(5)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetHash(): void + { + $this->assertIsString($this->subject->getHash('test')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetHashPassword(): void + { + $this->assertIsString($this->subject->getHashPassword('test', 1)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testValidateHash(): void + { + $this->assertIsBool($this->subject->validateHash('test', '1')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetStoreId(): void + { + $this->assertIsString($this->subject->getStoreId()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testRemoveAccents(): void + { + $str = 'Ae-Ä Oe-Ö Ue-Ü ae-ä oe-ö ue-ü'; + $this->assertEquals('Ae-Ae Oe-Oe Ue-Ue ae-ae oe-oe ue-ue', $this->subject->removeAccents($str, true)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testIsDevAllowed(): void + { + $this->assertIsBool($this->subject->isDevAllowed()); + } +} diff --git a/dev/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php similarity index 72% rename from dev/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php rename to tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php index ed7911d29ba..0df3a194aff 100644 --- a/dev/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php +++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTest.php @@ -1,69 +1,99 @@ buildPath($section, $group, $field); - } +namespace OpenMage\Tests\Unit\Mage\Core\Helper; - public function exposedBuildNodePath(string $scope, string $path): string - { - return $this->buildNodePath($scope, $path); - } -} +use Mage; +use Mage_Core_Exception; +use Mage_Core_Helper_EnvironmentConfigLoader; +use PHPUnit\Framework\TestCase; +use Varien_Simplexml_Config; class EnvironmentConfigLoaderTest extends TestCase { + public const XML_PATH_GENERAL = 'general/store_information/name'; + + public const XML_PATH_DEFAULT = 'default/general/store_information/name'; + + public const XML_PATH_WEBSITE = 'websites/base/general/store_information/name'; + + public const XML_PATH_STORE = 'stores/german/general/store_information/name'; + + /** + * @throws Mage_Core_Exception + */ public function setup(): void { - \Mage::setRoot(''); + Mage::setRoot(); } - public function testBuildPath() + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testBuildPath(): void { - $environmentConfigLoaderHelper = new TestEnvLoaderHelper(); + $environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper(); $path = $environmentConfigLoaderHelper->exposedBuildPath('GENERAL', 'STORE_INFORMATION', 'NAME'); - $this->assertEquals('general/store_information/name', $path); + $this->assertEquals(self::XML_PATH_GENERAL, $path); } - public function testBuildNodePath() + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testBuildNodePath(): void { - $environmentConfigLoaderHelper = new TestEnvLoaderHelper(); - $nodePath = $environmentConfigLoaderHelper->exposedBuildNodePath('DEFAULT', 'general/store_information/name'); - $this->assertEquals('default/general/store_information/name', $nodePath); + $environmentConfigLoaderHelper = new EnvironmentConfigLoaderTestHelper(); + $nodePath = $environmentConfigLoaderHelper->exposedBuildNodePath('DEFAULT', self::XML_PATH_GENERAL); + $this->assertEquals(self::XML_PATH_DEFAULT, $nodePath); } - public function test_xml_has_test_strings() + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testXmlHasTestStrings(): void { $xmlStruct = $this->getTestXml(); - $xml = new \Varien_Simplexml_Config(); + $xml = new Varien_Simplexml_Config(); $xml->loadString($xmlStruct); - $this->assertEquals('test_default', (string)$xml->getNode('default/general/store_information/name')); - $this->assertEquals('test_website', (string)$xml->getNode('websites/base/general/store_information/name')); - $this->assertEquals('test_store', (string)$xml->getNode('stores/german/general/store_information/name')); + $this->assertEquals('test_default', (string)$xml->getNode(self::XML_PATH_DEFAULT)); + $this->assertEquals('test_website', (string)$xml->getNode(self::XML_PATH_WEBSITE)); + $this->assertEquals('test_store', (string)$xml->getNode(self::XML_PATH_STORE)); } /** - * @dataProvider env_overrides_correct_config_keys - * @test + * @dataProvider envOverridesCorrectConfigKeysDataProvider + * @param array $config + * + * @group Mage_Core + * @group Mage_Core_Helper */ - public function env_overrides_for_valid_config_keys(array $config) + public function testEnvOverridesForValidConfigKeys(array $config): void { $xmlStruct = $this->getTestXml(); - $xmlDefault = new \Varien_Simplexml_Config(); + $xmlDefault = new Varien_Simplexml_Config(); $xmlDefault->loadString($xmlStruct); - $xml = new \Varien_Simplexml_Config(); + $xml = new Varien_Simplexml_Config(); $xml->loadString($xmlStruct); - // act + // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation $loader = new Mage_Core_Helper_EnvironmentConfigLoader(); $loader->setEnvStore([ $config['env_path'] => $config['value'] @@ -78,7 +108,10 @@ public function env_overrides_for_valid_config_keys(array $config) $this->assertNotEquals((string)$defaultValue, (string)$valueAfterOverride, 'Default value was not overridden.'); } - public function env_overrides_correct_config_keys(): array + /** + * @return array>> + */ + public function envOverridesCorrectConfigKeysDataProvider(): array { $defaultPath = 'OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME'; $defaultPathWithDash = 'OPENMAGE_CONFIG__DEFAULT__GENERAL__FOO-BAR__NAME'; @@ -96,7 +129,7 @@ public function env_overrides_correct_config_keys(): array [ 'Case DEFAULT overrides.' => [ 'case' => 'DEFAULT', - 'xml_path' => 'default/general/store_information/name', + 'xml_path' => self::XML_PATH_DEFAULT, 'env_path' => $defaultPath, 'value' => 'default_new_value' ] @@ -120,7 +153,7 @@ public function env_overrides_correct_config_keys(): array [ 'Case STORE overrides.' => [ 'case' => 'STORE', - 'xml_path' => 'stores/german/general/store_information/name', + 'xml_path' => self::XML_PATH_STORE, 'env_path' => $storePath, 'value' => 'store_new_value' ] @@ -144,7 +177,7 @@ public function env_overrides_correct_config_keys(): array [ 'Case WEBSITE overrides.' => [ 'case' => 'WEBSITE', - 'xml_path' => 'websites/base/general/store_information/name', + 'xml_path' => self::XML_PATH_WEBSITE, 'env_path' => $websitePath, 'value' => 'website_new_value' ] @@ -169,41 +202,44 @@ public function env_overrides_correct_config_keys(): array } /** - * @dataProvider env_does_not_override_on_wrong_config_keys - * @test + * @dataProvider envDoesNotOverrideOnWrongConfigKeysDataProvider + * @param array $config + * + * @group Mage_Core */ - public function env_does_not_override_for_invalid_config_keys(array $config) + public function testEnvDoesNotOverrideForInvalidConfigKeys(array $config): void { $xmlStruct = $this->getTestXml(); - $xmlDefault = new \Varien_Simplexml_Config(); + $xmlDefault = new Varien_Simplexml_Config(); $xmlDefault->loadString($xmlStruct); - $xml = new \Varien_Simplexml_Config(); + $xml = new Varien_Simplexml_Config(); $xml->loadString($xmlStruct); $defaultValue = 'test_default'; - $this->assertEquals($defaultValue, (string)$xml->getNode('default/general/store_information/name')); + $this->assertEquals($defaultValue, (string)$xml->getNode(self::XML_PATH_DEFAULT)); $defaultWebsiteValue = 'test_website'; - $this->assertEquals($defaultWebsiteValue, (string)$xml->getNode('websites/base/general/store_information/name')); + $this->assertEquals($defaultWebsiteValue, (string)$xml->getNode(self::XML_PATH_WEBSITE)); $defaultStoreValue = 'test_store'; - $this->assertEquals($defaultStoreValue, (string)$xml->getNode('stores/german/general/store_information/name')); + $this->assertEquals($defaultStoreValue, (string)$xml->getNode(self::XML_PATH_STORE)); - // act + // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation $loader = new Mage_Core_Helper_EnvironmentConfigLoader(); $loader->setEnvStore([ $config['path'] => $config['value'] ]); $loader->overrideEnvironment($xml); + $valueAfterCheck = ''; switch ($config['case']) { case 'DEFAULT': - $valueAfterCheck = $xml->getNode('default/general/store_information/name'); + $valueAfterCheck = $xml->getNode(self::XML_PATH_DEFAULT); break; case 'STORE': - $valueAfterCheck = $xml->getNode('stores/german/general/store_information/name'); + $valueAfterCheck = $xml->getNode(self::XML_PATH_STORE); break; case 'WEBSITE': - $valueAfterCheck = $xml->getNode('websites/base/general/store_information/name'); + $valueAfterCheck = $xml->getNode(self::XML_PATH_WEBSITE); break; } @@ -211,11 +247,15 @@ public function env_does_not_override_for_invalid_config_keys(array $config) $this->assertTrue(!str_contains('value_will_not_be_changed', (string)$valueAfterCheck), 'Default value was wrongfully overridden.'); } - public function env_does_not_override_on_wrong_config_keys(): array + /** + * @return array>> + */ + public function envDoesNotOverrideOnWrongConfigKeysDataProvider(): array { $defaultPath = 'OPENMAGE_CONFIG__DEFAULT__GENERAL__ST'; $websitePath = 'OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__ST'; $storePath = 'OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__ST'; + return [ [ 'Case DEFAULT with ' . $defaultPath . ' will not override.' => [ diff --git a/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTestHelper.php b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTestHelper.php new file mode 100644 index 00000000000..3739d3d7c79 --- /dev/null +++ b/tests/unit/Mage/Core/Helper/EnvironmentConfigLoaderTestHelper.php @@ -0,0 +1,33 @@ +buildPath($section, $group, $field); + } + + public function exposedBuildNodePath(string $scope, string $path): string + { + return $this->buildNodePath($scope, $path); + } +} diff --git a/tests/unit/Mage/Core/Helper/HintTest.php b/tests/unit/Mage/Core/Helper/HintTest.php new file mode 100644 index 00000000000..c9f3df4cbcf --- /dev/null +++ b/tests/unit/Mage/Core/Helper/HintTest.php @@ -0,0 +1,47 @@ +subject = Mage::helper('core/hint'); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetHintByCode(): void + { + $this->assertNull($this->subject->getHintByCode('test')); + } +} diff --git a/tests/unit/Mage/Core/Helper/JsTest.php b/tests/unit/Mage/Core/Helper/JsTest.php new file mode 100644 index 00000000000..e125a21b533 --- /dev/null +++ b/tests/unit/Mage/Core/Helper/JsTest.php @@ -0,0 +1,110 @@ +subject = Mage::helper('core/js'); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetTranslateJson(): void + { + $this->assertIsString($this->subject->getTranslateJson()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetTranslatorScript(): void + { + $this->assertIsString($this->subject->getTranslatorScript()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testIncludeScript(): void + { + $this->assertIsString($this->subject->includeScript('test')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testIncludeSkinScript(): void + { + $this->assertIsString($this->subject->includeSkinScript('test')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetDeleteConfirmJs(): void + { + $this->assertStringStartsWith('deleteConfirm', $this->subject->getDeleteConfirmJs('foo')); + $this->assertStringStartsWith('deleteConfirm', $this->subject->getDeleteConfirmJs('foo', 'bar')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetConfirmSetLocationJs(): void + { + $this->assertStringStartsWith('confirmSetLocation', $this->subject->getConfirmSetLocationJs('foo')); + $this->assertStringStartsWith('confirmSetLocation', $this->subject->getConfirmSetLocationJs('foo', 'bar')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetSetLocationJs(): void + { + $this->assertStringStartsWith('setLocation', $this->subject->getSetLocationJs('foo')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetSaveAndContinueEditJs(): void + { + $this->assertStringStartsWith('saveAndContinueEdit', $this->subject->getSaveAndContinueEditJs('foo')); + } +} diff --git a/tests/unit/Mage/Core/Helper/PurifierTest.php b/tests/unit/Mage/Core/Helper/PurifierTest.php new file mode 100644 index 00000000000..3667b9eb011 --- /dev/null +++ b/tests/unit/Mage/Core/Helper/PurifierTest.php @@ -0,0 +1,48 @@ +subject = Mage::helper('core/purifier'); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testPurify(): void + { + $this->assertIsArray($this->subject->purify([])); + $this->assertIsString($this->subject->purify('')); + } +} diff --git a/tests/unit/Mage/Core/Helper/SecurityTest.php b/tests/unit/Mage/Core/Helper/SecurityTest.php new file mode 100644 index 00000000000..0b470d4d24d --- /dev/null +++ b/tests/unit/Mage/Core/Helper/SecurityTest.php @@ -0,0 +1,157 @@ +subject = Mage::helper('core/security'); + } + + /** + * @return array|Mage_Page_Block_Html_Topmenu_Renderer|Mage_Core_Block_Template|string>> + * + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function validateAgainstBlockMethodBlacklistDataProvider(): array + { + // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation + $topmenu = new Mage_Page_Block_Html_Topmenu_Renderer(); + // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation + $template = new Mage_Core_Block_Template(); + + return [ + [ + $topmenu, + 'setData', + [] + ], + [ + $template, + 'setData', + [] + ], + ]; + } + + /** + * @dataProvider validateAgainstBlockMethodBlacklistDataProvider + * @doesNotPerformAssertions if data is correct, then NO exception is thrown, so we don't need an assertion + * @param Mage_Core_Block_Abstract $block + * @param string $method + * @param string[] $args + * @return void + * @throws Mage_Core_Exception + * + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testValidateAgainstBlockMethodBlacklist( + Mage_Core_Block_Abstract $block, + string $method, + array $args + ): void { + $this->subject->validateAgainstBlockMethodBlacklist($block, $method, $args); + } + + /** + * @return array|Mage_Page_Block_Html_Topmenu_Renderer|Mage_Core_Block_Template|string>> + */ + public function forbiddenBlockMethodsDataProvider(): array + { + // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation + $topmenu = new Mage_Page_Block_Html_Topmenu_Renderer(); + // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation + $template = new Mage_Core_Block_Template(); + + return [ + [ + $template, + 'fetchView', + [] + ], + [ + $topmenu, + 'fetchView', + [] + ], + [ + $topmenu, + 'render', + [] + ], + [ + $template, + 'Mage_Core_Block_Template::fetchView', + [] + ], + [ + $topmenu, + 'Mage_Page_Block_Html_Topmenu_Renderer::fetchView', + [] + ], + 'parent class name is passed as second arg' => [ + $topmenu, + 'Mage_Core_Block_Template::fetchView', + [] + ], + 'parent class name is passed as second arg2' => [ + $topmenu, + 'Mage_Core_Block_Template::render', + [] + ], + ]; + } + + /** + * @dataProvider forbiddenBlockMethodsDataProvider + * @param Mage_Core_Block_Abstract $block + * @param string $method + * @param string[] $args + * @return void + * @throws Mage_Core_Exception + * + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testValidateAgainstBlockMethodBlacklistThrowsException( + Mage_Core_Block_Abstract $block, + string $method, + array $args + ): void { + $this->expectExceptionMessage(sprintf('Action with combination block %s and method %s is forbidden.', get_class($block), $method)); + $this->subject->validateAgainstBlockMethodBlacklist($block, $method, $args); + } +} diff --git a/tests/unit/Mage/Core/Helper/StringTest.php b/tests/unit/Mage/Core/Helper/StringTest.php new file mode 100644 index 00000000000..734c64edf8c --- /dev/null +++ b/tests/unit/Mage/Core/Helper/StringTest.php @@ -0,0 +1,151 @@ +subject = Mage::helper('core/string'); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testTruncate(): void + { + $this->assertEquals('', $this->subject->truncate(null)); + $this->assertEquals('', $this->subject->truncate(self::TEST_STRING, 0)); + + $this->assertEquals('', $this->subject->truncate(self::TEST_STRING, 3)); + + $remainder = ''; + $this->assertEquals('12...', $this->subject->truncate(self::TEST_STRING, 5, '...', $remainder, false)); + + $resultString = $this->subject->truncate(self::TEST_STRING, 5, '...'); + $this->assertEquals('12...', $resultString); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testSubstr(): void + { + $resultString = $this->subject->substr(self::TEST_STRING, 2, 2); + $this->assertEquals('34', $resultString); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testSplitInjection(): void + { + $resultString = $this->subject->splitInjection(self::TEST_STRING, 1, '-', ' '); + #$this->assertEquals('1-2-3-4-5-6-7-8-9-0-', $resultString); + $this->assertIsString($resultString); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testStrlen(): void + { + $this->assertEquals(10, $this->subject->strlen(self::TEST_STRING)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testStrSplit(): void + { + $this->assertIsArray($this->subject->str_split('')); + $this->assertIsArray($this->subject->str_split(self::TEST_STRING)); + $this->assertIsArray($this->subject->str_split(self::TEST_STRING, 3)); + $this->assertIsArray($this->subject->str_split(self::TEST_STRING, 3, true, true)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testSplitWords(): void + { + $this->assertIsArray($this->subject->splitWords(null)); + $this->assertIsArray($this->subject->splitWords('')); + $this->assertIsArray($this->subject->splitWords(self::TEST_STRING)); + $this->assertIsArray($this->subject->splitWords(self::TEST_STRING, true)); + $this->assertIsArray($this->subject->splitWords(self::TEST_STRING, true, 1)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testParseQueryStr(): void + { + $this->assertIsArray($this->subject->parseQueryStr(self::TEST_STRING)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetArrayHelper(): void + { + $this->assertInstanceOf(Mage_Core_Helper_Array::class, $this->subject->getArrayHelper()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testUnserialize(): void + { + $this->assertNull($this->subject->unserialize(null)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testValidateSerializedObject(): void + { + $this->assertIsBool($this->subject->validateSerializedObject(self::TEST_STRING)); + $this->assertIsBool($this->subject->validateSerializedObject(self::TEST_STRING_JSON)); + } +} diff --git a/tests/unit/Mage/Core/Helper/UrlTest.php b/tests/unit/Mage/Core/Helper/UrlTest.php new file mode 100644 index 00000000000..c48530b8d14 --- /dev/null +++ b/tests/unit/Mage/Core/Helper/UrlTest.php @@ -0,0 +1,111 @@ +subject = Mage::helper('core/url'); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetCurrentBase64Url(): void + { + $this->assertIsString($this->subject->getCurrentBase64Url()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetEncodedUrl(): void + { + $this->assertIsString($this->subject->getEncodedUrl()); + $this->assertIsString($this->subject->getEncodedUrl(self::TEST_URL_1)); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testGetHomeUrl(): void + { + $this->assertIsString($this->subject->getHomeUrl()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testAddRequestParam(): void + { + $this->assertIsString($this->subject->addRequestParam(self::TEST_URL_1, [0 => 'int'])); + $this->assertIsString($this->subject->addRequestParam(self::TEST_URL_1, ['null' => null])); + $this->assertIsString($this->subject->addRequestParam(self::TEST_URL_1, ['key' => 'value'])); + $this->assertIsString($this->subject->addRequestParam(self::TEST_URL_1, ['key' => ['subKey' => 'subValue']])); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testRemoveRequestParam(): void + { + $this->assertIsString($this->subject->removeRequestParam(self::TEST_URL_1, 'foo')); + $this->assertIsString($this->subject->removeRequestParam(self::TEST_URL_2, 'foo')); + } + + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testEncodePunycode(): void + { + $this->assertIsString($this->subject->encodePunycode(self::TEST_URL_1)); + $this->assertIsString($this->subject->encodePunycode(self::TEST_URL_PUNY)); + } + /** + * @group Mage_Core + * @group Mage_Core_Helper + */ + public function testDecodePunycode(): void + { + $this->assertIsString($this->subject->decodePunycode(self::TEST_URL_1)); + $this->assertIsString($this->subject->decodePunycode(self::TEST_URL_PUNY)); + } +} diff --git a/tests/unit/Mage/Core/Model/LocaleTest.php b/tests/unit/Mage/Core/Model/LocaleTest.php new file mode 100644 index 00000000000..2ec18a03617 --- /dev/null +++ b/tests/unit/Mage/Core/Model/LocaleTest.php @@ -0,0 +1,82 @@ +subject = Mage::getModel('core/locale'); + } + + /** + * @dataProvider provideGetNumberData + * @param float|null $expectedResult + * @param string|float|int $value + * @return void + * + * @group Mage_Core + */ + public function testGetNumber(?float $expectedResult, $value): void + { + $this->assertEquals($expectedResult, $this->subject->getNumber($value)); + } + + /** + * @return array|float|int|string|null>> + */ + public function provideGetNumberData(): array + { + return [ + 'array' => [ + 1.0, + [1] + ], + 'int' => [ + 1.0, + 1 + ], + 'string' => [ + 1.0, + '1' + ], + 'string_comma' => [ + 1.0, + '1,0' + ], + 'string_dot' => [ + 1.0, + '1.0' + ], + 'null' => [ + null, + null + ], + ]; + } +} diff --git a/tests/unit/Mage/Core/Model/Security/HtmlEscapedStringTest.php b/tests/unit/Mage/Core/Model/Security/HtmlEscapedStringTest.php new file mode 100644 index 00000000000..48f82fa68bf --- /dev/null +++ b/tests/unit/Mage/Core/Model/Security/HtmlEscapedStringTest.php @@ -0,0 +1,101 @@ +string'; + + /** + * @var Mage_Core_Model_Security_HtmlEscapedString + */ + public Mage_Core_Model_Security_HtmlEscapedString $subject; + + /** + * @dataProvider provideHtmlEscapedStringAsStringData + * @param string $expectedResult + * @param string $string + * @param array $allowedTags + * @return void + * + * @group Mage_Core + */ + public function testToSting(string $expectedResult, string $string, ?array $allowedTags): void + { + // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation + $this->subject = new Mage_Core_Model_Security_HtmlEscapedString($string, $allowedTags); + $this->assertEquals($expectedResult, $this->subject->__toString()); + } + + /** + * @dataProvider provideHtmlEscapedStringGetUnescapedValueData + * @param string $expectedResult + * @param string $string + * @param array $allowedTags + * @return void + * + * @group Mage_Core + */ + public function testGetUnescapedValue(string $expectedResult, string $string, ?array $allowedTags): void + { + // phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation + $this->subject = new Mage_Core_Model_Security_HtmlEscapedString($string, $allowedTags); + $this->assertEquals($expectedResult, $this->subject->getUnescapedValue()); + } + + /** + * @return array|string|null>> + */ + public function provideHtmlEscapedStringAsStringData(): array + { + return [ + 'tags_null' => [ + 'This is a bold <b>string</b>', + self::TEST_STRING, + null + ], + 'tags_array' => [ + self::TEST_STRING, + self::TEST_STRING, + ['b'] + ], + ]; + } + + /** + * @return array|string|null>> + */ + public function provideHtmlEscapedStringGetUnescapedValueData(): array + { + return [ + 'tags_null' => [ + self::TEST_STRING, + self::TEST_STRING, + null + ], + 'tags_array' => [ + self::TEST_STRING, + self::TEST_STRING, + ['some-invalid-value'] + ], + ]; + } +} diff --git a/tests/unit/Mage/Core/Model/WebsiteTest.php b/tests/unit/Mage/Core/Model/WebsiteTest.php new file mode 100644 index 00000000000..a25cda689b3 --- /dev/null +++ b/tests/unit/Mage/Core/Model/WebsiteTest.php @@ -0,0 +1,166 @@ +subject = Mage::getModel('core/website'); + } + + /** + * @group Mage_Core + */ + public function testLoad(): void + { + $this->assertInstanceOf(Mage_Core_Model_Website::class, $this->subject->load(1)); + $this->assertInstanceOf(Mage_Core_Model_Website::class, $this->subject->load('default')); + } + + /** + * @group Mage_Core + */ + public function testLoadConfig(): void + { + $this->assertInstanceOf(Mage_Core_Model_Website::class, $this->subject->loadConfig('1')); + $this->assertInstanceOf(Mage_Core_Model_Website::class, $this->subject->loadConfig('default')); + } + + /** + * @group Mage_Core + */ + public function testGetStoreCollection(): void + { + $this->assertInstanceOf(Mage_Core_Model_Resource_Store_Collection::class, $this->subject->getStoreCollection()); + } + + /** + * @group Mage_Core + */ + public function testGetGroupCollection(): void + { + $this->assertInstanceOf(Mage_Core_Model_Resource_Store_Group_Collection::class, $this->subject->getGroupCollection()); + } + + /** + * @group Mage_Core + */ + public function testGetStores(): void + { + $this->assertIsArray($this->subject->getStores()); + } + + /** + * @group Mage_Core + */ + public function testGetStoreIds(): void + { + $this->assertIsArray($this->subject->getStoreIds()); + } + + /** + * @group Mage_Core + */ + public function testGetStoreCodes(): void + { + $this->assertIsArray($this->subject->getStoreCodes()); + } + + /** + * @group Mage_Core + */ + public function testGetStoresCount(): void + { + $this->assertIsInt($this->subject->getStoresCount()); + } + + /** + * @group Mage_Core + */ + public function testGetGroups(): void + { + $this->assertIsArray($this->subject->getGroups()); + } + + /** + * @group Mage_Core + */ + public function testGetGroupIds(): void + { + $this->assertIsArray($this->subject->getGroupIds()); + } + + /** + * @group Mage_Core + */ + public function testGetGroupsCount(): void + { + $this->assertIsInt($this->subject->getGroupsCount()); + } + + /** + * @group Mage_Core + */ + public function testGetBaseCurrency(): void + { + $this->assertIsObject($this->subject->getBaseCurrency()); + $this->assertInstanceOf(Mage_Directory_Model_Currency::class, $this->subject->getBaseCurrency()); + } + +// /** +// * @group Mage_Core +// */ +// public function testGetDefaultStore(): void +// { +// $this->assertIsObject($this->subject->getDefaultStore()); +// $this->assertInstanceOf(Mage_Core_Model_Store::class, $this->subject->getDefaultStore()); +// } + + /** + * @group Mage_Core + */ + public function testGetDefaultStoresSelect(): void + { + $this->assertIsObject($this->subject->getDefaultStoresSelect()); + $this->assertInstanceOf(Varien_Db_Select::class, $this->subject->getDefaultStoresSelect(true)); + } + + /** + * @group Mage_Core + */ + public function testIsReadOnly(): void + { + $this->assertFalse($this->subject->isReadOnly()); + $this->assertTrue($this->subject->isReadOnly(true)); + } +} diff --git a/tests/unit/Mage/Customer/Model/Convert/Adapter/CustomerTest.php b/tests/unit/Mage/Customer/Model/Convert/Adapter/CustomerTest.php new file mode 100644 index 00000000000..bcd4c7ea96c --- /dev/null +++ b/tests/unit/Mage/Customer/Model/Convert/Adapter/CustomerTest.php @@ -0,0 +1,154 @@ +subject = Mage::getModel('customer/convert_adapter_customer'); + } + +// /** +// * @return void +// * @throws Throwable +// * +// * @group Mage_Customer +// */ +// public function testSaveRowNoWebsite(): void +// { +// $data = []; +// try { +// $this->subject->saveRow($data); +// $this->fail(); +// } catch (Mage_Core_Exception $e) { +// $this->assertEquals('Skipping import row, required field "website" is not defined.', $e->getMessage()); +// } +// } +// +// /** +// * @return void +// * @throws Throwable +// * +// * @group Mage_Customer +// */ +// public function testSaveRowNoEmail(): void +// { +// $data = [ +// 'website' => 'base', +// ]; +// try { +// $this->subject->saveRow($data); +// $this->fail(); +// } catch (Mage_Core_Exception $e) { +// $this->assertEquals('Skipping import row, required field "email" is not defined.', $e->getMessage()); +// } +// } +// +// /** +// * @return void +// * @throws Throwable +// * +// * @group Mage_Customer +// */ +// public function testSaveRowNoGroup(): void +// { +// $data = [ +// 'website' => 'base', +// 'email' => 'test@example.com', +// ]; +// try { +// $this->subject->saveRow($data); +// $this->fail(); +// } catch (Mage_Core_Exception $e) { +// $this->assertEquals('Skipping import row, the value "" is not valid for the "group" field.', $e->getMessage()); +// } +// } +// +// /** +// * @return void +// * @throws Throwable +// * +// * @group Mage_Customer +// */ +// public function testSaveRowNoFirstname(): void +// { +// $data = [ +// 'website' => 'base', +// 'email' => 'test@example.com', +// 'group' => 'General', +// ]; +// try { +// $this->subject->saveRow($data); +// $this->fail(); +// } catch (Mage_Core_Exception $e) { +// $this->expectExceptionMessage('Skip import row, required field "firstname" for the new customer is not defined.'); +// } +// } +// +// /** +// * @return void +// * @throws Throwable +// * +// * @group Mage_Customer +// */ +// public function testSaveRowNoLastname(): void +// { +// $data = [ +// 'website' => 'base', +// 'email' => 'test@example.com', +// 'group' => 'General', +// 'firstname' => 'John', +// ]; +// try { +// $this->subject->saveRow($data); +// $this->fail(); +// } catch (Mage_Core_Exception $e) { +// $this->assertEquals('Skip import row, required field "lastname" for the new customer is not defined.', $e->getMessage()); +// } +// } + + /** + * @return void + * @throws Throwable + * + * @group Mage_Customer + */ + public function testSaveRow(): void + { + $data = [ + 'website' => 'base', + 'email' => 'test@example.com', + 'group' => 'General', + 'firstname' => 'John', + 'lastname' => 'Doe', + ]; + $this->assertInstanceOf(Mage_Customer_Model_Convert_Adapter_Customer::class, $this->subject->saveRow($data)); + } +} diff --git a/tests/unit/Mage/Downloadable/Helper/FileTest.php b/tests/unit/Mage/Downloadable/Helper/FileTest.php new file mode 100644 index 00000000000..83d48bf4314 --- /dev/null +++ b/tests/unit/Mage/Downloadable/Helper/FileTest.php @@ -0,0 +1,85 @@ +subject = Mage::helper('downloadable/file'); + } + + /** + * @dataProvider provideGetFilePathData + * @param string $expectedResult + * @param string $path + * @param string|null $file + * @return void + * + * @group Mage_Downloadable + */ + public function testGetFilePath(string $expectedResult, string $path, ?string $file): void + { + $result = $this->subject->getFilePath($path, $file); + $this->assertEquals($expectedResult, $result); + } + + /** + * @return array> + */ + public function provideGetFilePathData(): array + { + return [ + 'strings path and strings file' => [ + 'path' . DS . 'file', + 'path', + 'file' + ], + 'strings path and strings file with slash' => [ + 'path' . DS . 'file', + 'path', + '/file' + ], + 'string path and null file' => [ + 'path' . DS, + 'path', + null + ], + 'string path and empty file' => [ + 'path' . DS, + 'path', + '' + ], + 'strings path and strings file named 0' => [ + 'path' . DS . '0', + 'path', + '0' + ], + ]; + } +} diff --git a/tests/unit/Mage/Log/Helper/DataTest.php b/tests/unit/Mage/Log/Helper/DataTest.php new file mode 100644 index 00000000000..698a8a6493c --- /dev/null +++ b/tests/unit/Mage/Log/Helper/DataTest.php @@ -0,0 +1,75 @@ +subject = Mage::helper('log/data'); + } + + /** + * @group Mage_Log + * @group Mage_Log_Helper + */ + public function testIsVisitorLogEnabled(): void + { + $this->assertIsBool($this->subject->isVisitorLogEnabled()); + } + + /** + * @group Mage_Log + * @group Mage_Log_Helper + */ + public function testIsLogEnabled(): void + { + $this->assertIsBool($this->subject->isLogEnabled()); + } + + /** + * @group Mage_Log + * @group Mage_Log_Helper + */ + public function testIsLogDisabled(): void + { + $this->assertIsBool($this->subject->isLogDisabled()); + } + + /** + * @group Mage_Log + * @group Mage_Log_Helper + */ + public function testIsLogFileExtensionValid(): void + { + $this->assertIsBool($this->subject->isLogFileExtensionValid('invalid.file')); + $this->assertIsBool($this->subject->isLogFileExtensionValid('valid.log')); + } +} diff --git a/tests/unit/Mage/Log/Model/CronTest.php b/tests/unit/Mage/Log/Model/CronTest.php new file mode 100644 index 00000000000..0d754ab0795 --- /dev/null +++ b/tests/unit/Mage/Log/Model/CronTest.php @@ -0,0 +1,45 @@ +subject = Mage::getModel('log/cron'); + } + + /** + * @group Mage_Log + * @group Mage_Log_Model + */ + public function testLogClean(): void + { + $this->assertInstanceOf(Mage_Log_Model_Cron::class, $this->subject->logClean()); + } +} diff --git a/tests/unit/Mage/Log/Model/LogTest.php b/tests/unit/Mage/Log/Model/LogTest.php new file mode 100644 index 00000000000..d6c3031da1f --- /dev/null +++ b/tests/unit/Mage/Log/Model/LogTest.php @@ -0,0 +1,45 @@ +subject = Mage::getModel('log/log'); + } + + /** + * @group Mage_Log + * @group Mage_Log_Model + */ + public function testClean(): void + { + $this->assertInstanceOf(Mage_Log_Model_Log::class, $this->subject->clean()); + } +} diff --git a/tests/unit/Mage/Log/Model/VisitorTest.php b/tests/unit/Mage/Log/Model/VisitorTest.php new file mode 100644 index 00000000000..25c33a92b13 --- /dev/null +++ b/tests/unit/Mage/Log/Model/VisitorTest.php @@ -0,0 +1,46 @@ +subject = Mage::getModel('log/visitor'); + } + + /** + * @group Mage_Log + * @group Mage_Log_Model + * @runInSeparateProcess + */ + public function testInitServerData(): void + { + $this->assertInstanceOf(Mage_Log_Model_Visitor::class, $this->subject->initServerData()); + } +} diff --git a/tests/unit/Mage/Page/Block/Html/HeadTest.php b/tests/unit/Mage/Page/Block/Html/HeadTest.php new file mode 100644 index 00000000000..a7a5366f6c7 --- /dev/null +++ b/tests/unit/Mage/Page/Block/Html/HeadTest.php @@ -0,0 +1,82 @@ +subject = new Mage_Page_Block_Html_Head(); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testAddCss(): void + { + $this->assertInstanceOf(Mage_Page_Block_Html_Head::class, $this->subject->addCss('test')); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testAddJs(): void + { + $this->assertInstanceOf(Mage_Page_Block_Html_Head::class, $this->subject->addJs('test')); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testAddCssIe(): void + { + $this->assertInstanceOf(Mage_Page_Block_Html_Head::class, $this->subject->addCssIe('test')); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testAddJsIe(): void + { + $this->assertInstanceOf(Mage_Page_Block_Html_Head::class, $this->subject->addJsIe('test')); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testAddLinkRel(): void + { + $this->assertInstanceOf(Mage_Page_Block_Html_Head::class, $this->subject->addLinkRel('test', 'ref')); + } +} diff --git a/tests/unit/Mage/Page/Block/Html/HeaderTest.php b/tests/unit/Mage/Page/Block/Html/HeaderTest.php new file mode 100644 index 00000000000..8651f82c8bf --- /dev/null +++ b/tests/unit/Mage/Page/Block/Html/HeaderTest.php @@ -0,0 +1,83 @@ +subject = new Mage_Page_Block_Html_Header(); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ +// public function testGetIsHomePage(): void +// { +// $this->assertIsBool($this->subject->getIsHomePage()); +// } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testSetLogo(): void + { + $this->assertInstanceOf(Mage_Page_Block_Html_Header::class, $this->subject->setLogo('src', 'alt')); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testGetLogoSrc(): void + { + $this->assertIsString($this->subject->getLogoSrc()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testGetLogoSrcSmall(): void + { + $this->assertIsString($this->subject->getLogoSrcSmall()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testGetLogoAlt(): void + { + $this->assertInstanceOf(Mage_Core_Model_Security_HtmlEscapedString::class, $this->subject->getLogoAlt()); + } +} diff --git a/tests/unit/Mage/Page/Block/HtmlTest.php b/tests/unit/Mage/Page/Block/HtmlTest.php new file mode 100644 index 00000000000..2701dd9ac69 --- /dev/null +++ b/tests/unit/Mage/Page/Block/HtmlTest.php @@ -0,0 +1,73 @@ +subject = new Mage_Page_Block_Html(); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testGetBaseUrl(): void + { + $this->assertIsString($this->subject->getBaseUrl()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testGetBaseSecureUrl(): void + { + $this->assertIsString($this->subject->getBaseSecureUrl()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ +// public function testGetCurrentUrl(): void +// { +// $this->assertIsString($this->subject->getCurrentUrl()); +// } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testGetPrintLogoUrl(): void + { + $this->assertIsString($this->subject->getPrintLogoUrl()); + } +} diff --git a/tests/unit/Mage/Page/Block/RedirectTest.php b/tests/unit/Mage/Page/Block/RedirectTest.php new file mode 100644 index 00000000000..333a2a67cd0 --- /dev/null +++ b/tests/unit/Mage/Page/Block/RedirectTest.php @@ -0,0 +1,109 @@ +subject = new Mage_Page_Block_Redirect(); + } + + /** + * @group Mage_Page + * @group Mage_Page_Model + */ + public function testGetTargetUrl(): void + { + $this->assertEquals('', $this->subject->getTargetURL()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetMessage(): void + { + $this->assertEquals('', $this->subject->getMessage()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetRedirectOutput(): void + { + $this->assertIsString($this->subject->getRedirectOutput()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetJsRedirect(): void + { + $this->assertIsString($this->subject->getJsRedirect()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetHtmlFormRedirect(): void + { + $this->assertIsString($this->subject->getHtmlFormRedirect()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testIsHtmlFormRedirect(): void + { + $this->assertIsBool($this->subject->isHtmlFormRedirect()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetFormId(): void + { + $this->assertEquals('', $this->subject->getFormId()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetFormMethod(): void + { + $this->assertEquals('POST', $this->subject->getFormMethod()); + } +} diff --git a/tests/unit/Mage/Page/Block/SwitchTest.php b/tests/unit/Mage/Page/Block/SwitchTest.php new file mode 100644 index 00000000000..b0b7049c04b --- /dev/null +++ b/tests/unit/Mage/Page/Block/SwitchTest.php @@ -0,0 +1,118 @@ +subject = new Mage_Page_Block_Switch(); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetCurrentWebsiteId(): void + { + $this->assertIsString($this->subject->getCurrentWebsiteId()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetCurrentGroupId(): void + { + $this->assertIsString($this->subject->getCurrentGroupId()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetCurrentStoreId(): void + { + $this->assertIsString($this->subject->getCurrentStoreId()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetCurrentStoreCode(): void + { + $this->assertIsString($this->subject->getCurrentStoreCode()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testGetRawGroups(): void + { + $this->assertIsArray($this->subject->getRawGroups()); + } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ +// public function testGetRawStores(): void +// { +// $this->assertIsArray($this->subject->getRawStores()); +// } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ +// public function testGetGroups(): void +// { +// $this->assertIsArray($this->subject->getGroups()); +// } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ +// public function testGetStores(): void +// { +// $this->assertIsArray($this->subject->getStores()); +// } + + /** + * @group Mage_Page + * @group Mage_Page_Block + */ + public function testIsStoreInUrl(): void + { + $this->assertIsBool($this->subject->isStoreInUrl()); + } +} diff --git a/tests/unit/Mage/Sitemap/Model/SitemapTest.php b/tests/unit/Mage/Sitemap/Model/SitemapTest.php new file mode 100644 index 00000000000..ef5c6ec880a --- /dev/null +++ b/tests/unit/Mage/Sitemap/Model/SitemapTest.php @@ -0,0 +1,49 @@ +subject = Mage::getModel('sitemap/sitemap'); + } + + /** + * @group Mage_Sitemap + */ + public function testGenerateXml(): void + { + $mock = $this->getMockBuilder(Mage_Sitemap_Model_Sitemap::class) + ->setMethods(['getSitemapFilename']) + ->getMock(); + + $mock->expects($this->any())->method('getSitemapFilename')->willReturn('text.xml'); + $this->assertInstanceOf(Mage_Sitemap_Model_Sitemap::class, $mock->generateXml()); + } +} diff --git a/tests/unit/Mage/Uploader/Helper/DataTest.php b/tests/unit/Mage/Uploader/Helper/DataTest.php new file mode 100644 index 00000000000..735f8d95fad --- /dev/null +++ b/tests/unit/Mage/Uploader/Helper/DataTest.php @@ -0,0 +1,43 @@ +subject = Mage::helper('uploader/data'); + } + + /** + * @group Mage_Uploader + */ + public function testIsModuleEnabled(): void + { + $this->assertIsBool($this->subject->isModuleEnabled()); + } +} diff --git a/dev/tests/unit/Mage/Uploader/Helper/FileTest.php b/tests/unit/Mage/Uploader/Helper/FileTest.php similarity index 71% rename from dev/tests/unit/Mage/Uploader/Helper/FileTest.php rename to tests/unit/Mage/Uploader/Helper/FileTest.php index 72db51d7ec1..e815c3cf5de 100644 --- a/dev/tests/unit/Mage/Uploader/Helper/FileTest.php +++ b/tests/unit/Mage/Uploader/Helper/FileTest.php @@ -1,10 +1,24 @@ setNode('global/mime/types/test-new-node', 'application/octet-stream'); + + /** @var Mage_Core_Model_Config $config */ + $config = Mage::getConfig(); + $config->setNode('global/mime/types/test-new-node', 'application/octet-stream'); $this->subject = Mage::helper('uploader/file'); } @@ -27,10 +44,12 @@ public function setUp(): void * @param array $expectedResult * @param string|array $extensionsList * @return void + * + * @group Mage_Uploader */ public function testGetMimeTypeFromExtensionList(array $expectedResult, $extensionsList): void { - self::assertSame($expectedResult, $this->subject->getMimeTypeFromExtensionList($extensionsList)); + $this->assertEquals($expectedResult, $this->subject->getMimeTypeFromExtensionList($extensionsList)); } /** @@ -66,16 +85,25 @@ public function provideGetMimeTypeFromExtensionListData(): array ]; } + /** + * @group Mage_Uploader + */ public function testGetPostMaxSize(): void { - self::assertIsString($this->subject->getPostMaxSize()); + $this->assertIsString($this->subject->getPostMaxSize()); } + /** + * @group Mage_Uploader + */ public function testGetUploadMaxSize(): void { - self::assertIsString($this->subject->getUploadMaxSize()); + $this->assertIsString($this->subject->getUploadMaxSize()); } + /** + * @group Mage_Uploader + */ public function testGetDataMaxSize(): void { $mock = $this->getMockBuilder(Mage_Uploader_Helper_File::class) @@ -84,7 +112,7 @@ public function testGetDataMaxSize(): void $mock->expects($this->once())->method('getPostMaxSize')->willReturn('1G'); $mock->expects($this->once())->method('getUploadMaxSize')->willReturn('1M'); - self::assertSame('1M', $mock->getDataMaxSize()); + $this->assertEquals('1M', $mock->getDataMaxSize()); } /** @@ -92,6 +120,8 @@ public function testGetDataMaxSize(): void * @param int $expectedResult * @param string $maxSize * @return void + * + * @group Mage_Uploader */ public function testGetDataMaxSizeInBytes(int $expectedResult, string $maxSize): void { @@ -100,7 +130,7 @@ public function testGetDataMaxSizeInBytes(int $expectedResult, string $maxSize): ->getMock(); $mock->expects($this->once())->method('getDataMaxSize')->willReturn($maxSize); - self::assertSame($expectedResult, $mock->getDataMaxSizeInBytes()); + $this->assertEquals($expectedResult, $mock->getDataMaxSizeInBytes()); } /** diff --git a/dev/tests/unit/Varien/Db/VarienDbAdapterPdoMysqlTest.php b/tests/unit/Varien/Db/Adapter/Pdo/MysqlTest.php similarity index 69% rename from dev/tests/unit/Varien/Db/VarienDbAdapterPdoMysqlTest.php rename to tests/unit/Varien/Db/Adapter/Pdo/MysqlTest.php index 0d36667c07d..c1d3b341aef 100644 --- a/dev/tests/unit/Varien/Db/VarienDbAdapterPdoMysqlTest.php +++ b/tests/unit/Varien/Db/Adapter/Pdo/MysqlTest.php @@ -1,14 +1,30 @@ getConstructor(); $constructor->invoke($this->adapter, $config); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithUnixSocket(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); $fakeSocket = '/var/run/mysqld/mysqld.sock'; + + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, $fakeSocket); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_UNIX_SOCKET); @@ -46,11 +68,15 @@ public function testGetHostInfoWithUnixSocket(): void $this->assertNull($hostInfo->getPort()); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithIpv4Address(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, '192.168.1.1:3306'); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV4_ADDRESS); @@ -59,11 +85,15 @@ public function testGetHostInfoWithIpv4Address(): void $this->assertNull($hostInfo->getUnixSocket()); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithIpv4AddressWithoutPort(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, '192.168.1.1'); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV4_ADDRESS); @@ -72,11 +102,15 @@ public function testGetHostInfoWithIpv4AddressWithoutPort(): void $this->assertNull($hostInfo->getUnixSocket()); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithHostname(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, 'db.example.com:3306'); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_HOSTNAME); @@ -85,11 +119,15 @@ public function testGetHostInfoWithHostname(): void $this->assertNull($hostInfo->getUnixSocket()); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithHostnameWithoutPort(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, 'db.example.com'); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_HOSTNAME); @@ -98,11 +136,15 @@ public function testGetHostInfoWithHostnameWithoutPort(): void $this->assertNull($hostInfo->getUnixSocket()); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithIpv6Address(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, '[2001:db8::1]:3306'); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV6_ADDRESS); @@ -111,11 +153,15 @@ public function testGetHostInfoWithIpv6Address(): void $this->assertNull($hostInfo->getUnixSocket()); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithIpv6AddressWithoutPort(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, '2001:db8::1'); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV6_ADDRESS); @@ -124,11 +170,15 @@ public function testGetHostInfoWithIpv6AddressWithoutPort(): void $this->assertNull($hostInfo->getUnixSocket()); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithIpv6AddressWithZoneId(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, '[fe80::1%eth0]:3306'); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV6_ADDRESS); @@ -137,11 +187,15 @@ public function testGetHostInfoWithIpv6AddressWithZoneId(): void $this->assertNull($hostInfo->getUnixSocket()); } + /** + * @group Varien_Db + */ public function testGetHostInfoWithIpv6AddressWithZoneIdWithoutPort(): void { - $method = new \ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); + $method = new ReflectionMethod(Varien_Db_Adapter_Pdo_Mysql::class, '_getHostInfo'); $method->setAccessible(true); + /** @var Varien_Object $hostInfo */ $hostInfo = $method->invoke($this->adapter, 'fe80::1%eth0'); $this->assertEquals($hostInfo->getAddressType(), Varien_Db_Adapter_Pdo_Mysql::ADDRESS_TYPE_IPV6_ADDRESS); @@ -149,4 +203,4 @@ public function testGetHostInfoWithIpv6AddressWithZoneIdWithoutPort(): void $this->assertNull($hostInfo->getPort()); $this->assertNull($hostInfo->getUnixSocket()); } -} \ No newline at end of file +} diff --git a/tests/unit/Varien/ObjectTest.php b/tests/unit/Varien/ObjectTest.php new file mode 100644 index 00000000000..928275852a4 --- /dev/null +++ b/tests/unit/Varien/ObjectTest.php @@ -0,0 +1,261 @@ +subject = new Varien_Object(); + } + + /** + * @dataProvider provideGetDataData + * @param mixed $expectedResult + * @param string $setKey + * @param mixed $setValue + * @param string $key + * @param string|int|null $index + * @return void + * + * @group Varien_Object + */ + public function testGetData($expectedResult, $setKey, $setValue, string $key, $index = null): void + { + $this->subject->setData($setKey, $setValue); + $this->assertEquals($expectedResult, $this->subject->getData($key, $index)); + } + + /** + * @return array|int|string>|int|stdClass|string|Varien_Object|null>> + */ + public function provideGetDataData(): array + { + return [ + 'empty_key' => [ + ['empty_key' => ['empty_value']], + 'empty_key', + ['empty_value'], + '' + ], + 'string' => [ + 'value', + 'string', + 'value', + 'string' + ], + 'int' => [ + 1, + 'int', + 1, + 'int' + ], + 'numeric' => [ + '1', + 'numeric', + '1', + 'numeric' + ], + 'array' => [ + ['string', 1], + 'array', + ['string', 1], + 'array', + ], + 'array_index_int' => [ + 'string', + 'array_index_int', + ['string', 1], + 'array_index_int', + 0, + ], + 'array_index_int_invalid' => [ + null, + 'array_index_int_invalid', + ['string', 1], + 'array_index_int_invalid', + 999, + ], + 'array_index_string' => [ + 1, + 'array_index_string', + ['string' => 'string', 'int' => 1], + 'array_index_string', + 'int', + ], + 'array_index_string_string' => [ + null, + 'array_index_string_string', + 'some_string', + 'array_index_string_string', + 'not-exists', + ], + 'array_index_string_varien_object' => [ + [], + 'array_index_string_varien_object', + new Varien_Object(['array' => []]), + 'array_index_string_varien_object', + 'array', + ], + 'array_index_string_std_class' => [ + null, + 'array_index_string_std_class', + new stdClass(), + 'array_index_string_std_class', + 'not-exists', + ], + 'array_nested' => [ + 1, + 'array_nested', + ['nested' => ['string' => 'string', 'int' => 1]], + 'array_nested/nested/int', + ], + 'array_nested_invalid_key' => [ + null, + 'array_nested', + ['nested' => ['string' => 'string', 'int' => 1]], + 'array_nested/nested/invalid_key', + ], + 'array_nested_empty_key' => [ + null, + 'array_nested', + ['nested' => ['string' => 'string', 'int' => '']], + 'array_nested/nested/', + ], + 'array_nested_string' => [ + 'some"\n"string', + 'array_nested_string', + ['nested' => 'some"\n"string'], + 'array_nested_string/nested', + ], + 'array_nested_varien_object' => [ + null, + 'array_nested_varien_object', + new Varien_Object(), + 'array_nested_varien_object/nested', + ], + 'array_nested_std_class' => [ + null, + 'array_nested_std_class', + new stdClass(), + 'array_nested_std_class/nested', + ], + 'array_nested_key_not_exists' => [ + null, + 'array_nested_key_not_exists', + ['nested' => ['string' => 'string', 'int' => 1]], + 'array_nested_key_not_exists_test/nested/int', + ], + ]; + } + + /** + * @group Varien_Object + */ + public function testToString(): void + { + $this->subject->setString1('open'); + $this->subject->setString2('mage'); + $this->assertEquals('open, mage', $this->subject->toString()); + $this->assertEquals('openmage', $this->subject->toString('{{string1}}{{string2}}')); + $this->assertEquals('open', $this->subject->toString('{{string1}}{{string_not_exists}}')); + } + + /** + * @group Varien_Object + */ + public function testGetSetUnsData(): void + { + $this->assertTrue($this->subject->isEmpty()); + $this->subject->setABC('abc'); + $this->subject->setData('efg', 'efg'); + $this->subject->set123('123'); + $this->subject->setData('345', '345'); + $this->subject->setKeyAFirst('value_a_first'); + $this->subject->setData('key_a_2nd', 'value_a_2nd'); + $this->subject->setKeyA3rd('value_a_3rd'); + $this->subject->setData('left', 'over'); + $this->assertFalse($this->subject->isEmpty()); + + $this->assertEquals('abc', $this->subject->getData('a_b_c')); + $this->assertEquals('abc', $this->subject->getABC()); + $this->subject->unsetData('a_b_c'); + + $this->assertEquals('efg', $this->subject->getData('efg')); + $this->assertEquals('efg', $this->subject->getEfg()); + $this->subject->unsEfg(); + + $this->assertEquals('123', $this->subject->getData('123')); + $this->assertEquals('123', $this->subject->get123()); + $this->subject->uns123(); + + $this->subject->unsetData('345'); + + $this->assertEquals('value_a_first', $this->subject->getData('key_a_first')); + $this->assertEquals('value_a_first', $this->subject->getKeyAFirst()); + $this->subject->unsetData('key_a_first'); + + $this->assertEquals('value_a_2nd', $this->subject->getData('key_a_2nd')); + $this->assertEquals('value_a_2nd', $this->subject->getKeyA_2nd()); + $this->subject->unsetData('key_a_2nd'); + + $this->assertEquals('value_a_3rd', $this->subject->getData('key_a3rd')); + $this->assertEquals('value_a_3rd', $this->subject->getKeyA3rd()); + $this->subject->unsetData('key_a3rd'); + + $this->assertEquals(['left' => 'over'], $this->subject->getData()); + + $this->subject->unsetData(); + $this->assertEquals([], $this->subject->getData()); + $this->assertTrue($this->subject->isEmpty()); + + try { + /** @phpstan-ignore-next-line */ + $this->subject->notData(); + $this->fail('Invalid __call'); + } catch (Varien_Exception $exception) { + $this->assertStringStartsWith('Invalid method', $exception->getMessage()); + } + } + + /** + * @group Varien_Object + */ + public function testOffset(): void + { + $this->assertFalse($this->subject->offsetExists('off')); + + $this->subject->offsetSet('off', 'set'); + $this->assertTrue($this->subject->offsetExists('off')); + $this->assertEquals('set', $this->subject->offsetGet('off')); + $this->assertEquals(null, $this->subject->offsetGet('not-exists')); + + $this->subject->offsetUnset('off'); + $this->assertFalse($this->subject->offsetExists('off')); + } +}