Skip to content

Commit c7b87bc

Browse files
committed
add tests for formrequest generator
1 parent 53be314 commit c7b87bc

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

system/Commands/Generators/Views/formrequest.tpl.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function rules(): array
3030

3131
// /**
3232
// * Determines if the current user is authorized to make this request.
33+
// *
34+
// * Defaults to true in FormRequest. Override only when authorization
35+
// * depends on application logic.
3336
// */
3437
// public function isAuthorized(): bool
3538
// {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Commands\Generators;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\StreamFilterTrait;
18+
use PHPUnit\Framework\Attributes\Group;
19+
20+
/**
21+
* @internal
22+
*/
23+
#[Group('Others')]
24+
final class FormRequestGeneratorTest extends CIUnitTestCase
25+
{
26+
use StreamFilterTrait;
27+
28+
protected function tearDown(): void
29+
{
30+
parent::tearDown();
31+
32+
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
33+
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
34+
$dir = dirname($file);
35+
36+
if (is_file($file)) {
37+
unlink($file);
38+
}
39+
}
40+
41+
public function testGenerateFormRequest(): void
42+
{
43+
command('make:request user');
44+
45+
$file = APPPATH . 'Requests/User.php';
46+
47+
$this->assertFileExists($file);
48+
$this->assertStringContainsString(
49+
'Defaults to true in FormRequest. Override only when authorization',
50+
file_get_contents($file),
51+
);
52+
}
53+
54+
public function testGenerateFormRequestWithOptionSuffix(): void
55+
{
56+
command('make:request admin -suffix');
57+
$this->assertFileExists(APPPATH . 'Requests/AdminRequest.php');
58+
}
59+
}

0 commit comments

Comments
 (0)