-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Testing): Add ability to create request without Laravel container
BREAKING CHANGE: createPostRequest has been renamed to createAndValidadeRequest
- Loading branch information
Showing
10 changed files
with
221 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Testing/Laravel/Contracts/Validation/ValidatorMock.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\Laravel\Contracts\Validation; | ||
|
||
use Illuminate\Contracts\Validation\Validator; | ||
use Illuminate\Support\MessageBag; | ||
|
||
class ValidatorMock implements Validator | ||
{ | ||
public function __construct( | ||
private readonly array $validatedData | ||
) { | ||
} | ||
|
||
public function validate() | ||
{ | ||
return $this->validatedData; | ||
} | ||
|
||
public function validated() | ||
{ | ||
return $this->validatedData; | ||
} | ||
|
||
public function fails(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function failed() | ||
{ | ||
return []; | ||
} | ||
|
||
public function sometimes($attribute, $rules, callable $callback) | ||
{ | ||
return $this; | ||
} | ||
|
||
public function after($callback) | ||
{ | ||
return $this; | ||
} | ||
|
||
public function errors() | ||
{ | ||
return new MessageBag(); | ||
} | ||
|
||
public function getMessageBag() | ||
{ | ||
return new MessageBag(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Unit\Testing\Concerns; | ||
|
||
class AutoAction | ||
{ | ||
public function __construct( | ||
public readonly string $test, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Unit\Testing\Concerns; | ||
|
||
use LaraStrict\Testing\Concerns\CreateRequest; | ||
use PHPUnit\Framework\TestCase; | ||
use Tests\LaraStrict\Feature\Testing\Concerns\TestRequest; | ||
|
||
class CreateRequestTest extends TestCase | ||
{ | ||
use CreateRequest; | ||
|
||
public function testNoContainerUsage(): void | ||
{ | ||
$data = [ | ||
TestRequest::KeyTest => 'value', | ||
]; | ||
$request = $this->createFormRequest(requestClass: TestRequest::class, data: $data); | ||
$this->assertEquals($data, $request->validated()); | ||
$this->assertTrue($request->acceptsJson()); | ||
$this->assertEquals('https://testing', $request->url()); | ||
} | ||
|
||
public function testContainerUsage(): void | ||
{ | ||
$data = [ | ||
TestRequest::KeyTest => 'value', | ||
]; | ||
$customAction = new CustomAction(); | ||
$request = $this->createFormRequest( | ||
requestClass: TestContainerRequest::class, | ||
data: $data, | ||
makeBindings: [ | ||
CustomAction::class => $customAction, | ||
] | ||
); | ||
$this->assertEquals($data, $request->validated()); | ||
$this->assertTrue($request->acceptsJson()); | ||
$this->assertEquals('https://testing', $request->url()); | ||
$this->assertNotNull($customAction->autoAction); | ||
$this->assertEquals('test', $customAction->autoAction->test); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Unit\Testing\Concerns; | ||
|
||
class CustomAction | ||
{ | ||
public ?AutoAction $autoAction = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Unit\Testing\Concerns; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class TestContainerRequest extends FormRequest | ||
{ | ||
final public const KeyTest = 'test'; | ||
|
||
public function rules(): array | ||
{ | ||
return [ | ||
self::KeyTest => ['required'], | ||
]; | ||
} | ||
|
||
protected function passedValidation() | ||
{ | ||
$autoAction = $this->container->make(AutoAction::class, ['test']); | ||
assert($autoAction instanceof AutoAction); | ||
|
||
$customAction = $this->container->make(CustomAction::class); | ||
assert($customAction instanceof CustomAction); | ||
|
||
$customAction->autoAction = $autoAction; | ||
} | ||
} |