[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.
This commit is contained in:
Markus Podar
2020-01-01 22:51:21 +01:00
committed by Barry vd. Heuvel
parent 0d1dd2182b
commit fb0c576806
6 changed files with 135 additions and 15 deletions
+1
View File
@@ -2,3 +2,4 @@ build
vendor vendor
composer.lock composer.lock
.idea .idea
.phpunit.result.cache
+24 -10
View File
@@ -1,25 +1,39 @@
language: php language: php
php:
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
## Cache composer ## Cache composer
cache: cache:
directories: directories:
- $HOME/.composer/cache - $HOME/.composer/cache
env:
global:
- RUN_PHPUNIT=1
- RUN_PHPCS=0
matrix: matrix:
include: include:
- php: 7.0 - 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: 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 - travis_wait 20 travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
script: script:
- vendor/bin/phpcs --standard=psr2 src/ - if [[ $RUN_PHPCS = 1 ]]; then vendor/bin/phpcs --standard=psr2 src/; fi
- vendor/bin/phpunit - if [[ $RUN_PHPUNIT = 1 ]]; then vendor/bin/phpunit; fi
+3 -3
View File
@@ -22,9 +22,9 @@
"illuminate/config": "^5.5|^6", "illuminate/config": "^5.5|^6",
"illuminate/view": "^5.5|^6", "illuminate/view": "^5.5|^6",
"phpro/grumphp": "^0.14", "phpro/grumphp": "^0.14",
"phpunit/phpunit" : "4.*",
"scrutinizer/ocular": "~1.1", "scrutinizer/ocular": "~1.1",
"squizlabs/php_codesniffer": "^3" "squizlabs/php_codesniffer": "^3",
"orchestra/testbench": "^4.4"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@@ -33,7 +33,7 @@
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"Barryvdh\\LaravelIdeHelper\\": "tests" "Barryvdh\\LaravelIdeHelper\\Tests\\": "tests"
} }
}, },
"scripts": { "scripts": {
+66
View File
@@ -0,0 +1,66 @@
<?php
namespace Barryvdh\LaravelIdeHelper\Tests\Console;
use Barryvdh\LaravelIdeHelper\Console\EloquentCommand;
use Barryvdh\LaravelIdeHelper\Tests\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Mockery;
use ReflectionClass;
class EloquentCommandTest extends TestCase
{
public function testCommand()
{
$modelFilename = $this->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();
}
}
+5 -2
View File
@@ -1,8 +1,11 @@
<?php <?php
namespace Barryvdh\LaravelIdeHelper; namespace Barryvdh\LaravelIdeHelper\Tests;
class ExampleTest extends \PHPUnit_Framework_TestCase use Barryvdh\LaravelIdeHelper\Method;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{ {
/** /**
* Test that we can actually instantiate the class * Test that we can actually instantiate the class
+36
View File
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests;
use Illuminate\Console\Command;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Symfony\Component\Console\Tester\CommandTester;
abstract class TestCase extends BaseTestCase
{
/**
* The `CommandTester` is directly returned, use methods like
* `->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;
}
}