From fb0c576806506730a2fa6ff1f1ad1b97325a2d0f Mon Sep 17 00:00:00 2001 From: Markus Podar Date: Wed, 1 Jan 2020 22:51:21 +0100 Subject: [PATCH] [PROPOSAL] Add more tests to the library (#865) * phpunit: use dedicate `Tests` namespace for tests * tests: replace phpunit with orchestra/testbench * phpunit: ignore result cache file * phpunit: adapt test for newer version * tests: add base testcase class with a helper to run/assert commands * tests: add test for ide-helper:eloquent * travis: add global default variables * travis: disable xdebug, speeds up running tests * travis: convert php versions to build matrix for environment vars * travis: only run phpcs/phpunit when the env vars signal it However phpcs only runs with one test combination from the matrix, not necessary to execute it every time. --- .gitignore | 1 + .travis.yml | 34 ++++++++++---- composer.json | 6 +-- tests/Console/EloquentCommandTest.php | 66 +++++++++++++++++++++++++++ tests/MethodTest.php | 7 ++- tests/TestCase.php | 36 +++++++++++++++ 6 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 tests/Console/EloquentCommandTest.php create mode 100644 tests/TestCase.php diff --git a/.gitignore b/.gitignore index 4d164ac..6ffbb8e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build vendor composer.lock .idea +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml index 197443b..08b2081 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,25 +1,39 @@ language: php -php: - - 7.0 - - 7.1 - - 7.2 - - 7.3 - - 7.4 - ## Cache composer cache: directories: - $HOME/.composer/cache +env: + global: + - RUN_PHPUNIT=1 + - RUN_PHPCS=0 + matrix: include: - php: 7.0 - env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"' + env: RUN_PHPUNIT=0 + - php: 7.0 + env: RUN_PHPUNIT=0 COMPOSER_FLAGS="--prefer-stable --prefer-lowest" + - php: 7.1 + env: RUN_PHPUNIT=0 + - php: 7.2 + - php: 7.3 + - php: 7.4 + env: RUN_PHPCS=1 before_script: + - phpenv config-rm xdebug.ini || true + +install: + - | + if [[ $RUN_PHPUNIT = 0 ]]; then + # We assume the older PHP/Laravel versions and thus restore a working dependency system for them + composer remove --dev orchestra/testbench --no-interaction --no-update + fi - travis_wait 20 travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist script: - - vendor/bin/phpcs --standard=psr2 src/ - - vendor/bin/phpunit + - if [[ $RUN_PHPCS = 1 ]]; then vendor/bin/phpcs --standard=psr2 src/; fi + - if [[ $RUN_PHPUNIT = 1 ]]; then vendor/bin/phpunit; fi diff --git a/composer.json b/composer.json index dbae450..7c44add 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,9 @@ "illuminate/config": "^5.5|^6", "illuminate/view": "^5.5|^6", "phpro/grumphp": "^0.14", - "phpunit/phpunit" : "4.*", "scrutinizer/ocular": "~1.1", - "squizlabs/php_codesniffer": "^3" + "squizlabs/php_codesniffer": "^3", + "orchestra/testbench": "^4.4" }, "autoload": { "psr-4": { @@ -33,7 +33,7 @@ }, "autoload-dev": { "psr-4": { - "Barryvdh\\LaravelIdeHelper\\": "tests" + "Barryvdh\\LaravelIdeHelper\\Tests\\": "tests" } }, "scripts": { diff --git a/tests/Console/EloquentCommandTest.php b/tests/Console/EloquentCommandTest.php new file mode 100644 index 0000000..1ca3ef6 --- /dev/null +++ b/tests/Console/EloquentCommandTest.php @@ -0,0 +1,66 @@ +getVendorModelFilename(); + + // Ensure the mixins are not present + $modelSource = file_get_contents($modelFilename); + $this->assertStringNotContainsString('* @mixin \\Eloquent', $modelSource); + $this->assertStringNotContainsString('* @mixin \\Illuminate\\Database\\Eloquent\\Builder', $modelSource); + $this->assertStringNotContainsString('* @mixin \\Illuminate\\Database\\Query\\Builder', $modelSource); + + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + // We don't care about actual args (filename) + ->andReturn('abstract class Model implements'); // This is enough to trigger the replacement logic + $mockFilesystem + ->shouldReceive('put') + ->with( + Mockery::any(), // First arg is path, we don't care + Mockery::capture($actualContent) + ) + ->andReturn(1) // Simulate we wrote _something_ to the file + ->once(); + + $this->instance(Filesystem::class, $mockFilesystem); + $command = $this->app->make(EloquentCommand::class); + + $tester = $this->runCommand($command); + + $expectedContent = '/** + * + * + * @mixin \Eloquent + * @mixin \Illuminate\Database\Eloquent\Builder + * @mixin \Illuminate\Database\Query\Builder + */ +abstract class Model implements'; + $this->assertSame($expectedContent, $actualContent); + + $display = $tester->getDisplay(); + $this->assertRegExp(';Unexpected no document on Illuminate\\\Database\\\Eloquent\\\Model;', $display); + $this->assertRegExp(';Wrote expected docblock to .*/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php;', $display); + } + + private function getVendorModelFilename(): string + { + $class = Model::class; + $reflectedClass = new ReflectionClass($class); + + return $reflectedClass->getFileName(); + } +} diff --git a/tests/MethodTest.php b/tests/MethodTest.php index b290ba4..f077ffb 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -1,8 +1,11 @@ getDisplay()` or `->getStatusCode()` on it. + * + * @param Command $command + * @param array $arguments The command line arguments, array of key=>value + * Examples: + * - named arguments: ['model' => 'Post'] + * - boolean flags: ['--all' => true] + * - arguments with values: ['--arg' => 'value'] + * @param array $interactiveInput Interactive responses to the command + * I.e. anything the command `->ask()` or `->confirm()`, etc. + * @return CommandTester + */ + protected function runCommand(Command $command, array $arguments = [], array $interactiveInput = []): CommandTester + { + $command->setLaravel($this->app); + + $tester = new CommandTester($command); + $tester->setInputs($interactiveInput); + + $tester->execute($arguments); + + return $tester; + } +}