Welcome to Admin Junkies, Guest — join our community!

Register or log in to explore all our content and services for free on Admin Junkies.

PHP Integration testing in Mezzio

Tyrsson

Retired Staff
Retired Staff
Joined
Apr 29, 2023
Messages
402
Website
github.com
Credits
1,098
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.

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:

Log in or register to unlock full forum benefits!

Log in or register to unlock full forum benefits!

Register

Register on Admin Junkies completely free.

Register now
Log in

If you have an account, please log in

Log in
Activity
So far there's no one here

Users who are viewing this thread

Would You Rather #9

  • Start a forum in a popular but highly competitive niche

    Votes: 5 21.7%
  • Initiate a forum within a limited-known niche with zero competition

    Votes: 18 78.3%
Win this space by entering the Website of The Month Contest

Theme editor

Theme customizations

Graphic Backgrounds

Granite Backgrounds