add more tests for backends.Common

This commit is contained in:
Abdulmhsen B. A. A
2023-12-22 21:29:53 +03:00
parent 73823288a3
commit a13f489be4
2 changed files with 54 additions and 0 deletions

View File

@@ -89,5 +89,11 @@ class ErrorTest extends TestCase
$error->hasException(),
'hasException() should return true if previous exception is set.'
);
$this->assertStringContainsString(
'message with no tags',
$error->__toString(),
'__toString() should return the message.'
);
}
}

View File

@@ -5,10 +5,16 @@ declare(strict_types=1);
namespace Tests\Backends\Common;
use App\Backends\Common\Error;
use App\Libs\Exceptions\Backends\RuntimeException;
use App\Libs\TestCase;
use App\Libs\Uri;
use Monolog\Handler\NullHandler;
use Monolog\Logger;
class ResponseTest extends TestCase
{
use \App\Backends\Common\CommonTrait;
public function test_backend_response_object(): void
{
$response = new \App\Backends\Common\Response(
@@ -49,4 +55,46 @@ class ResponseTest extends TestCase
);
}
public function test_tryResponse(): void
{
$context = new \App\Backends\Common\Context(
clientName: 'test',
backendName: 'test',
backendUrl: new Uri('https://example.com'),
cache: new \App\Backends\Common\Cache(
logger: new Logger('test', [new NullHandler()]),
cache: new \Symfony\Component\Cache\Psr16Cache(
new \Symfony\Component\Cache\Adapter\NullAdapter()
),
),
trace: false,
);
$response = (fn() => $this->tryResponse($context, fn() => throw new RuntimeException('test', 500)))();
$this->assertTrue(
$response->hasError(),
'Response object should not have an error if error is null.'
);
$this->assertNull(
$response->response,
'Response object should have the same response as the one passed in the constructor.'
);
$this->assertInstanceOf(
Error::class,
$response->getError(),
'getError() should return an Error object in all cases even if error is null.'
);
$response = (fn() => $this->tryResponse($context, fn() => 'i am teapot'))();
$this->assertSame(
'i am teapot',
$response->response,
'Response object should have the same response as the one passed in the constructor.'
);
}
}