Skip to content

Commit

Permalink
Добавлены тесты получения списка пользователей, создания и изменения
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Jun 26, 2024
1 parent 8dcb683 commit b00c357
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 68 deletions.
2 changes: 2 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->redirectGuestsTo('/');
$middleware->web(CheckBannedMiddleware::class);
$middleware->trustProxies(at: ['127.0.0.1', '192.168.65.1/16']);
})
->withExceptions(function (Exceptions $exceptions) {
//
Expand Down
48 changes: 24 additions & 24 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<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">./app</directory>
</include>
</source>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<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">./app</directory>
</include>
</source>
</phpunit>
14 changes: 7 additions & 7 deletions routes/admin-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
});

Route::group(['prefix' => 'users', 'middleware' => ['auth:web', 'can:is-admin']], function () {
Route::get('/', [AdminUsersController::class, 'index'])->name('admin-auth.users.index');
Route::get('/list-filters', [AdminUsersController::class, 'listFilters'])->name('admin-auth.users.list-filters');
Route::post('/create', [AdminUsersController::class, 'create'])->name('admin-auth.users.create');
Route::post('/update', [AdminUsersController::class, 'update'])->name('admin-auth.users.update');
Route::get('/get-user-form/{id}', [AdminUsersController::class, 'editUserFormData'])->name('admin-auth.users.edit-user-form');
Route::get('/get-form-params', [AdminUsersController::class, 'getFormParams'])->name('admin-auth.users.get-form-params');
Route::post('/delete/{id}', [AdminUsersController::class, 'delete'])->name('admin-auth.users.delete');
Route::get('/', [AdminUsersController::class, 'index'])->name('admin.users.index');
Route::get('/list-filters', [AdminUsersController::class, 'listFilters'])->name('admin.users.list-filters');
Route::post('/create', [AdminUsersController::class, 'create'])->name('admin.users.create');
Route::post('/update', [AdminUsersController::class, 'update'])->name('admin.users.update');
Route::get('/get-user-form/{id}', [AdminUsersController::class, 'editUserFormData'])->name('admin.users.edit-user-form');
Route::get('/get-form-params', [AdminUsersController::class, 'getFormParams'])->name('admin.users.get-form-params');
Route::post('/delete/{id}', [AdminUsersController::class, 'delete'])->name('admin.users.delete');
});
19 changes: 0 additions & 19 deletions tests/Feature/ExampleTest.php

This file was deleted.

50 changes: 50 additions & 0 deletions tests/Feature/Users/AdminUsersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Users;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Modules\Users\Models\User;
use Tests\TestCase;

class AdminUsersTest extends TestCase
{
use RefreshDatabase;

public function testAccessToPages(): void
{
$response = $this->get(route('admin.users.index'));
$response->assertRedirect('/');
}

public function testIndexPage(): void
{
$user = User::query()->find(1);
$this->actingAs($user, 'web');
$response = $this->get(route('admin.users.index'));

$response->assertStatus(200)
->assertJsonStructure(
[
'data' => [
'*' => [
'id',
'name',
'active',
'email',
'role',
'createdAt',
'updatedAt',
'createdBy',
'updatedBy',
],
],
'current_page',
'last_page',
'total',
'per_page',
]
);
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;

protected bool $seed = true;
}
18 changes: 0 additions & 18 deletions tests/Unit/ExampleTest.php

This file was deleted.

54 changes: 54 additions & 0 deletions tests/Unit/Users/Services/UsersServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Users\Services;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Modules\Users\DTO\Request\AdminCreateUserRequestDTO;
use Modules\Users\DTO\Request\AdminUpdateUserRequestDTO;
use Modules\Users\Services\UsersService;
use Tests\TestCase;

class UsersServiceTest extends TestCase
{
use RefreshDatabase;

public function testCreateAndUpdateUser(): void
{
$userService = new UsersService();
$userDTO = new AdminCreateUserRequestDTO(
true,
'UserName',
'user-email@example.com',
'admin',
'password'
);
$createdUser = $userService->create($userDTO);

$this->assertTrue($createdUser->active);
$this->assertEquals('UserName', $createdUser->name);
$this->assertEquals('user-email@example.com', $createdUser->email);
$this->assertEquals('admin', $createdUser->role);
$this->assertNotEmpty($createdUser->password);


$updateRequest = new AdminUpdateUserRequestDTO(
$createdUser->id,
false,
'UpdateUserName',
'updated-user-email@example.com',
'user',
Hash::make('password2')
);

$updatedUser = $userService->update($updateRequest);

$this->assertFalse($updatedUser->active);
$this->assertEquals('UpdateUserName', $updatedUser->name);
$this->assertEquals('updated-user-email@example.com', $updatedUser->email);
$this->assertEquals('user', $updatedUser->role);
$this->assertNotEmpty($updatedUser->password);
}
}

0 comments on commit b00c357

Please sign in to comment.