Download the PHP package lastdragon-ru/lara-asp-testing without Composer
On this page you can find all versions of the php package lastdragon-ru/lara-asp-testing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lastdragon-ru/lara-asp-testing
More information about lastdragon-ru/lara-asp-testing
Files in lastdragon-ru/lara-asp-testing
Package lara-asp-testing
Short Description The Awesome Set of Packages for Laravel - Testing Helpers.
License MIT
Homepage https://github.com/LastDragon-ru/lara-asp
Informations about the package lara-asp-testing
(Laravel) Testing Helpers 🐝
This package provides various useful asserts for PHPUnit 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]: <lara-asp-documentator:requirements "{$directory}">
Requirements
Requirement | Constraint | Supported by |
---|---|---|
PHP | ^8.3 |
HEAD ⋯ 5.0.0 |
^8.2 |
HEAD ⋯ 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 | ^11.0.8 |
HEAD |
^11.0.0 |
7.0.1 ⋯ 6.2.0 |
|
^10.34.0 |
7.0.1 ⋯ 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 | ^11.0.0 |
HEAD ⋯ 6.2.0 |
^10.5.0 |
HEAD |
|
^10.1.0 |
7.0.1 ⋯ 6.0.0 |
Installation
[!NOTE]
The package intended to use in dev.
Usage
[!IMPORTANT]
By default, package overrides scalar comparator to make it strict! So
assertEquals(true, 1)
isfalse
.
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.
Comparators
[!TIP]
Should be registered before test, check/use built-in traits.
DatabaseQueryComparator
Compares two Query
.
We are performing following normalization before comparison to be more precise:
- Renumber
laravel_reserved_*
(it will always start from0
and will not contain gaps) - Format the query by
doctrine/sql-formatter
package
EloquentModelComparator
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.
ScalarStrictComparator
Makes comparison of scalars strict.
Extensions
PHPUnit TestCase
RefreshDatabaseIfEmpty
💀
The trait is very similar to standard \Illuminate\Foundation\Testing\RefreshDatabase
but there is one
difference: it will refresh the database only if it is empty. This is very
useful for local testing and allow significantly reduce bootstrap time.
WithTempDirectory
Allows to create a temporary directory. The directory will be removed automatically after script shutdown.
WithTempFile
Allows to create a temporary file. The file will be removed automatically after script shutdown.
WithTestData
Allows to get instance of TestData
(a small helper to load data
associated with test)
Laravel TestCase
WithTranslations
Allows replacing translation strings for Laravel.
Override
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).
Eloquent Model Factory
FixRecentlyCreated
After creating the model will have wasRecentlyCreated = true
, in most
cases this is unwanted behavior, this trait fixes it.
WithoutModelEvents
Disable models events during make/create.
Mixins
\Illuminate\Testing\TestResponse
Name | Description |
---|---|
assertThat() |
Asserts that response satisfies given constraint. |
assertContentType() |
Asserts that a response has a specified content type. |
assertStatusCode() |
Asserts that a response has a specified status code. |
assertJsonMatchesSchema() |
Asserts that a response contains JSON that matches the schema. |
assertXmlMatchesSchema() |
Asserts that a response contains XML that matches the schema. |
Assertions
assertDatabaseQueryEquals
Asserts that SQL Query equals SQL Query.
Read more.
assertJsonMatchesSchema
Asserts that JSON matches schema. Validation based on the Opis JSON Schema package.
Read more.
assertPsrResponse
Asserts that PSR Response satisfies given constraint (we have a lot of built-in 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.
assertThatResponse
💀
Asserts that PSR Response satisfies given constraint (we have a lot of built-in responses, but, of course, you can create a custom).
Read more.
assertXmlMatchesSchema
Asserts that XML matches schema XSD or Relax NG. Validation based on the standard methods of DOMDocument
class.
Read more.
Laravel Response Testing
What is wrong with the Laravel approach? 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:
assertOk() failed
assertHeader() failed
Expected status code 200 but received 500.
Hmmm, 500, probably this is php error? Why? Where? 😰
Compare with:
assertThat() failed
Reusing the test code is problematic
In most real applications you have multiple roles (eg guest
, user
, admin
), guards, and policies. Very difficult to test all of them and usually you need create many testRouteIsNotAvailableForGuest()
, testRouteIsAvailableForAdminOnly()
, etc with a lot of boilerplate code. Also, often you cannot reuse that (boilerplate) code and must write it again and again. That is really annoying.
Resolving this problem is very simple. First, we need to create classes for the required Responses (actually package already provides few most used responses 🙄). Let's start with a simple JSON response:
Next, lets add JSON Validation Error:
Finally, the test:
The same test with default assertions may look something like this:
Feel the difference 😉
PSR Response Testing
Internally package uses PSR-7
so you can test any Psr\Http\Message\ResponseInterface
🤩
Data Providers on steroids
There is another cool feature that allows us to test a lot of use cases without code duplication - the CompositeDataProvider
. It's merging multiple provides into one in the following way:
So we can organize our tests like this:
Enjoy 😸
Mocking properties (Mockery) 🧪
[!IMPORTANT]
Working prototype for How to mock protected properties? (#1142). Please note that implementation relies on Reflection and internal Mockery methods/properties.
Limitations/Notes:
- Readonly properties should be uninitialized.
- Private properties aren't supported.
- Property value must be an object.
- Property must be used while test.
- Property can be mocked only once.
- Objects without methods will be marked as unused.
Custom Test Requirements
Unfortunately, PHPUnit doesn't allow to add/extend existing requirements and probably will not:
I do not think that additional attributes for test requirements should be added. After all, the existing ones are only convenient syntax sugar. Simply check your custom requirements in a before-test method and call
markTestSkipped()
when they are not met. © @sebastianbergmann
The extension listen several events and checks all attributes of test class/method which are implements Requirement
. If the requirements don't meet, the test will be marked as skipped. Please note that at least one "before" hook will be executed anyway (PHPUnit emits events after hook execution).
You need to register extension first:
And then
Upgrading
Please follow Upgrade Guide.
Contributing
This package is the part of Awesome Set of Packages for Laravel. Please use the main repository to report issues, send pull requests, or ask questions.
"\LastDragon_ru\LaraASP\Testing\Database\QueryLog\Query"
"\LastDragon_ru\LaraASP\Testing\Utils\TestData"
All versions of lara-asp-testing with dependencies
ext-json Version *
ext-libxml Version *
ext-dom Version *
ext-xmlreader Version *
ext-mbstring Version *
composer/semver Version ^3.2
doctrine/sql-formatter Version ^1.1
http-interop/http-factory-guzzle Version ^1.0.0
illuminate/collections Version ^10.34.0|^11.0.0
illuminate/console Version ^10.34.0|^11.0.0
illuminate/contracts Version ^10.34.0|^11.0.0
illuminate/database Version ^10.34.0|^11.0.0
illuminate/testing Version ^10.34.0|^11.0.0
illuminate/translation Version ^10.34.0|^11.0.0
mockery/mockery Version ^1.6.5
opis/json-schema Version ^2.3.0
phpunit/phpunit Version ^10.1.0|^11.0.0
psr/http-message Version ^1.0.0|^2.0.0
sebastian/comparator Version ^5.0|^6.0.0
sebastian/exporter Version ^5.0|^6.0.0
symfony/deprecation-contracts Version ^3.0.0
symfony/filesystem Version ^6.3.0|^7.0.0
symfony/http-foundation Version ^6.3.0|^7.0.0
symfony/mime Version ^6.3.0|^7.0.0
symfony/psr-http-message-bridge Version ^2.0.0|^6.4.0|^7.0.0
symfony/polyfill-php83 Version ^1.28