2024-07-29 16:50:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Tests\Unit\Service\Remote;
|
|
|
|
|
|
|
|
use App\Entity\Remote\Brilo\Users\Address;
|
|
|
|
use App\Entity\Remote\Brilo\Users\AddressGeo;
|
|
|
|
use App\Entity\Remote\Brilo\Users\Company;
|
|
|
|
use App\Entity\Remote\Brilo\Users\User;
|
|
|
|
use App\Service\Remote\BriloApiUsers;
|
|
|
|
use App\Service\Remote\BriloRemoteApiException;
|
|
|
|
use App\Tests\Common\LoggerTrait;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
|
|
|
use Symfony\Component\HttpClient\Response\JsonMockResponse;
|
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
|
|
|
|
|
class BriloApiUsersTest extends TestCase
|
|
|
|
{
|
|
|
|
use LoggerTrait;
|
|
|
|
|
|
|
|
private const array API_USER_LIST = [
|
|
|
|
[
|
|
|
|
"id" => 0,
|
|
|
|
"name" => "Leanne Graham",
|
|
|
|
"username" => "Bret",
|
|
|
|
"email" => "Sincere@april.biz",
|
|
|
|
"address" => [
|
|
|
|
"street" => "Kulas Light",
|
|
|
|
"suite" => "Apt. 555",
|
|
|
|
"city" => "Gwenborough",
|
|
|
|
"zipcode" => "92997-3874",
|
|
|
|
"geo" => [
|
|
|
|
"lat" => "-38.3159",
|
|
|
|
"lng" => "80.1496"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"phone" => "0-770-736-8031 x56442",
|
|
|
|
"website" => "hildegard.org",
|
|
|
|
"company" => [
|
|
|
|
"name" => "Romaguera-Crona",
|
|
|
|
"catchPhrase" => "Multi-layered client-server neural-net",
|
|
|
|
"bs" => "harness real-time e-markets"
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"id" => 1,
|
|
|
|
"name" => "Ervin Howell",
|
|
|
|
"username" => "Antonette",
|
|
|
|
"email" => "Shanna@melissa.tv",
|
|
|
|
"address" => [
|
|
|
|
"street" => "Victor Plains",
|
|
|
|
"suite" => "Suite 878",
|
|
|
|
"city" => "Wisokyburgh",
|
|
|
|
"zipcode" => "90565-7771",
|
|
|
|
"geo" => [
|
|
|
|
"lat" => "-44.9509",
|
|
|
|
"lng" => "-35.4618"
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"phone" => "009-692-6593 x09125",
|
|
|
|
"website" => "anastasia.net",
|
|
|
|
"company" => [
|
|
|
|
"name" => "Deckow-Crist",
|
|
|
|
"catchPhrase" => "Proactive didactic contingency",
|
|
|
|
"bs" => "synergize scalable supply-chains"
|
|
|
|
],
|
|
|
|
"nonExistentKey" => "value"
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
public function testUserListSuccess(): void
|
|
|
|
{
|
|
|
|
$testUserList = $this->buildTestUserList();
|
|
|
|
$serializer = $this->createMock(SerializerInterface::class);
|
|
|
|
$serializer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('deserialize')
|
|
|
|
->with(
|
|
|
|
json_encode(self::API_USER_LIST),
|
|
|
|
User::class . '[]',
|
|
|
|
'json'
|
|
|
|
)->willReturn($testUserList);
|
|
|
|
|
|
|
|
$response = new JsonMockResponse(self::API_USER_LIST);
|
|
|
|
$mockedClient = new MockHttpClient($response);
|
2024-08-04 13:15:12 +00:00
|
|
|
$briloApiUsers = new BriloApiUsers($mockedClient, $this->getLogger(), $serializer, 'http://nonexistent.local');
|
2024-07-29 16:50:58 +00:00
|
|
|
$this->assertEquals($testUserList, $briloApiUsers->getUsers());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidHttpCodeReturned(): void
|
|
|
|
{
|
|
|
|
$serializer = $this->createMock(SerializerInterface::class);
|
|
|
|
$serializer
|
|
|
|
->expects($this->never())
|
|
|
|
->method('deserialize');
|
|
|
|
|
|
|
|
$response = new JsonMockResponse(self::API_USER_LIST, [
|
|
|
|
'http_code' => 401,
|
|
|
|
]);
|
|
|
|
$mockedClient = new MockHttpClient(function () use ($response) {
|
|
|
|
return $response;
|
|
|
|
});
|
|
|
|
$this->expectException(BriloRemoteApiException::class);
|
2024-08-04 13:15:12 +00:00
|
|
|
$briloApiUsers = new BriloApiUsers($mockedClient, $this->getLogger(), $serializer, 'http://nonexistent.local');
|
2024-07-29 16:50:58 +00:00
|
|
|
$briloApiUsers->getUsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return User[]
|
|
|
|
*/
|
|
|
|
private function buildTestUserList(): array
|
|
|
|
{
|
|
|
|
// convert self::API_USER_LIST to User[]
|
|
|
|
return array_map(function ($userData) {
|
|
|
|
return new User(
|
|
|
|
$userData['id'],
|
|
|
|
$userData['name'],
|
|
|
|
$userData['username'],
|
|
|
|
$userData['email'],
|
|
|
|
$userData['phone'],
|
|
|
|
$userData['website'],
|
|
|
|
new Address(
|
|
|
|
$userData['address']['street'],
|
|
|
|
$userData['address']['suite'],
|
|
|
|
$userData['address']['city'],
|
|
|
|
$userData['address']['zipcode'],
|
|
|
|
new AddressGeo(
|
|
|
|
$userData['address']['geo']['lat'],
|
|
|
|
$userData['address']['geo']['lng']
|
|
|
|
)
|
|
|
|
),
|
|
|
|
new Company(
|
|
|
|
$userData['company']['name'],
|
|
|
|
$userData['company']['catchPhrase'],
|
|
|
|
$userData['company']['bs']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}, self::API_USER_LIST);
|
|
|
|
}
|
|
|
|
}
|