Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROPOSAL] Add more tests to the library #865

Merged
merged 10 commits into from
Jan 1, 2020
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build
vendor
composer.lock
.idea
.phpunit.result.cache
34 changes: 24 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -33,7 +33,7 @@
},
"autoload-dev": {
"psr-4": {
"Barryvdh\\LaravelIdeHelper\\": "tests"
"Barryvdh\\LaravelIdeHelper\\Tests\\": "tests"
}
},
"scripts": {
Expand Down
66 changes: 66 additions & 0 deletions tests/Console/EloquentCommandTest.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
7 changes: 5 additions & 2 deletions tests/MethodTest.php
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 36 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -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;
}
}