Skip to content

Commit

Permalink
test(ai/common): add CoreApi/Endpoint/Exception/
Browse files Browse the repository at this point in the history
  • Loading branch information
coppee committed Oct 12, 2023
1 parent 3a2db61 commit 5691ca8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace EMS\CommonBundle\Tests\Unit\Common\CoreApi\Exception;

use EMS\CommonBundle\Common\CoreApi\Exception\BaseUrlNotDefinedException;
use PHPUnit\Framework\TestCase;

class BaseUrlNotDefinedExceptionAiTest extends TestCase
{
public function testExceptionDefaultMessage(): void
{
$exception = new BaseUrlNotDefinedException();

$expectedMessage = 'Core api base url not defined!';
$this->assertEquals($expectedMessage, $exception->getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace EMS\CommonBundle\Tests\Unit\Common\CoreApi\Exception;

use EMS\CommonBundle\Common\CoreApi\Exception\NotAuthenticatedException;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\HttpClient\ResponseInterface;

class NotAuthenticatedExceptionAiTest extends TestCase
{
public function testExceptionMessageAndCode(): void
{
$httpCode = 401;
$httpMethod = 'GET';
$url = 'https://example.com/api/resource';

$response = $this->createMock(ResponseInterface::class);
$response->method('getInfo')
->willReturn([
'http_code' => $httpCode,
'http_method' => $httpMethod,
'url' => $url,
]);
$response->method('getStatusCode')->willReturn($httpCode);

$exception = new NotAuthenticatedException($response);

$expectedMessage = \sprintf('%s Unauthorized for [%s] %s', $httpCode, $httpMethod, $url);
$this->assertEquals($expectedMessage, $exception->getMessage());
$this->assertEquals($httpCode, $exception->getCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace EMS\CommonBundle\Tests\Unit\Common\CoreApi\Exception;

use EMS\CommonBundle\Common\CoreApi\Exception\NotSuccessfulException;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\HttpClient\ResponseInterface;

class NotSuccessfulExceptionAiTest extends TestCase
{
public function testExceptionMessage(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getInfo')
->willReturn([
'http_method' => 'GET',
'url' => 'https://example.com/api/test',
]);
$response->method('getStatusCode')
->willReturn(404);

$exception = new NotSuccessfulException($response);

$expectedMessage = '[GET] https://example.com/api/test was not successful! (Check logs!)';
$this->assertEquals($expectedMessage, $exception->getMessage());
$this->assertEquals(404, $exception->getCode());
}
}

0 comments on commit 5691ca8

Please sign in to comment.