Skip to content

Commit 33a206e

Browse files
[10.x] Test Improvements (#48378)
* [10.x] Test Improvements Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 5f8d1a2 commit 33a206e

24 files changed

+1178
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"league/flysystem-read-only": "^3.3",
105105
"league/flysystem-sftp-v3": "^3.0",
106106
"mockery/mockery": "^1.5.1",
107-
"orchestra/testbench-core": "^8.4",
107+
"orchestra/testbench-core": "^8.10",
108108
"pda/pheanstalk": "^4.0",
109109
"phpstan/phpstan": "^1.4.7",
110110
"phpunit/phpunit": "^10.0.7",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Generators;
4+
5+
class CastMakeCommandTest extends TestCase
6+
{
7+
protected $files = [
8+
'app/Casts/Foo.php',
9+
];
10+
11+
public function testItCanGenerateCastFile()
12+
{
13+
$this->artisan('make:cast', ['name' => 'Foo'])
14+
->assertExitCode(0);
15+
16+
$this->assertFileContains([
17+
'namespace App\Casts;',
18+
'use Illuminate\Contracts\Database\Eloquent\CastsAttributes;',
19+
'class Foo implements CastsAttributes',
20+
'public function get(Model $model, string $key, mixed $value, array $attributes): mixed',
21+
'public function set(Model $model, string $key, mixed $value, array $attributes): mixed',
22+
], 'app/Casts/Foo.php');
23+
}
24+
25+
public function testItCanGenerateInboundCastFile()
26+
{
27+
$this->artisan('make:cast', ['name' => 'Foo', '--inbound' => true])
28+
->assertExitCode(0);
29+
30+
$this->assertFileContains([
31+
'namespace App\Casts;',
32+
'use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;',
33+
'class Foo implements CastsInboundAttributes',
34+
'public function set(Model $model, string $key, mixed $value, array $attributes): mixed',
35+
], 'app/Casts/Foo.php');
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Generators;
4+
5+
class ChannelMakeCommandTest extends TestCase
6+
{
7+
protected $files = [
8+
'app/Broadcasting/FooChannel.php',
9+
];
10+
11+
public function testItCanGenerateChannelFile()
12+
{
13+
$this->artisan('make:channel', ['name' => 'FooChannel'])
14+
->assertExitCode(0);
15+
16+
$this->assertFileContains([
17+
'namespace App\Broadcasting;',
18+
'use Illuminate\Foundation\Auth\User;',
19+
'class FooChannel',
20+
], 'app/Broadcasting/FooChannel.php');
21+
}
22+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Generators;
4+
5+
class ComponentMakeCommandTest extends TestCase
6+
{
7+
protected $files = [
8+
'app/View/Components/Foo.php',
9+
'resources/views/components/foo.blade.php',
10+
'tests/Feature/View/Components/FooTest.php',
11+
];
12+
13+
public function testItCanGenerateComponentFile()
14+
{
15+
$this->artisan('make:component', ['name' => 'Foo'])
16+
->assertExitCode(0);
17+
18+
$this->assertFileContains([
19+
'namespace App\View\Components;',
20+
'use Illuminate\View\Component;',
21+
'class Foo extends Component',
22+
"return view('components.foo');",
23+
], 'app/View/Components/Foo.php');
24+
25+
$this->assertFilenameExists('resources/views/components/foo.blade.php');
26+
$this->assertFilenameNotExists('tests/Feature/View/Components/FooTest.php');
27+
}
28+
29+
public function testItCanGenerateInlineComponentFile()
30+
{
31+
$this->artisan('make:component', ['name' => 'Foo', '--inline' => true])
32+
->assertExitCode(0);
33+
34+
$this->assertFileContains([
35+
'namespace App\View\Components;',
36+
'use Illuminate\View\Component;',
37+
'class Foo extends Component',
38+
"return <<<'blade'",
39+
], 'app/View/Components/Foo.php');
40+
41+
$this->assertFilenameNotExists('resources/views/components/foo.blade.php');
42+
}
43+
44+
public function testItCanGenerateComponentFileWithTest()
45+
{
46+
$this->artisan('make:component', ['name' => 'Foo', '--test' => true])
47+
->assertExitCode(0);
48+
49+
$this->assertFilenameExists('app/View/Components/Foo.php');
50+
$this->assertFilenameExists('resources/views/components/foo.blade.php');
51+
$this->assertFilenameExists('tests/Feature/View/Components/FooTest.php');
52+
}
53+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Generators;
4+
5+
class ControllerMakeCommandTest extends TestCase
6+
{
7+
protected $files = [
8+
'app/Http/Controllers/FooController.php',
9+
'app/Models/Bar.php',
10+
'app/Models/Foo.php',
11+
'tests/Feature/Http/Controllers/FooControllerTest.php',
12+
];
13+
14+
public function testItCanGenerateControllerFile()
15+
{
16+
$this->artisan('make:controller', ['name' => 'FooController'])
17+
->assertExitCode(0);
18+
19+
$this->assertFileContains([
20+
'namespace App\Http\Controllers;',
21+
'use Illuminate\Http\Request;',
22+
'class FooController extends Controller',
23+
], 'app/Http/Controllers/FooController.php');
24+
25+
$this->assertFileNotContains([
26+
'public function __invoke(Request $request)',
27+
], 'app/Http/Controllers/FooController.php');
28+
29+
$this->assertFilenameNotExists('tests/Feature/Http/Controllers/FooControllerTest.php');
30+
}
31+
32+
public function testItCanGenerateControllerFileWithInvokableTypeOption()
33+
{
34+
$this->artisan('make:controller', ['name' => 'FooController', '--type' => 'invokable'])
35+
->assertExitCode(0);
36+
37+
$this->assertFileContains([
38+
'namespace App\Http\Controllers;',
39+
'use Illuminate\Http\Request;',
40+
'class FooController extends Controller',
41+
'public function __invoke(Request $request)',
42+
], 'app/Http/Controllers/FooController.php');
43+
}
44+
45+
public function testItCanGenerateControllerFileWithInvokableOption()
46+
{
47+
$this->artisan('make:controller', ['name' => 'FooController', '--invokable' => true])
48+
->assertExitCode(0);
49+
50+
$this->assertFileContains([
51+
'namespace App\Http\Controllers;',
52+
'use Illuminate\Http\Request;',
53+
'class FooController extends Controller',
54+
'public function __invoke(Request $request)',
55+
], 'app/Http/Controllers/FooController.php');
56+
}
57+
58+
public function testItCanGenerateControllerFileWithModelOption()
59+
{
60+
$this->artisan('make:controller', ['name' => 'FooController', '--model' => 'Foo'])
61+
->expectsQuestion('A App\Models\Foo model does not exist. Do you want to generate it?', false)
62+
->assertExitCode(0);
63+
64+
$this->assertFileContains([
65+
'namespace App\Http\Controllers;',
66+
'use App\Models\Foo;',
67+
'public function index()',
68+
'public function create()',
69+
'public function store(Request $request)',
70+
'public function show(Foo $foo)',
71+
'public function edit(Foo $foo)',
72+
'public function update(Request $request, Foo $foo)',
73+
'public function destroy(Foo $foo)',
74+
], 'app/Http/Controllers/FooController.php');
75+
}
76+
77+
public function testItCanGenerateControllerFileWithModelAndParentOption()
78+
{
79+
$this->artisan('make:controller', ['name' => 'FooController', '--model' => 'Bar', '--parent' => 'Foo'])
80+
->expectsQuestion('A App\Models\Foo model does not exist. Do you want to generate it?', false)
81+
->expectsQuestion('A App\Models\Bar model does not exist. Do you want to generate it?', false)
82+
->assertExitCode(0);
83+
84+
$this->assertFileContains([
85+
'namespace App\Http\Controllers;',
86+
'use App\Models\Bar;',
87+
'use App\Models\Foo;',
88+
'public function index(Foo $foo)',
89+
'public function create(Foo $foo)',
90+
'public function store(Request $request, Foo $foo)',
91+
'public function show(Foo $foo, Bar $bar)',
92+
'public function edit(Foo $foo, Bar $bar)',
93+
'public function update(Request $request, Foo $foo, Bar $bar)',
94+
'public function destroy(Foo $foo, Bar $bar)',
95+
], 'app/Http/Controllers/FooController.php');
96+
}
97+
98+
public function testItCanGenerateControllerFileWithApiOption()
99+
{
100+
$this->artisan('make:controller', ['name' => 'FooController', '--api' => true])
101+
->assertExitCode(0);
102+
103+
$this->assertFileContains([
104+
'namespace App\Http\Controllers;',
105+
'use Illuminate\Http\Request;',
106+
'class FooController extends Controller',
107+
'public function index()',
108+
'public function store(Request $request)',
109+
'public function update(Request $request, string $id)',
110+
'public function destroy(string $id)',
111+
], 'app/Http/Controllers/FooController.php');
112+
113+
$this->assertFileNotContains([
114+
'public function create()',
115+
'public function edit($id)',
116+
], 'app/Http/Controllers/FooController.php');
117+
}
118+
119+
public function testItCanGenerateControllerFileWithInvokableIgnoresApiOption()
120+
{
121+
$this->artisan('make:controller', ['name' => 'FooController', '--api' => true, '--invokable' => true])
122+
->assertExitCode(0);
123+
124+
$this->assertFileContains([
125+
'namespace App\Http\Controllers;',
126+
'use Illuminate\Http\Request;',
127+
'class FooController extends Controller',
128+
'public function __invoke(Request $request)',
129+
], 'app/Http/Controllers/FooController.php');
130+
131+
$this->assertFileNotContains([
132+
'public function index()',
133+
'public function store(Request $request)',
134+
'public function update(Request $request, $id)',
135+
'public function destroy($id)',
136+
], 'app/Http/Controllers/FooController.php');
137+
}
138+
139+
public function testItCanGenerateControllerFileWithApiAndModelOption()
140+
{
141+
$this->artisan('make:controller', ['name' => 'FooController', '--model' => 'Foo', '--api' => true])
142+
->expectsQuestion('A App\Models\Foo model does not exist. Do you want to generate it?', false)
143+
->assertExitCode(0);
144+
145+
$this->assertFileContains([
146+
'namespace App\Http\Controllers;',
147+
'use App\Models\Foo;',
148+
'public function index()',
149+
'public function store(Request $request)',
150+
'public function show(Foo $foo)',
151+
'public function update(Request $request, Foo $foo)',
152+
'public function destroy(Foo $foo)',
153+
], 'app/Http/Controllers/FooController.php');
154+
155+
$this->assertFileNotContains([
156+
'public function create()',
157+
'public function edit(Foo $foo)',
158+
], 'app/Http/Controllers/FooController.php');
159+
}
160+
161+
public function testItCanGenerateControllerFileWithTest()
162+
{
163+
$this->artisan('make:controller', ['name' => 'FooController', '--test' => true])
164+
->assertExitCode(0);
165+
166+
$this->assertFilenameExists('app/Http/Controllers/FooController.php');
167+
$this->assertFilenameExists('tests/Feature/Http/Controllers/FooControllerTest.php');
168+
}
169+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Generators;
4+
5+
class EventMakeCommandTest extends TestCase
6+
{
7+
protected $files = [
8+
'app/Events/FooCreated.php',
9+
];
10+
11+
public function testItCanGenerateEventFile()
12+
{
13+
$this->artisan('make:event', ['name' => 'FooCreated'])
14+
->assertExitCode(0);
15+
16+
$this->assertFileContains([
17+
'namespace App\Events;',
18+
'class FooCreated',
19+
], 'app/Events/FooCreated.php');
20+
}
21+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Generators;
4+
5+
class ExceptionMakeCommandTest extends TestCase
6+
{
7+
protected $files = [
8+
'app/Exceptions/FooException.php',
9+
];
10+
11+
public function testItCanGenerateExceptionFile()
12+
{
13+
$this->artisan('make:exception', ['name' => 'FooException'])
14+
->assertExitCode(0);
15+
16+
$this->assertFileContains([
17+
'namespace App\Exceptions;',
18+
'use Exception;',
19+
'class FooException extends Exception',
20+
], 'app/Exceptions/FooException.php');
21+
22+
$this->assertFileNotContains([
23+
'public function report()',
24+
'public function render($request)',
25+
], 'app/Exceptions/FooException.php');
26+
}
27+
28+
public function testItCanGenerateExceptionFileWithReportOption()
29+
{
30+
$this->artisan('make:exception', ['name' => 'FooException', '--report' => true])
31+
->assertExitCode(0);
32+
33+
$this->assertFileContains([
34+
'namespace App\Exceptions;',
35+
'use Exception;',
36+
'class FooException extends Exception',
37+
'public function report()',
38+
], 'app/Exceptions/FooException.php');
39+
40+
$this->assertFileNotContains([
41+
'public function render($request)',
42+
], 'app/Exceptions/FooException.php');
43+
}
44+
45+
public function testItCanGenerateExceptionFileWithRenderOption()
46+
{
47+
$this->artisan('make:exception', ['name' => 'FooException', '--render' => true])
48+
->assertExitCode(0);
49+
50+
$this->assertFileContains([
51+
'namespace App\Exceptions;',
52+
'use Exception;',
53+
'class FooException extends Exception',
54+
'public function render(Request $request): Response',
55+
], 'app/Exceptions/FooException.php');
56+
57+
$this->assertFileNotContains([
58+
'public function report()',
59+
], 'app/Exceptions/FooException.php');
60+
}
61+
62+
public function testItCanGenerateExceptionFileWithReportAndRenderOption()
63+
{
64+
$this->artisan('make:exception', ['name' => 'FooException', '--report' => true, '--render' => true])
65+
->assertExitCode(0);
66+
67+
$this->assertFileContains([
68+
'namespace App\Exceptions;',
69+
'use Exception;',
70+
'class FooException extends Exception',
71+
'public function render(Request $request): Response',
72+
'public function report()',
73+
], 'app/Exceptions/FooException.php');
74+
}
75+
}

0 commit comments

Comments
 (0)