From 0ecf4774a15dacefb4edd3cb949253368a2383cf Mon Sep 17 00:00:00 2001 From: Kevin Horst Date: Thu, 27 Apr 2023 13:55:57 +0200 Subject: [PATCH] add console create unit test --- .github/workflows/ci.yml | 16 +++++ .gitignore | 3 +- composer.json | 9 ++- phpunit.xml.dist | 17 +++++ tests/Console/AbstractConsoleTest.php | 30 ++++++++ tests/Console/CreateTest.php | 69 +++++++++++++++++++ .../stubs/company/my-package/.gitignore | 3 + .../stubs/company/my-package/.travis.yml | 13 ++++ .../stubs/company/my-package/composer.json | 17 +++++ .../stubs/company/my-package/phpspec.yml | 4 ++ .../stubs/company/my-package/phpunit.xml | 19 +++++ .../stubs/company/my-package/src/Example.php | 8 +++ .../company/my-package/tests/ExampleTest.php | 10 +++ 13 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 tests/Console/AbstractConsoleTest.php create mode 100644 tests/Console/CreateTest.php create mode 100644 tests/Console/stubs/company/my-package/.gitignore create mode 100644 tests/Console/stubs/company/my-package/.travis.yml create mode 100644 tests/Console/stubs/company/my-package/composer.json create mode 100644 tests/Console/stubs/company/my-package/phpspec.yml create mode 100644 tests/Console/stubs/company/my-package/phpunit.xml create mode 100644 tests/Console/stubs/company/my-package/src/Example.php create mode 100644 tests/Console/stubs/company/my-package/tests/ExampleTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 925a091..981318e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,4 +19,20 @@ jobs: run: composer install --prefer-dist --no-progress - name: Run tests run: php vendor/bin/phpspec run + phpunit: + runs-on: ubuntu-latest + strategy: + matrix: + php: [8.1, 8.2] + steps: + - uses: actions/checkout@v1 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + - name: Validate Composer files + run: composer validate --no-check-all --strict + - name: Install Composer dependencies + run: composer install --prefer-dist --no-progress + - name: Run tests + run: php vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index aa0f446..e73553f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ +.phpunit.result.cache studio.json composer.phar composer.lock -vendor/ +vendor/ \ No newline at end of file diff --git a/composer.json b/composer.json index 02397fe..1662739 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,11 @@ "Studio\\": "src" } }, + "autoload-dev": { + "psr-4": { + "StudioTests\\": "tests" + } + }, "require": { "php": ">=8.1", "composer-plugin-api": "^2.3", @@ -18,7 +23,9 @@ }, "require-dev": { "composer/composer": "^2.5", - "phpspec/phpspec": "^7.4" + "phpspec/phpspec": "^7.4", + "phpunit/phpunit": "^9.6 | ^10", + "mikey179/vfsstream": "^1.6.11" }, "replace": { "franzliedke/studio": "self.version" diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..8092340 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,17 @@ + + + + + + + tests + tests/**/stubs + + + + + + + + + \ No newline at end of file diff --git a/tests/Console/AbstractConsoleTest.php b/tests/Console/AbstractConsoleTest.php new file mode 100644 index 0000000..ad5b581 --- /dev/null +++ b/tests/Console/AbstractConsoleTest.php @@ -0,0 +1,30 @@ +add(new CreateCommand); + + // this uses a special testing container that allows you to fetch private services + /** @var Command $command */ + $command = $application->get($this->getCommandFqcn()); + + $commandTester = new CommandTester($command); + $commandTester->setInputs($inputs); + $commandTester->execute($arguments); + + return $commandTester; + } + + abstract protected function getCommandFqcn(): string; +} \ No newline at end of file diff --git a/tests/Console/CreateTest.php b/tests/Console/CreateTest.php new file mode 100644 index 0000000..b4e27e7 --- /dev/null +++ b/tests/Console/CreateTest.php @@ -0,0 +1,69 @@ +root = vfsStream::setup(); + } + + function testExecute(): void + { + $commandTester = $this->executeCommand( + ['path' => $this->root->url() . '/company/my-package'], + [ + // package name + 'company/my-package', + // default namespace (psr-4) + 'Company/MyPackage', + // set up PhpUnit + 'yes', + // set up PhpSpec + 'yes', + // set up TravisCI + 'yes' + ] + ); + + $this->assertEquals( + self::getStructure(), + vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure() + ); + } + + protected function getCommandFqcn(): string + { + return 'create'; + } + + protected static function getStructure(): array + { + return [ + 'root' => [ + 'company' => [ + 'my-package' => [ + 'spec' => [], + 'src' => [ + 'Example.php' => file_get_contents(__DIR__ . '/stubs/company/my-package/src/Example.php'), + ], + 'tests' => [ + 'ExampleTest.php' => file_get_contents(__DIR__ . '/stubs/company/my-package/tests/ExampleTest.php'), + ], + '.gitignore' => file_get_contents(__DIR__ . '/stubs/company/my-package/.gitignore'), + '.travis.yml' => file_get_contents(__DIR__ . '/stubs/company/my-package/.travis.yml'), + 'composer.json' => file_get_contents(__DIR__ . '/stubs/company/my-package/composer.json'), + 'phpspec.yml' => file_get_contents(__DIR__ . '/stubs/company/my-package/phpspec.yml'), + 'phpunit.xml' => file_get_contents(__DIR__ . '/stubs/company/my-package/phpunit.xml'), + ], + ], + ], + ]; + } +} \ No newline at end of file diff --git a/tests/Console/stubs/company/my-package/.gitignore b/tests/Console/stubs/company/my-package/.gitignore new file mode 100644 index 0000000..7a8a0ad --- /dev/null +++ b/tests/Console/stubs/company/my-package/.gitignore @@ -0,0 +1,3 @@ +composer.phar +composer.lock +vendor diff --git a/tests/Console/stubs/company/my-package/.travis.yml b/tests/Console/stubs/company/my-package/.travis.yml new file mode 100644 index 0000000..ac80f2d --- /dev/null +++ b/tests/Console/stubs/company/my-package/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - 5.5 + - 5.6 + - 7.0 + - hhvm + +before_script: + - travis_retry composer self-update + - travis_retry composer install --prefer-source --no-interaction --dev + +script: phpunit diff --git a/tests/Console/stubs/company/my-package/composer.json b/tests/Console/stubs/company/my-package/composer.json new file mode 100644 index 0000000..1498c2c --- /dev/null +++ b/tests/Console/stubs/company/my-package/composer.json @@ -0,0 +1,17 @@ +{ + "name": "company/my-package", + "autoload": { + "psr-4": { + "Company\\MyPackage\\": "src/" + } + }, + "require-dev": { + "phpunit/phpunit": "^9.6 || ^10", + "phpspec/phpspec": "^7.4" + }, + "autoload-dev": { + "psr-4": { + "Company\\MyPackage\\Tests\\": "tests/" + } + } +} \ No newline at end of file diff --git a/tests/Console/stubs/company/my-package/phpspec.yml b/tests/Console/stubs/company/my-package/phpspec.yml new file mode 100644 index 0000000..12410d7 --- /dev/null +++ b/tests/Console/stubs/company/my-package/phpspec.yml @@ -0,0 +1,4 @@ +suites: + library_suite: + namespace: Company\MyPackage\Tests + psr4_prefix: Company\MyPackage\Tests diff --git a/tests/Console/stubs/company/my-package/phpunit.xml b/tests/Console/stubs/company/my-package/phpunit.xml new file mode 100644 index 0000000..b423a7e --- /dev/null +++ b/tests/Console/stubs/company/my-package/phpunit.xml @@ -0,0 +1,19 @@ + + + + + ./tests/ + + + diff --git a/tests/Console/stubs/company/my-package/src/Example.php b/tests/Console/stubs/company/my-package/src/Example.php new file mode 100644 index 0000000..6204aeb --- /dev/null +++ b/tests/Console/stubs/company/my-package/src/Example.php @@ -0,0 +1,8 @@ +