As we know most times integration testing can be one of the hardest things to set for an application depending on its complexity.
This is how simple Mezzio makes it. The following is a integration test using the actual configured Psr\Container to test if the save page command handler is handling the command. Its just a quick one that needs to be improved but the point is that it works. Most times just figuring out the approach is the biggest hurdle.
This is how simple Mezzio makes it. The following is a integration test using the actual configured Psr\Container to test if the save page command handler is handling the command. Its just a quick one that needs to be improved but the point is that it works. Most times just figuring out the approach is the biggest hurdle.
Code:
<?php
/**
* Test Type: Integration
*/
declare(strict_types=1);
namespace PageManagerTest\Storage;
use League\Tactician\CommandBus;
use PageManager\Storage\PageEntity;
use PageManager\Storage\SavePageCommand;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
final class SavePageCommandHandlerTest extends TestCase
{
/** @var ContainerInterface&MockObject */
protected $container;
/** @var CommandBus */
protected $commandBus;
/** @var array<string, int|string|null> $data */
protected $data = [
'title' => 'SavePageCommandHandlertest',
'description' => 'Created during test.',
];
protected function setUp(): void
{
$this->initContainer();
}
protected function initContainer(): void
{
$this->container = require __DIR__ . '/../../../config/container.php';
}
public function testSavePageCommandHandlerHandlesCommand(): void
{
$commandBus = $this->container->get(CommandBus::class);
$entity = new PageEntity(...$this->data);
$command = new SavePageCommand($entity);
$result = $commandBus->handle($command);
$this->assertInstanceOf(PageEntity::class, $result);
}
}
Last edited: