Skip to content

Commit

Permalink
feat: add new package worksome/request-factories and some test to exa…
Browse files Browse the repository at this point in the history
…mple
  • Loading branch information
sojeda committed Nov 16, 2023
1 parent 5b85be5 commit 9e664dd
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 33 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^7.0",
"pestphp/pest": "^2.6",
"pestphp/pest-plugin-laravel": "^2.0",
"pestphp/pest-plugin-laravel": "^2.2",
"phpunit/phpunit": "^10.2",
"ramsey/conventional-commits": "^1.5",
"spatie/laravel-ignition": "^2.0",
"webgriffe/captainhook": "^2.1",
"worksome/coding-style": "^2.5"
"worksome/coding-style": "^2.5",
"worksome/request-factories": "^3.2"
},
"autoload": {
"psr-4": {
Expand Down
75 changes: 74 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"Nuno",
"PAPERTRAIL",
"Parens",
"passw",
"phpredis",
"Phpstan",
"phpstorm",
Expand Down
53 changes: 25 additions & 28 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_DATABASE" value="testing"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<coverage/>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_DATABASE" value="testing"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/App/Users/Controllers/StoreUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function __invoke(StoreUserRequest $request, StoreUserAction $storeUserAc

return responder()
->success($user, UserTransformer::class)
->respond();
->respond(JsonResponse::HTTP_CREATED);
}
}
32 changes: 32 additions & 0 deletions tests/Feature/Users/ListUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Users;

use App\Users\Controllers\StoreUserController;
use App\Users\Transformers\UserTransformer;
use Database\Factories\UserFactory;
use Domain\Users\Models\User;
use Illuminate\Support\Collection;
use function Pest\Laravel\getJson;

describe('users', function () {
/** @see StoreUserController */
it('can list users successfully', function () {
/** @var Collection<int, User> $users */
$users = UserFactory::new()
->count(5)
->create();

$transformer = new UserTransformer();

getJson(url('/api/users'))
->assertSuccessful()
->assertExactJson([
'status' => 200,
'success' => true,
'data' => $users->map(fn(User $user) => $transformer->transform($user))->toArray(),
]);
});
});
25 changes: 25 additions & 0 deletions tests/Feature/Users/StoreUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Users;

use App\Users\Controllers\StoreUserController;
use Tests\RequestFactories\StoreUserRequestFactory;
use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\postJson;

describe('users', function () {
/** @see StoreUserController */
it('can create a user successfully', function () {
$data = StoreUserRequestFactory::new()->create();

postJson(url('/api/users'), $data)
->assertCreated();

assertDatabaseHas('users', [
'name' => $data['name'],
'email' => $data['email']
]);
});
});
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

uses(
Tests\TestCase::class,
// Illuminate\Foundation\Testing\RefreshDatabase::class,
Illuminate\Foundation\Testing\RefreshDatabase::class,
)->in('Feature');

/*
Expand Down
20 changes: 20 additions & 0 deletions tests/RequestFactories/StoreUserRequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tests\RequestFactories;

use Worksome\RequestFactories\RequestFactory;

class StoreUserRequestFactory extends RequestFactory
{
public function definition(): array
{
return [
'name' => $this->faker->name,
'email' => $this->faker->email,
'password' => 'passw0rd',
'password_confirmation' => 'passw0rd'
];
}
}

0 comments on commit 9e664dd

Please sign in to comment.