Skip to content

Commit 318ffac

Browse files
committed
Generate assertions for new respond statement
1 parent 97add59 commit 318ffac

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

src/Generators/TestGenerator.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Blueprint\Models\Statements\QueryStatement;
1313
use Blueprint\Models\Statements\RedirectStatement;
1414
use Blueprint\Models\Statements\RenderStatement;
15+
use Blueprint\Models\Statements\RespondStatement;
1516
use Blueprint\Models\Statements\SendStatement;
1617
use Blueprint\Models\Statements\SessionStatement;
1718
use Blueprint\Models\Statements\ValidateStatement;
@@ -23,6 +24,7 @@ class TestGenerator implements Generator
2324
const TESTS_REDIRECT = 2;
2425
const TESTS_SAVE = 4;
2526
const TESTS_DELETE = 8;
27+
const TESTS_RESPONDS = 16;
2628

2729
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
2830
private $files;
@@ -289,6 +291,20 @@ protected function buildTestCases(Controller $controller)
289291
$assertion .= '));';
290292

291293
array_unshift($assertions['response'], $assertion);
294+
} elseif ($statement instanceof RespondStatement) {
295+
$tested_bits |= self::TESTS_RESPONDS;
296+
297+
if ($statement->content()) {
298+
array_unshift($assertions['response'], '$response->assertJson($' . $statement->content() . ');');
299+
}
300+
301+
if ($statement->status() === 200) {
302+
array_unshift($assertions['response'], '$response->assertOk();');
303+
} elseif ($statement->status() === 204) {
304+
array_unshift($assertions['response'], '$response->assertNoContent();');
305+
} else {
306+
array_unshift($assertions['response'], '$response->assertNoContent(' . $statement->status() . ');');
307+
}
292308
} elseif ($statement instanceof SessionStatement) {
293309
$assertions['response'][] = sprintf('$response->assertSessionHas(\'%s\', %s);', $statement->reference(), '$' . str_replace('.', '->', $statement->reference()));
294310
} elseif ($statement instanceof EloquentStatement) {
@@ -491,6 +507,10 @@ private function buildTestCaseName(string $name, int $tested_bits)
491507
$verifications[] = 'redirects';
492508
}
493509

510+
if ($tested_bits & self::TESTS_RESPONDS) {
511+
$verifications[] = 'responds_with';
512+
}
513+
494514
if (empty($verifications)) {
495515
return $name . '_behaves_as_expected';
496516
}

tests/Feature/Generator/TestGeneratorTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ public function output_generates_test_for_controller_tree($definition, $path, $t
5959
$this->files->expects('get')
6060
->with('stubs/test/case.stub')
6161
->andReturn(file_get_contents('stubs/test/case.stub'));
62+
$dirname = dirname($path);
6263
$this->files->expects('exists')
63-
->with('tests/Feature/Http/Controllers')
64+
->with($dirname)
6465
->andReturnFalse();
6566
$this->files->expects('makeDirectory')
66-
->with('tests/Feature/Http/Controllers', 0755, true);
67+
->with($dirname, 0755, true);
6768
$this->files->expects('put')
6869
->with($path, $this->fixture($test));
6970

@@ -109,6 +110,7 @@ public function controllerTreeDataProvider()
109110
{
110111
return [
111112
['definitions/readme-example.bp', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example.php'],
113+
['definitions/respond-statements.bp', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements.php'],
112114
];
113115
}
114116
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers\Api;
4+
5+
use App\Post;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Tests\TestCase;
8+
9+
/**
10+
* @see \App\Http\Controllers\Api\PostController
11+
*/
12+
class PostControllerTest extends TestCase
13+
{
14+
use RefreshDatabase;
15+
16+
/**
17+
* @test
18+
*/
19+
public function index_responds_with()
20+
{
21+
$posts = factory(Post::class, 3)->create();
22+
23+
$response = $this->get(route('post.index'));
24+
25+
$response->assertOk();
26+
$response->assertJson($posts);
27+
}
28+
29+
30+
/**
31+
* @test
32+
*/
33+
public function store_responds_with()
34+
{
35+
$response = $this->post(route('post.store'));
36+
37+
$response->assertNoContent();
38+
}
39+
40+
41+
/**
42+
* @test
43+
*/
44+
public function error_responds_with()
45+
{
46+
$response = $this->get(route('post.error'));
47+
48+
$response->assertNoContent(400);
49+
}
50+
}

0 commit comments

Comments
 (0)