mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-19 02:23:11 +00:00
[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:
committed by
Barry vd. Heuvel
parent
0d1dd2182b
commit
fb0c576806
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
<?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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user