Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ jobs:
matrix:
os: [ubuntu-latest]
php: [7.3, 7.4, 8.0]
laravel: [7.*, 8.*]
laravel: [8.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- os: macos-latest
php: 7.4
laravel: 7.*
laravel: 8.*
dependency-version: prefer-stable

- os: windows-latest
php: 7.4
laravel: 7.*
laravel: 8.*
dependency-version: prefer-stable

name: P${{ matrix.php }} - ${{ matrix.os }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
Expand All @@ -51,7 +51,7 @@ jobs:
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/pest
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"php": "^7.3|^8.0",
"jasonmccreary/laravel-test-assertions": "^1.0",
"laravel-shift/blueprint": ">=1.23.0"
"laravel-shift/blueprint": "^2.0"
},
"require-dev": {
"mockery/mockery": "^1.4",
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/MakeHttpTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function execute($files, Tree $tree): Action
{
$stub = $this->stubFileContent('test.stub');

$builder = new HttpTestBuilder($files, $tree);
$builder = new HttpTestBuilder($tree);

foreach ($tree->controllers() as $controller) {
$path = $this->getPath($controller);
Expand Down
7 changes: 0 additions & 7 deletions src/Builders/Concerns/BuildsFactoryStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ private function classFactory(string $variable, string $model, int $count): stri

return sprintf('$%s = %s::factory()%s->create();', $variable, $model, $times);
}

private function legacyFactory(string $variable, string $model, int $count): string
{
$times = $count > 1 ? sprintf(', %s', $count) : '';

return sprintf('$%s = factory(%s::class%s)->create();', $variable, $model, $times);
}
}
13 changes: 0 additions & 13 deletions src/Builders/Concerns/DeterminesLaravelVersion.php

This file was deleted.

7 changes: 1 addition & 6 deletions src/Builders/HttpTestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class HttpTestBuilder
use ReadsStubFiles;
use PopulatesTestStub;

/** @var \Illuminate\Contracts\Filesystem\Filesystem|\Illuminate\Filesystem\Filesystem */
private $files;

/** @var array */
private $imports = [];

Expand All @@ -63,10 +60,8 @@ class HttpTestBuilder
/** @var Tree */
private $tree;

/** @param \Illuminate\Contracts\Filesystem\Filesystem|\Illuminate\Filesystem\Filesystem $files */
public function __construct($files, Tree $tree)
public function __construct(Tree $tree)
{
$this->files = $files;
$this->tree = $tree;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Builders/PendingOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use Blueprint\Models\Controller;
use Fidum\BlueprintPestAddon\Builders\Concerns\BuildsFactoryStatements;
use Fidum\BlueprintPestAddon\Builders\Concerns\DeterminesLaravelVersion;
use Illuminate\Support\Str;

class PendingOutput
{
use BuildsFactoryStatements;
use DeterminesLaravelVersion;

/** @var array[] */
protected $assertions = [
Expand Down Expand Up @@ -62,9 +60,7 @@ public function addAssertions(string $type, array $assertions): self

public function addFactory(string $variable, string $model, int $count = 1): self
{
$statement = static::isLaravel8OrHigher()
? $this->classFactory($variable, $model, $count)
: $this->legacyFactory($variable, $model, $count);
$statement = $this->classFactory($variable, $model, $count);

return $this->addSetUp('data', $statement);
}
Expand Down
6 changes: 0 additions & 6 deletions src/Builders/Statements/StatementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Blueprint\Tree;
use Fidum\BlueprintPestAddon\Builders\PendingOutput;
use Fidum\BlueprintPestAddon\Contracts\StatementBuilder as StatementBuilderContract;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;

abstract class StatementBuilder implements StatementBuilderContract
Expand Down Expand Up @@ -47,9 +46,4 @@ public function __construct(
$this->context = PendingOutput::context($this->controller);
$this->variable = Str::camel($this->context);
}

protected static function isLaravel8OrHigher()
{
return version_compare(App::version(), '8.0.0', '>=');
}
}
14 changes: 5 additions & 9 deletions tests/Feature/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use Blueprint\Blueprint;
use Blueprint\Lexers\ControllerLexer;
use Blueprint\Lexers\ModelLexer;
use Blueprint\Lexers\StatementLexer;
use Blueprint\Models\Controller;
use Blueprint\Tree;
use Fidum\BlueprintPestAddon\PestGenerator;
use Fidum\BlueprintPestAddon\Tests\TestCase;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Mockery\MockInterface;

class FeatureTestCase extends TestCase
Expand Down Expand Up @@ -40,7 +40,7 @@ protected function setUp(): void

$this->blueprint = new Blueprint();
$this->blueprint->registerLexer(new ModelLexer());
$this->blueprint->registerLexer(new ControllerLexer(new \Blueprint\Lexers\StatementLexer()));
$this->blueprint->registerLexer(new ControllerLexer(new StatementLexer()));
$this->blueprint->registerGenerator($this->subject);
}

Expand Down Expand Up @@ -76,7 +76,7 @@ protected function getExampleTestsOutput(bool $featureExists, bool $unitExists):
return $output;
}

protected function getHttpTestsOutput(Tree $tree, string $controllerPath, string $fixturePath = null): array
protected function getHttpTestsOutput(Tree $tree, string $controllerPath): array
{
$controllers = $tree->controllers();

Expand All @@ -100,14 +100,10 @@ protected function getHttpTestsOutput(Tree $tree, string $controllerPath, string
$ns = str_replace('\\', '/', Blueprint::relativeNamespace($controller->fullyQualifiedClassName()));
$controllerPath = 'tests/Feature/'.$ns.'Test.php';

$fixtureControllerPath = $fixturePath
? Str::finish($fixturePath, '/').$controller->className().'Test.php'
: $controllerPath;

$this->files->expects('put')
->withArgs(function ($pathArg, $output) use ($controllerPath, $fixtureControllerPath) {
->withArgs(function ($pathArg, $output) use ($controllerPath) {
$this->assertSame($controllerPath, $pathArg);
$this->assertSame($this->fixture($fixtureControllerPath), $output);
$this->assertSame($this->fixture($controllerPath), $output);

return true;
});
Expand Down
69 changes: 30 additions & 39 deletions tests/Feature/PestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,33 @@
expect($files)->toBeArray()->toBeEmpty();
});

it('generates tests using class factories', pestGeneratorTest('8.0.0'))->with('pest');

it('generates tests using legacy factories', pestGeneratorTest('7.0.0', true))->with('pest');

function pestGeneratorTest($version, bool $legacy = false): callable
{
return function (
string $definition,
bool $pestGlobalFileExists,
int $createdCount = 0,
int $updatedCount = 0,
array $config = [],
bool $exampleFeature = false,
bool $exampleUnit = false,
string $folder = ''
) use ($version, $legacy) {
/** @var FeatureTestCase $this */
$this->useLaravelVersion($version);

$defaultConfig = $this->app['config']->get('blueprint');
$this->app['config']->set('blueprint', array_merge($defaultConfig, $config));

$tokens = $this->blueprint->parse($this->definition($definition));
$tree = $this->blueprint->analyze($tokens);

$exampleFileOutput = $this->getExampleTestsOutput($exampleFeature, $exampleUnit);
$pestGlobalFileOutput = $this->getPestGlobalFileOutput($pestGlobalFileExists);
$httpTestsOutput = $this->getHttpTestsOutput($tree,
'tests/Feature/Http/Controllers'.($folder ? '/'.$folder : ''),
$legacy ? 'tests/Feature/Http/Legacy/'.$folder : null,
);

$expectedOutput = array_merge_recursive($pestGlobalFileOutput, $exampleFileOutput, $httpTestsOutput);

expect($expectedOutput['created'] ?? [])->toHaveCount($createdCount);
expect($expectedOutput['updated'] ?? [])->toHaveCount($updatedCount);
expect($this->subject->output($tree))->toBe($expectedOutput);
};
}
it('generates tests using class factories', function (
string $definition,
bool $pestGlobalFileExists,
int $createdCount = 0,
int $updatedCount = 0,
array $config = [],
bool $exampleFeature = false,
bool $exampleUnit = false,
string $folder = ''
) {
/** @var FeatureTestCase $this */
$defaultConfig = $this->app['config']->get('blueprint');
$this->app['config']->set('blueprint', array_merge($defaultConfig, $config));

$tokens = $this->blueprint->parse($this->definition($definition));
$tree = $this->blueprint->analyze($tokens);

$exampleFileOutput = $this->getExampleTestsOutput($exampleFeature, $exampleUnit);
$pestGlobalFileOutput = $this->getPestGlobalFileOutput($pestGlobalFileExists);
$httpTestsOutput = $this->getHttpTestsOutput(
$tree,
'tests/Feature/Http/Controllers'.($folder ? '/'.$folder : ''),
);

$expectedOutput = array_merge_recursive($pestGlobalFileOutput, $exampleFileOutput, $httpTestsOutput);

expect($expectedOutput['created'] ?? [])->toHaveCount($createdCount);
expect($expectedOutput['updated'] ?? [])->toHaveCount($updatedCount);
expect($this->subject->output($tree))->toBe($expectedOutput);
})->with('pest');
11 changes: 0 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Blueprint\BlueprintServiceProvider;
use Fidum\BlueprintPestAddon\BlueprintPestAddonServiceProvider;
use Illuminate\Support\Facades\App;
use Orchestra\Testbench\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
Expand All @@ -24,14 +23,4 @@ protected function getPackageProviders($app): array
BlueprintPestAddonServiceProvider::class,
];
}

protected function useLaravelVersion(string $version)
{
$appMock = \Mockery::mock($this->app);
$appMock->shouldReceive('version')
->withNoArgs()
->andReturn($version);

App::swap($appMock);
}
}

This file was deleted.

Loading