Commit 5d02b229 authored by Daniel Król's avatar Daniel Król

Basic integration tests

parent 6d5669bb
<?php
namespace Mleko\Narrator\Bundle\Tests\Integration;
use Mleko\Narrator\Bundle\Tests\Integration\TestApp\AppKernel;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\TestCase;
abstract class IntegrationTest extends TestCase
{
/**
* @var AppKernel
*/
protected $kernel;
/**
* @var vfsStreamDirectory
*/
protected $root;
protected function setUp()
{
/* force reload of ContainerClass */
$this->setRunTestInSeparateProcess(true);
$this->setPreserveGlobalState(false);
$this->root = vfsStream::setup('testRoot');
$cacheDir = vfsStream::newDirectory("cache", 0777);
$this->root->addChild($cacheDir);
$logDir = vfsStream::newDirectory("logs", 0777);
$this->root->addChild($logDir);
$this->kernel = new AppKernel("test", true);
$this->kernel->getContainerBuilder()->setParameter("kernel.secret", "secretChangeIt");
$this->kernel->setCacheDir($cacheDir->url());
$this->kernel->setLogDir($logDir->url());
$config = vfsStream::newFile("config.yml");
$this->root->addChild($config);
$this->kernel->setConfigPath($config->url());
}
}
<?php
namespace Mleko\Narrator\Bundle\Tests\Integration;
use Mleko\Narrator\EventEmitter;
use org\bovigo\vfs\vfsStream;
/**
* @runTestsInSeparateProcesses
*/
class SimpleEventListenerTest extends IntegrationTest
{
public function testDefaultEventBus()
{
$servicesConfig = vfsStream::newFile("services.xml");
$servicesConfig->setContent(<<<'EOF'
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service class="Mleko\Narrator\Bundle\Tests\Integration\TestApp\Counter" id="narrator.listener.counter">
<tag name="narrator.listener" event="stdClass"/>
</service>
</services>
</container>
EOF
);
$this->root->addChild($servicesConfig);
$this->kernel->setConfigPath($servicesConfig->url());
$this->kernel->boot();
$container = $this->kernel->getContainer();
$this->assertTrue($container->has("narrator.event_bus.default"));
$this->assertTrue($container->has("narrator.listener.counter"));
$container->get("narrator.event_bus.default")->emit(new \stdClass());
$this->assertEquals(1, $container->get("narrator.listener.counter")->getCount());
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
* @expectedExceptionMessageRegExp "You have requested a non-existent service \"narrator.event_bus.invalid\"\."
*/
public function testMissingEventBus()
{
$this->kernel->boot();
$container = $this->kernel->getContainer();
$container->get("narrator.event_bus.invalid");
}
public function testCustomEventBus()
{
$servicesConfig = vfsStream::newFile("config.yml");
$servicesConfig->setContent(<<<'EOF'
narrator:
event_bus:
default: ~
inheritance:
resolver:
type: instanceof
EOF
);
$this->root->addChild($servicesConfig);
$this->kernel->setConfigPath($servicesConfig->url());
$this->kernel->boot();
$container = $this->kernel->getContainer();
$this->assertTrue($container->has("narrator.event_bus.inheritance"));
/** @var EventEmitter $eventEmitter */
$this->assertNotNull($container->get("narrator.event_bus.inheritance"));
}
}
<?php
namespace Mleko\Narrator\Bundle\Tests\Integration\TestApp;
use Mleko\Narrator\Bundle\NarratorBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
class AppKernel extends Kernel
{
private $cacheDir;
private $configPath;
private $logDir;
public function registerBundles()
{
return [
new NarratorBundle()
];
}
public function setConfigPath($configPath)
{
$this->configPath = $configPath;
}
public function getLogDir()
{
return $this->logDir;
}
public function setLogDir($logDir)
{
$this->logDir = $logDir;
}
public function getCacheDir()
{
return $this->cacheDir;
}
public function setCacheDir($dir)
{
$this->cacheDir = $dir;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->configPath);
}
public function getContainerBuilder()
{
return parent::getContainerBuilder();
}
}
<?php
namespace Mleko\Narrator\Bundle\Tests\Integration\TestApp;
class Counter
{
private $count = 0;
private $lastEvent;
public function handle($event)
{
$this->count++;
$this->lastEvent = $event;
}
public function getCount()
{
return $this->count;
}
public function getLastEvent()
{
return $this->lastEvent;
}
}
......@@ -26,7 +26,7 @@ class ListenerServiceTest extends \PHPUnit_Framework_TestCase
$listener = new \Mleko\Narrator\Bundle\Listener\ListenerService('mock.listener', $this->container);
$event = new \stdClass();
$meta = new \Mleko\Narrator\Meta($event, 'strClass', 'stdClass', $this->getMockBuilder(\Mleko\Narrator\EventSource::class)->getMockForAbstractClass());
$meta = new \Mleko\Narrator\Meta($event, \stdClass::class, \stdClass::class, $this->getMockBuilder(\Mleko\Narrator\EventSource::class)->getMockForAbstractClass());
$listener->handle($event, $meta);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment