https://github.com/lastdragon-ru/lara-asp-testing
This package is the part of Awesome Set of Packages for Laravel
https://github.com/lastdragon-ru/lara-asp-testing
lara-asp laravel laravel-package phpunit phpunit-assertions psr-7 testing
Last synced: 2 months ago
JSON representation
This package is the part of Awesome Set of Packages for Laravel
- Host: GitHub
- URL: https://github.com/lastdragon-ru/lara-asp-testing
- Owner: LastDragon-ru
- License: mit
- Created: 2021-01-19T08:46:24.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-05-18T07:08:40.000Z (about 2 years ago)
- Last Synced: 2024-05-18T08:22:34.279Z (about 2 years ago)
- Topics: lara-asp, laravel, laravel-package, phpunit, phpunit-assertions, psr-7, testing
- Language: PHP
- Homepage: https://github.com/LastDragon-ru/lara-asp
- Size: 710 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# (Laravel) Testing Helpers 🐝
This package provides various useful asserts for [PHPUnit](https://phpunit.de/) and better solution for HTTP tests - testing HTTP response has never been so easy! And this not only about `TestResponse` but any PSR response 😎
[include:artisan]:
[//]: # (start: preprocess/78cfc4c7c7c55577)
[//]: # (warning: Generated automatically. Do not edit.)
# Requirements
| Requirement | Constraint | Supported by |
|--------------|---------------------|------------------|
| PHP | `^8.5` | `HEAD` |
| | `^8.4` | `HEAD ⋯ 8.0.0` |
| | `^8.3` | `10.3.0 ⋯ 5.0.0` |
| | `^8.2` | `7.2.0 ⋯ 2.0.0` |
| | `^8.1` | `6.4.2 ⋯ 2.0.0` |
| | `^8.0` | `4.6.0 ⋯ 2.0.0` |
| | `^8.0.0` | `1.1.2 ⋯ 0.12.0` |
| | `>=8.0.0` | `0.11.0 ⋯ 0.4.0` |
| | `>=7.4.0` | `0.3.0 ⋯ 0.1.0` |
| Laravel | `^12.0.1` | `HEAD ⋯ 9.0.0` |
| | `^11.0.8` | `8.1.1 ⋯ 8.0.0` |
| | `^11.0.0` | `7.2.0 ⋯ 6.2.0` |
| | `^10.34.0` | `7.2.0 ⋯ 6.2.0` |
| | `^10.0.0` | `6.1.0 ⋯ 2.1.0` |
| | `^9.21.0` | `5.6.0 ⋯ 5.0.0-beta.1` |
| | `^9.0.0` | `5.0.0-beta.0 ⋯ 0.12.0` |
| | `^8.22.1` | `3.0.0 ⋯ 0.2.0` |
| | `^8.0` | `0.1.0` |
| PHPUnit | `^12.5.8` | `HEAD` |
| | `^12.0.0` | `10.3.0 ⋯ 9.0.0` |
| | `^11.3.0` | `10.3.0 ⋯ 10.1.0` |
| | `^11.2.0` | `10.0.0` |
| | `^11.1.0` | `9.3.1` , `9.3.0` |
| | `^11.0.0` | `9.2.0 ⋯ 6.2.0` |
| | `^10.5.0` | `8.1.1 ⋯ 8.0.0` |
| | `^10.1.0` | `7.2.0 ⋯ 6.0.0` |
[//]: # (end: preprocess/78cfc4c7c7c55577)
[include:template]: ../../docs/Shared/InstallationDev.md ({"data": {"package": "lara-asp-testing"}})
[//]: # (start: preprocess/35b8d0b2d34b8b44)
[//]: # (warning: Generated automatically. Do not edit.)
# Installation
> [!NOTE]
>
> The package intended to use in dev.
```shell
composer require --dev lastdragon-ru/lara-asp-testing
```
[//]: # (end: preprocess/35b8d0b2d34b8b44)
# Usage
> [!IMPORTANT]
>
> By default, package overrides scalar comparator to make it strict! So `assertEquals(true, 1)` is `false`.
In the general case, you just need to update `tests/TestCase.php` to include most important things, but you also can include only desired features, please see related traits and extensions below.
[include:example]: ./docs/Examples/TestCase.php
[//]: # (start: preprocess/564b8c0c2927454f)
[//]: # (warning: Generated automatically. Do not edit.)
```php
app;
}
}
```
[//]: # (end: preprocess/564b8c0c2927454f)
# Comparators
> [!TIP]
>
> Should be registered before test, check/use [built-in traits](./src/Concerns).
## [`DatabaseQueryComparator`](./src/Comparators/DatabaseQueryComparator.php)
[include:docblock]: ./src/Comparators/DatabaseQueryComparator.php
[//]: # (start: preprocess/e008bf0a6f53648d)
[//]: # (warning: Generated automatically. Do not edit.)
Compares two [`Query`][code-links/f2055681d6897706].
We are performing following normalization before comparison to be more precise:
* Renumber `laravel_reserved_*` (it will always start from `0` and will not contain gaps)
* Format the query by [`doctrine/sql-formatter`](https://github.com/doctrine/sql-formatter) package
[//]: # (end: preprocess/e008bf0a6f53648d)
## [`EloquentModelComparator`](./src/Comparators/EloquentModelComparator.php)
[include:docblock]: ./src/Comparators/EloquentModelComparator.php
[//]: # (start: preprocess/b9eae8b36fc2d911)
[//]: # (warning: Generated automatically. Do not edit.)
Compares two Eloquent Models.
The problem is models after creating from the factory and selecting from
the database may have different types for the same properties. For example,
`factory()->create()` will set `key` as `int`, but `select` will set it to
`string` and (strict) comparison will fail. This comparator normalizes
properties types before comparison.
[//]: # (end: preprocess/b9eae8b36fc2d911)
# Extensions
## Laravel `TestCase`
### [`WithTranslations`](./src/Utils/WithTranslations.php)
[include:docblock]: ./src/Utils/WithTranslations.php
[//]: # (start: preprocess/4c9468401db9a611)
[//]: # (warning: Generated automatically. Do not edit.)
Allows replacing translation strings for Laravel.
[//]: # (end: preprocess/4c9468401db9a611)
### [`Override`](./src/Concerns/Override.php)
[include:docblock]: ./src/Concerns/Override.php
[//]: # (start: preprocess/c09d2d2405dbd5d3)
[//]: # (warning: Generated automatically. Do not edit.)
Similar to `\Illuminate\Foundation\Testing\Concerns\InteractsWithContainer` but will mark test as failed if
override was not used while test (that helps to find unused code).
[//]: # (end: preprocess/c09d2d2405dbd5d3)
## Eloquent Model Factory
### [`FixRecentlyCreated`](./src/Database/Eloquent/Factories/FixRecentlyCreated.php)
[include:docblock]: ./src/Database/Eloquent/Factories/FixRecentlyCreated.php
[//]: # (start: preprocess/59039405fcb32123)
[//]: # (warning: Generated automatically. Do not edit.)
After creating the model will have `wasRecentlyCreated = true`, in most
cases this is unwanted behavior, this trait fixes it.
[//]: # (end: preprocess/59039405fcb32123)
### [`WithoutModelEvents`](./src/Database/Eloquent/Factories/WithoutModelEvents.php)
[include:docblock]: ./src/Database/Eloquent/Factories/WithoutModelEvents.php
[//]: # (start: preprocess/2a65f210857bd0bb)
[//]: # (warning: Generated automatically. Do not edit.)
Disable models events during make/create.
[//]: # (end: preprocess/2a65f210857bd0bb)
# Mixins
## `\Illuminate\Testing\TestResponse`
| Name | Description |
|----------------------------------------------------------|---------------------------------------------------|
| [`assertThat()`](./docs/Assertions/AssertPsrResponse.md) | Asserts that response satisfies given constraint. |
# Assertions
[include:document-list]: ./docs/Assertions
[//]: # (start: preprocess/c79a463462fd8331)
[//]: # (warning: Generated automatically. Do not edit.)
## [`assertDatabaseQueryEquals`]()
Asserts that SQL Query equals SQL Query.
[Read more]().
## [`assertJsonMatchesSchema`]()
Asserts that JSON matches [schema](https://json-schema.org/). Validation based on the [Opis JSON Schema](https://github.com/opis/json-schema) package.
[Read more]().
## [`assertPsrResponse`]()
Asserts that PSR Response satisfies given constraint (we have a lot of built-in [constraints](src/Constraints/Response) and [responses](src/Responses), but, of course, you can create a custom).
[Read more]().
## [`assertQueryLogEquals`]()
Asserts that `QueryLog` equals `QueryLog`.
[Read more]().
## [`assertScheduled`]()
Asserts that Schedule contains task.
[Read more]().
## [`assertScoutQueryEquals`]()
Asserts that Scout Query equals Scout Query.
[Read more]().
[//]: # (end: preprocess/c79a463462fd8331)
# Laravel Response Testing
What is wrong with the [Laravel approach](https://laravel.com/docs/http-tests)? Well, there are two big problems.
## Where is the error?
You never know why your test failed and need to debug it to find the reason. Real-life example:
```php
get('/')
->assertOk()
->assertHeader('Content-Type', 'application/json');
}
}
```
assertOk() failed
```text
Testing started at 15:46 ...
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Random Seed: 1610451974
Expected status code 200 but received 500.
Failed asserting that 200 is identical to 500.
vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:186
app/Http/Controllers/IndexControllerTest.php:16
Time: 00:01.373, Memory: 26.00 MB
```
assertHeader() failed
```text
Testing started at 17:57 ...
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Random Seed: 1610459878
Header [Content-Type] was found, but value [text/html; charset=UTF-8] does not match [application/json].
Failed asserting that two values are equal.
Expected :'application/json'
Actual :'text/html; charset=UTF-8'
vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:229
app/Http/Controllers/IndexControllerTest.php:18
Time: 00:01.082, Memory: 24.00 MB
FAILURES!
Tests: 1, Assertions: 3, Failures: 1.
Process finished with exit code 1
```
> Expected status code 200 but received 500.
Hmmm, 500, probably this is php error? Why? Where? 😰
Compare with:
```php
get('/')->assertThat(new Response(
new Ok(),
new JsonContentType()
));
}
}
```
assertThat() failed
```text
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Random Seed: 1610461475
Failed asserting that GuzzleHttp\Psr7\Response Object &000000001ef973410000000013328b0b (
'reasonPhrase' => 'Internal Server Error'
'statusCode' => 500
'headers' => Array &0 (
'cache-control' => Array &1 (
0 => 'no-cache, private'
)
'date' => Array &2 (
0 => 'Tue, 12 Jan 2021 14:24:36 GMT'
)
'content-type' => Array &3 (
0 => 'text/html; charset=UTF-8'
)
)
'headerNames' => Array &5 (
'cache-control' => 'cache-control'
'date' => 'date'
'content-type' => 'content-type'
'set-cookie' => 'Set-Cookie'
)
'protocol' => '1.1'
'stream' => GuzzleHttp\Psr7\Stream Object &000000001ef972d20000000013328b0b (
'stream' => resource(846) of type (stream)
'size' => null
'seekable' => true
'readable' => true
'writable' => true
'uri' => 'php://temp'
'customMetadata' => Array &6 ()
)
) has Status Code is equal to 200.