2024-07-29 16:50:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Tests\Unit\Service\Remote;
|
|
|
|
|
2024-07-29 18:41:57 +00:00
|
|
|
use App\Entity\Remote\Brilo\Posts\Post;
|
|
|
|
use App\Entity\Remote\Brilo\Users\User;
|
|
|
|
use App\Service\Remote\BriloApiPosts;
|
|
|
|
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 BlogApiPostsTest extends TestCase
|
2024-07-29 16:50:58 +00:00
|
|
|
{
|
2024-07-29 18:41:57 +00:00
|
|
|
use LoggerTrait;
|
|
|
|
|
|
|
|
private const array API_POSTS_LIST = [
|
|
|
|
[
|
|
|
|
"userId" => 1,
|
|
|
|
"id" => 1,
|
|
|
|
"title" => "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
|
|
|
|
"body" => "quia et suscipit
|
|
|
|
suscipit recusandae consequuntur expedita et cum
|
|
|
|
reprehenderit molestiae ut ut quas totam
|
|
|
|
nostrum rerum est autem sunt rem eveniet architecto"
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"userId" => 1,
|
|
|
|
"id" => 2,
|
|
|
|
"title" => "qui est esse",
|
|
|
|
"body" => "est rerum tempore vitae
|
|
|
|
sequi sint nihil reprehenderit dolor beatae ea dolores neque
|
|
|
|
fugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis
|
|
|
|
qui aperiam non debitis possimus qui neque nisi nulla"
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
public function testUserListSuccess(): void
|
|
|
|
{
|
|
|
|
$testUserList = $this->buildTestPostList();
|
|
|
|
$serializer = $this->createMock(SerializerInterface::class);
|
|
|
|
$serializer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('deserialize')
|
|
|
|
->with(
|
|
|
|
json_encode(self::API_POSTS_LIST),
|
|
|
|
Post::class . '[]',
|
|
|
|
'json'
|
|
|
|
)->willReturn($testUserList);
|
|
|
|
|
|
|
|
$response = new JsonMockResponse(self::API_POSTS_LIST);
|
|
|
|
$mockedClient = new MockHttpClient($response);
|
|
|
|
$briloApiPosts = new BriloApiPosts($mockedClient, $this->getLogger(), $serializer);
|
|
|
|
$this->assertEquals($testUserList, $briloApiPosts->getPosts());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidHttpCodeReturned(): void
|
|
|
|
{
|
|
|
|
$serializer = $this->createMock(SerializerInterface::class);
|
|
|
|
$serializer
|
|
|
|
->expects($this->never())
|
|
|
|
->method('deserialize');
|
|
|
|
|
|
|
|
$response = new JsonMockResponse(self::API_POSTS_LIST, [
|
|
|
|
'http_code' => 401,
|
|
|
|
]);
|
|
|
|
$mockedClient = new MockHttpClient(function () use ($response) {
|
|
|
|
return $response;
|
|
|
|
});
|
|
|
|
$this->expectException(BriloRemoteApiException::class);
|
|
|
|
$briloApiPosts = new BriloApiPosts($mockedClient, $this->getLogger(), $serializer);
|
|
|
|
$briloApiPosts->getPosts();
|
|
|
|
}
|
2024-07-29 16:50:58 +00:00
|
|
|
|
2024-07-29 18:41:57 +00:00
|
|
|
/**
|
|
|
|
* @return Post[]
|
|
|
|
*/
|
|
|
|
public function buildTestPostList(): array
|
|
|
|
{
|
|
|
|
// Convert self::API_POSTS_LIST to Post[] and return it
|
|
|
|
return array_map(function ($postData) {
|
|
|
|
return new Post(
|
|
|
|
$postData['userId'],
|
|
|
|
$postData['id'],
|
|
|
|
$postData['title'],
|
|
|
|
$postData['body']
|
|
|
|
);
|
|
|
|
}, self::API_POSTS_LIST);
|
|
|
|
}
|
2024-07-29 16:50:58 +00:00
|
|
|
}
|