Skip to content

Commit 4bb1c07

Browse files
committed
Initial commit
0 parents  commit 4bb1c07

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Laravel Test Assertions
2+
A set of helpful assertions when testing Laravel applications.
3+
4+
## Requirements
5+
Your application must be running the latest LTS version (5.5) or higher and using [Laravel's testing harness](https://laravel.com/docs/5.8/testing).
6+
7+
## Installation
8+
You may install these assertions with Composer by running:
9+
10+
```sh
11+
composer require --dev jasonmccreary/laravel-test-assertions
12+
```
13+
14+
Afterwards, add the trait to your base `TestCase` class:
15+
16+
```php
17+
<?php
18+
namespace Tests;
19+
20+
use JMac\Testing\Traits\HttpTestAssertions;
21+
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
22+
23+
abstract class TestCase extends BaseTestCase
24+
{
25+
use CreatesApplication, HttpTestAssertions;
26+
}
27+
```
28+
29+
## Assertions
30+
This package adds several assertions helpful when writing [Http Tests](https://laravel.com/docs/5.8/http-tests).
31+
32+
```assertActionUsesFormRequest(string $controller, string $method, string $form_request)```
33+
Verifies the _action_ for a given controller performs validation using the given form request.
34+

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "jasonmccreary/laravel-test-assertions",
3+
"description": "A set of helpful assertions when testing Laravel applications.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Jason McCreary",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.0"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"JMac\\Testing\\": "src/"
18+
}
19+
}
20+
}

src/Traits/HttpTestAssertions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace JMac\Testing\Traits;
4+
5+
trait HttpTestAssertions
6+
{
7+
public function assertActionUsesFormRequest(string $controller, string $method, string $form_request)
8+
{
9+
$this->assertTrue(is_subclass_of($form_request, 'Illuminate\\Foundation\\Http\\FormRequest'), $form_request . ' is not a type of Form Request');
10+
11+
try {
12+
$reflector = new \ReflectionClass($controller);
13+
$action = $reflector->getMethod($method);
14+
} catch (\ReflectionException $exception) {
15+
$this->fail('Controller action could not be found: ' . $controller . '@' . $method);
16+
}
17+
18+
$this->assertTrue($action->isPublic(), 'Action "' . $method . '" is not public, controller actions must be public.');
19+
20+
$actual = collect($action->getParameters())->contains(function ($parameter) use ($form_request) {
21+
return $parameter->getType() instanceof \ReflectionNamedType && $parameter->getType()->getName() === $form_request;
22+
});
23+
24+
$this->assertTrue($actual, 'Action "' . $method . '" does not have validation using the "' . $form_request . '" Form Request.');
25+
}
26+
}

0 commit comments

Comments
 (0)