113 lines
3.1 KiB
PHP
113 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Functional;
|
|
|
|
use App\Factory\ApiTokenFactory;
|
|
use App\Factory\CompanyFactory;
|
|
use App\Factory\UserFactory;
|
|
|
|
class UserResourceTest extends ApiTestCase
|
|
{
|
|
public function testUserLoginHttp(): void
|
|
{
|
|
$company = CompanyFactory::createOne();
|
|
$user = UserFactory::createOne(['company' => $company]);
|
|
|
|
$this->browser()
|
|
->post('/login', [
|
|
'json' => [
|
|
'email' => $user->getEmail(),
|
|
'password' => 'password'
|
|
]
|
|
])
|
|
->assertStatus(204)
|
|
->assertHeaderContains('Location', '/api/users/' . $user->getId());
|
|
}
|
|
|
|
public function testUserLogoutHttp()
|
|
{
|
|
$user = UserFactory::createOne();
|
|
$this->browser()
|
|
->actingAs($user)
|
|
->get('/logout')
|
|
->assertStatus(204)
|
|
;
|
|
}
|
|
|
|
public function testUserLoginToken(): void
|
|
{
|
|
$token = ApiTokenFactory::createOne();
|
|
|
|
$this->browser()
|
|
->get('api/users', [
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . $token->getToken()
|
|
]
|
|
])
|
|
->assertStatus(200);
|
|
}
|
|
|
|
public function testCanGetUser(): void
|
|
{
|
|
$user = UserFactory::createOne();
|
|
|
|
$this->browser()
|
|
->actingAs($user)
|
|
->get('/api/users/' . $user->getId())
|
|
->assertSuccessful()
|
|
->assertJson()
|
|
->assertJsonMatches('email', $user->getEmail())
|
|
->assertJsonMatches('firstName', $user->getFirstName())
|
|
->assertJsonMatches('lastName', $user->getLastName())
|
|
;
|
|
}
|
|
|
|
public function testCanPostToCreateUser(): void
|
|
{
|
|
$loggedUser = UserFactory::createOne();
|
|
|
|
$this->browser()
|
|
->actingAs($loggedUser)
|
|
->post('/api/users', [
|
|
'json' => [
|
|
'email' => 'john.doe@mail.com',
|
|
'firstName' => 'John',
|
|
'lastName' => 'Doe',
|
|
'password' => 'password',
|
|
],
|
|
])
|
|
->assertSuccessful()
|
|
->post('/login', [
|
|
'json' => [
|
|
'email' => 'john.doe@mail.com',
|
|
'password' => 'password',
|
|
],
|
|
])
|
|
->assertSuccessful();
|
|
}
|
|
|
|
public function testCanPatchToUpdateUser(): void
|
|
{
|
|
$loggedUser = UserFactory::createOne();
|
|
|
|
$this->browser()
|
|
->actingAs($loggedUser)
|
|
->patch('/api/users/' . $loggedUser->getId(), [
|
|
'json' => [
|
|
'firstName' => 'John',
|
|
'lastName' => 'Doe',
|
|
],
|
|
'headers' => [
|
|
'Content-Type' => 'application/merge-patch+json'
|
|
]
|
|
])
|
|
->assertSuccessful()
|
|
->get('/api/users/' . $loggedUser->getId())
|
|
->assertSuccessful()
|
|
->assertJson()
|
|
->assertJsonMatches('firstName', 'John')
|
|
->assertJsonMatches('lastName', 'Doe');
|
|
;
|
|
}
|
|
}
|