PHP code example of lastdragon-ru / lara-asp-testing
1. Go to this page and download the library: Download lastdragon-ru/lara-asp-testing library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lastdragon-ru / lara-asp-testing example snippets
declare(strict_types = 1);
namespace Tests;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use LastDragon_ru\LaraASP\Testing\Assertions\Assertions;
use LastDragon_ru\LaraASP\Testing\Concerns\Concerns;
use Override;
abstract class TestCase extends BaseTestCase {
use Assertions; // Added
use Concerns; // Added
use CreatesApplication;
#[Override]
protected function app(): Application {
return $this->app;
}
}
declare(strict_types = 1);
namespace Tests;
use LastDragon_ru\LaraASP\Testing\Database\RefreshDatabaseIfEmpty;
use LastDragon_ru\LaraASP\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase {
use CreatesApplication;
use RefreshDatabaseIfEmpty;
protected function shouldSeed() {
return true;
}
}
declare(strict_types = 1);
namespace App\Http\Controllers;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\TestCase;
/**
* @internal
*/
#[CoversClass(IndexController::class)]
class IndexControllerTest extends TestCase {
public function testIndex() {
$this->get('/')
->assertOk()
->assertHeader('Content-Type', 'application/json');
}
}
declare(strict_types = 1);
namespace App\Http\Controllers;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\ContentTypes\JsonContentType;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Ok;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\TestCase;
/**
* @internal
*/
#[CoversClass(IndexController::class)]
class IndexControllerTest extends TestCase {
public function testIndex() {
$this->get('/')->assertThat(new Response(
new Ok(),
new JsonContentType()
));
}
}
declare(strict_types = 1);
namespace Tests\Responses;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\ContentTypes\JsonContentType;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Ok;
class JsonResponse extends Response {
public function __construct() {
parent::__construct(
new Ok(),
new JsonContentType(),
);
}
}
declare(strict_types = 1);
namespace Tests\Responses;
use LastDragon_ru\LaraASP\Testing\Constraints\Json\JsonMatchesSchema;
use LastDragon_ru\LaraASP\Testing\Constraints\Json\JsonSchema;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\Body;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\ContentTypes\JsonContentType;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\UnprocessableEntity;
use LastDragon_ru\LaraASP\Testing\Utils\WithTestData;
class ValidationErrorResponse extends Response {
use WithTestData;
public function __construct() {
parent::__construct(
new UnprocessableEntity(),
new JsonContentType(),
new Body([
new JsonMatchesSchema(new JsonSchema(self::getTestData(self::class)->file('.json'))),
]),
);
}
}
declare(strict_types = 1);
namespace App\Http\Controllers;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\Responses;
use Tests\TestCase;
/**
* @internal
*/
#[CoversClass(IndexController::class)]
class IndexControllerTest extends TestCase {
public function testIndex() {
$this->getJson('/')->assertThat(new ValidationErrorResponse());
}
public function testTest() {
$this->getJson('/test')->assertThat(new ValidationErrorResponse());
}
}
declare(strict_types = 1);
namespace App\Http\Controllers;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\TestCase;
/**
* @internal
*/
#[CoversClass(IndexController::class)]
class IndexControllerTest extends TestCase {
public function testIndex() {
$this->getJson('/')
->assertStatus(422)
->assertHeader('Content-Type', 'application/json')
->assertJsonStructure([
'message',
'errors',
]);
}
public function testTest() {
$this->getJson('/test')
->assertStatus(422)
->assertHeader('Content-Type', 'application/json')
->assertJsonStructure([
'message',
'errors',
]);;
}
}
declare(strict_types = 1);
use LastDragon_ru\LaraASP\Testing\Assertions\ResponseAssertions;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\ContentTypes\JsonContentType;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Ok;
use PHPUnit\Framework\TestCase;
class ResponseInterfaceTest extends TestCase {
use ResponseAssertions;
public function testResponse() {
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = null;
self::assertThatResponse($response, new Response(
new Ok(),
new JsonContentType(),
));
}
}
declare(strict_types = 1);
namespace Tests\Feature;
use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Support\Facades\Route;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\NotFound;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Ok;
use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Unauthorized;
use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider;
use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider;
use LastDragon_ru\LaraASP\Testing\Providers\DataProvider as DataProviderContract;
use LastDragon_ru\LaraASP\Testing\Providers\ExpectedFinal;
use LastDragon_ru\LaraASP\Testing\Responses\Laravel\Json\ValidationErrorResponse;
use PHPUnit\Framework\Attributes\DataProvider;use Tests\TestCase;
class ExampleTest extends TestCase {
// <editor-fold desc="Prepare">
// =========================================================================
public function setUp(): void {
parent::setUp();
Route::get('/users/{user}', function (User $user) {
return $user->email;
})->middleware(['auth', SubstituteBindings::class]);
Route::post('/users/{user}', function (Request $request, User $user) {
$user->email = $request->validate([
'email' => 'r(),
self::getModelDataProvider(),
new ArrayDataProvider([
'no email' => [
new ValidationErrorResponse(['email' => null]),
[],
],
'invalid email' => [
new ValidationErrorResponse([
'email' => 'The email must be a valid email address.',
]),
[
'email' => '123',
],
],
'valid email' => [
new Ok(),
[
'email' => '[email protected]',
],
],
])
))->getData();
}
// </editor-fold>
// <editor-fold desc="Shared">
// =========================================================================
protected static function getUserDataProvider(): DataProviderContract {
return new ArrayDataProvider([
'guest' => [
new ExpectedFinal(new Unauthorized()),
null,
],
'authenticated' => [
new Ok(),
function () {
return User::factory()->create();
},
],
]);
}
protected static function getModelDataProvider(): DataProviderContract {
return new ArrayDataProvider([
'user not exists' => [
new ExpectedFinal(new NotFound()),
null,
],
'user exists' => [
new Ok(),
function () {
return User::factory()->create();
},
],
]);
}
// </editor-fold>
}
declare(strict_types = 1);
// phpcs:disable PSR1.Files.SideEffects
// phpcs:disable PSR1.Classes.ClassDeclaration
namespace LastDragon_ru\LaraASP\Testing\Docs\Examples\MockProperties;
use LastDragon_ru\LaraASP\Testing\Mockery\MockProperties;
use Mockery;
class A {
public function __construct(
protected readonly B $b,
) {
// empty
}
public function a(): void {
$this->b->b();
}
}
class B {
public function b(): void {
echo 1;
}
}
$mock = Mockery::mock(A::class, MockProperties::class);
$mock
->shouldUseProperty('b')
->value(
Mockery::mock(B::class), // or just `new B()`.
);
$mock->a();
declare(strict_types = 1);
use LastDragon_ru\LaraASP\Testing\Requirements\Requirements\RequiresComposerPackage;
use PHPUnit\Framework\TestCase;
class SomePackageTest extends TestCase {
#[RequiresComposerPackage('some/package')]
public function testSomePackage(): void {
// .....
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.