Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit 87d4ce8

Browse files
authored
Fixing missing model import in generated tests for show-only controllers (#37)
1 parent 28d2258 commit 87d4ce8

File tree

41 files changed

+526
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+526
-14
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
/.php.cs export-ignore
1313
/.github export-ignore
1414

15+
# Declare files that will always have LF line endings on checkout.
16+
/tests/__snapshots__/*.php text eol=lf

.styleci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ preset: laravel
22

33
finder:
44
exclude:
5+
- "tests/__snapshots__"
56
- "tests/fixtures"

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
"laravel-shift/blueprint": "^2.3"
2626
},
2727
"require-dev": {
28-
"mockery/mockery": "^1.4.4",
29-
"orchestra/testbench": "^7.0",
30-
"pestphp/pest": "^1.21.1",
31-
"phpunit/phpunit": "^9.3",
32-
"vimeo/psalm": "^4.0"
28+
"mockery/mockery": "^1.5.1",
29+
"orchestra/testbench": "^7.22",
30+
"pestphp/pest": "^1.22.4",
31+
"phpunit/phpunit": "^9.6.3",
32+
"spatie/pest-plugin-snapshots": "^1.1",
33+
"vimeo/psalm": "^4.30"
3334
},
3435
"autoload": {
3536
"psr-4": {

src/Builders/Statements/InitialStatementBuilder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
use Blueprint\Models\Controller;
66
use Blueprint\Models\Statements\SessionStatement;
77
use Blueprint\Tree;
8+
use Fidum\BlueprintPestAddon\Builders\Concerns\DeterminesModels;
89
use Fidum\BlueprintPestAddon\Builders\PendingOutput;
910

1011
class InitialStatementBuilder extends StatementBuilder
1112
{
13+
use DeterminesModels;
14+
1215
public function __construct(
1316
protected Controller $controller,
1417
protected string $methodName,
@@ -23,7 +26,9 @@ public function execute(): PendingOutput
2326
if (in_array($this->methodName, ['edit', 'update', 'show', 'destroy'])) {
2427
$model = $this->controller->prefix();
2528

26-
$this->output->addFactory($this->variable, $model);
29+
$this->output
30+
->addFactory($this->variable, $model)
31+
->addImport($this->fullyQualifiedModelClassName($model));
2732
}
2833

2934
return $this->output;

tests/Datasets/Pest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
'custom queries defined on controller routes' => ['query.yml', false, 2],
1616
'controller test created in specified subfolder' => ['subfolder.yml', false, 2, 0, [], false, false, 'Api'],
1717
'controller test created in specified subfolder with nested models' => ['subfolder_nested.yml', false, 2, 0, ['models_namespace' => 'Models'], false, false, 'Screening'],
18+
'controller with single action' => ['single_action_controller.yml', true, 1, 1],
1819
]);

tests/Feature/FeatureTestCase.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,24 @@ protected function getExampleTestsOutput(bool $featureExists, bool $unitExists):
6161
$output = [];
6262

6363
if ($featureExists) {
64-
$this->files->expects('put')->with($this->exampleFeatureFile, $this->fixture($this->exampleFeatureFile));
64+
$this->files->expects('put')->withArgs(function ($file, $content) {
65+
$this->assertSame($this->exampleFeatureFile, $file);
66+
$this->assertMatchesPHPSnapshot($content);
67+
68+
return true;
69+
});
70+
6571
$output['updated'][] = $this->exampleFeatureFile;
6672
}
6773

6874
if ($unitExists) {
69-
$this->files->expects('put')->with($this->exampleUnitFile, $this->fixture($this->exampleUnitFile));
75+
$this->files->expects('put')->withArgs(function ($file, $content) {
76+
$this->assertSame($this->exampleUnitFile, $file);
77+
$this->assertMatchesPHPSnapshot($content);
78+
79+
return true;
80+
});
81+
7082
$output['updated'][] = $this->exampleUnitFile;
7183
}
7284

@@ -100,7 +112,7 @@ protected function getHttpTestsOutput(Tree $tree, string $controllerPath): array
100112
$this->files->expects('put')
101113
->withArgs(function ($pathArg, $output) use ($controllerPath) {
102114
$this->assertSame($controllerPath, $pathArg);
103-
$this->assertSame($this->fixture($controllerPath), $output);
115+
$this->assertMatchesPHPSnapshot($output);
104116

105117
return true;
106118
});
@@ -114,7 +126,12 @@ protected function getHttpTestsOutput(Tree $tree, string $controllerPath): array
114126
protected function getPestGlobalFileOutput(bool $updated): array
115127
{
116128
$this->files->expects('exists')->with($this->pestGlobalFile)->andReturn($updated);
117-
$this->files->expects('put')->with($this->pestGlobalFile, $this->fixture($this->pestGlobalFile));
129+
$this->files->expects('put')->withArgs(function ($file, $output) {
130+
$this->assertSame($this->pestGlobalFile, $file);
131+
$this->assertMatchesPHPSnapshot($output);
132+
133+
return true;
134+
});
118135

119136
$key = $updated ? 'updated' : 'created';
120137

tests/Feature/PestGeneratorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@
3535
$tokens = $this->blueprint->parse($this->definition($definition));
3636
$tree = $this->blueprint->analyze($tokens);
3737

38-
$exampleFileOutput = $this->getExampleTestsOutput($exampleFeature, $exampleUnit);
3938
$pestGlobalFileOutput = $this->getPestGlobalFileOutput($pestGlobalFileExists);
39+
$exampleFileOutput = $this->getExampleTestsOutput($exampleFeature, $exampleUnit);
4040
$httpTestsOutput = $this->getHttpTestsOutput(
4141
$tree,
4242
'tests/Feature/Http/Controllers'.($folder ? '/'.$folder : ''),
4343
);
4444

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

47-
expect($expectedOutput['created'] ?? [])->toHaveCount($createdCount);
48-
expect($expectedOutput['updated'] ?? [])->toHaveCount($updatedCount);
49-
expect($this->subject->output($tree))->toBe($expectedOutput);
47+
expect($expectedOutput['created'] ?? [])->toHaveCount($createdCount)
48+
->and($expectedOutput['updated'] ?? [])->toHaveCount($updatedCount)
49+
->and($this->subject->output($tree))->toBe($expectedOutput);
5050
})->with('pest');

tests/PHPDriver.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Fidum\BlueprintPestAddon\Tests;
4+
5+
use Spatie\Snapshots\Drivers\TextDriver;
6+
7+
class PHPDriver extends TextDriver
8+
{
9+
public function extension(): string
10+
{
11+
return 'php';
12+
}
13+
}

tests/TestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
use Blueprint\BlueprintServiceProvider;
66
use Fidum\BlueprintPestAddon\BlueprintPestAddonServiceProvider;
77
use Orchestra\Testbench\TestCase as BaseTestCase;
8+
use function Spatie\Snapshots\assertMatchesSnapshot;
89

910
class TestCase extends BaseTestCase
1011
{
12+
public function assertMatchesPHPSnapshot(string $content)
13+
{
14+
assertMatchesSnapshot(str_replace("\r", '', $content), new PHPDriver());
15+
}
16+
1117
protected function getEnvironmentSetUp($app): void
1218
{
1319
// blueprint config
File renamed without changes.

0 commit comments

Comments
 (0)