Created more tests

This commit is contained in:
Alejandro Celaya 2019-07-20 10:47:12 +02:00
parent bc99ee6ebe
commit 37e286df48
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\EventDispatcher\Async;
use Interop\Container\ContainerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\EventDispatcher\Async\TaskRunner;
use Shlinkio\Shlink\EventDispatcher\Async\TaskRunnerDelegator;
use Swoole\Http\Server as HttpServer;
class TaskRunnerDelegatorTest extends TestCase
{
/** @var TaskRunnerDelegator */
private $delegator;
public function setUp(): void
{
$this->delegator = new TaskRunnerDelegator();
}
/** @test */
public function serverIsFetchedFromCallbackAndDecorated(): void
{
$server = $this->createMock(HttpServer::class);
$server
->expects($this->exactly(2))
->method('on');
$callback = function () use ($server) {
return $server;
};
$container = $this->prophesize(ContainerInterface::class);
$getTaskRunner = $container->get(TaskRunner::class)->willReturn($this->prophesize(TaskRunner::class)->reveal());
$getLogger = $container->get(LoggerInterface::class)->willReturn(
$this->prophesize(LoggerInterface::class)->reveal()
);
$result = ($this->delegator)($container->reveal(), '', $callback);
$this->assertSame($server, $result);
$getTaskRunner->shouldHaveBeenCalledOnce();
$getLogger->shouldHaveBeenCalledOnce();
}
}

View File

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\EventDispatcher\Async;
use Interop\Container\ContainerInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use ReflectionObject;
use Shlinkio\Shlink\EventDispatcher\Async\TaskRunner;
use Shlinkio\Shlink\EventDispatcher\Async\TaskRunnerFactory;
class TaskRunnerFactoryTest extends TestCase
{
/** @var TaskRunnerFactory */
private $factory;
public function setUp(): void
{
$this->factory = new TaskRunnerFactory();
}
/** @test */
public function properlyCreatesService(): void
{
$loggerMock = $this->prophesize(LoggerInterface::class);
$logger = $loggerMock->reveal();
$containerMock = $this->prophesize(ContainerInterface::class);
$getLogger = $containerMock->get(LoggerInterface::class)->willReturn($logger);
$container = $containerMock->reveal();
$taskRunner = ($this->factory)($container, '');
$loggerProp = $this->getPropertyFromTaskRunner($taskRunner, 'logger');
$containerProp = $this->getPropertyFromTaskRunner($taskRunner, 'container');
$this->assertSame($container, $containerProp);
$this->assertSame($logger, $loggerProp);
$getLogger->shouldHaveBeenCalledOnce();
}
private function getPropertyFromTaskRunner(TaskRunner $taskRunner, string $propertyName)
{
$ref = new ReflectionObject($taskRunner);
$prop = $ref->getProperty($propertyName);
$prop->setAccessible(true);
return $prop->getValue($taskRunner);
}
}