Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit tests to cover module, request factory, and response factory. #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

class Module
{
/**
* @return mixed[]
*/
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}

/**
* @return mixed[]
*/
public function getAutoloaderConfig()
{
return [
Expand All @@ -23,6 +29,11 @@ public function getAutoloaderConfig()
];
}

/**
* @param MvcEvent $e
* @return void
* @throws \Interop\Container\Exception\ContainerException
*/
public function onBootstrap(MvcEvent $e)
{
$application = $e->getApplication();
Expand All @@ -36,6 +47,7 @@ public function onBootstrap(MvcEvent $e)
/* @var $eventManager \Zend\EventManager\EventManager */
$eventManager = $application->getEventManager();

/** @var ModuleOptions $moduleOptions */
$moduleOptions = $serviceManager->get(ModuleOptions::class);
foreach ($moduleOptions->getListeners() as $service) {
/** @var ListenerAggregateInterface $listener */
Expand Down
28 changes: 28 additions & 0 deletions tests/Factory/RequestListenerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace NewRelicTest\Factory;

use NewRelic\Client;
use NewRelic\Factory\RequestListenerFactory;
use NewRelic\Listener\RequestListener;
use NewRelic\ModuleOptions;
use Zend\ServiceManager\ServiceManager;

/**
* @covers \NewRelic\Factory\RequestListenerFactory
*/
class RequestListenerFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateService()
{
$serviceLocator = new ServiceManager();
$serviceLocator->setService('NewRelic\Client', new Client());
$serviceLocator->setService('NewRelic\ModuleOptions', new ModuleOptions());

$factory = new RequestListenerFactory();

$listener = $factory->createService($serviceLocator);

self::assertInstanceOf(RequestListener::class, $listener);
}
}
28 changes: 28 additions & 0 deletions tests/Factory/ResponseListenerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace NewRelicTest\Factory;

use NewRelic\Client;
use NewRelic\Factory\ResponseListenerFactory;
use NewRelic\Listener\ResponseListener;
use NewRelic\ModuleOptions;
use Zend\ServiceManager\ServiceManager;

/**
* @covers \NewRelic\Factory\ResponseListenerFactory
*/
class ResponseListenerFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateService()
{
$serviceLocator = new ServiceManager();
$serviceLocator->setService('NewRelic\Client', new Client());
$serviceLocator->setService('NewRelic\ModuleOptions', new ModuleOptions());

$factory = new ResponseListenerFactory();

$listener = $factory->createService($serviceLocator);

self::assertInstanceOf(ResponseListener::class, $listener);
}
}
110 changes: 110 additions & 0 deletions tests/ModuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace NewRelicTest;

use NewRelic\Client;
use NewRelic\Module;
use NewRelic\ModuleOptions;
use Zend\EventManager\EventManager;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Mvc\ApplicationInterface;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceManager;

/**
* @covers \NewRelic\Module
*/
class ModuleTest extends \PHPUnit_Framework_TestCase
{
/** @var EventManager */
private $events;

/** @var ServiceManager */
private $services;

/** @var ApplicationInterface|\PHPUnit_Framework_MockObject_MockObject */
private $application;

/** @var ListenerAggregateInterface|\PHPUnit_Framework_MockObject_MockObject */
private $listener;

/** @var ModuleOptions */
private $options;

/** @var Client|\PHPUnit_Framework_MockObject_MockObject */
private $client;

/** @var Module */
private $module;

protected function setUp()
{
$this->client = $this->getMockBuilder(Client::class)->getMock();
$this->options = new ModuleOptions();
$this->listener = $this->getMockBuilder(ListenerAggregateInterface::class)->getMock();
$this->services = new ServiceManager();
$this->application = $this->getMockBuilder(ApplicationInterface::class)->getMock();
$this->application->method('getServiceManager')->willReturn($this->services);
$this->events = new EventManager();
$this->application->method('getEventManager')->willReturn($this->events);

$this->module = $module = new Module();
}

public function testGetConfig()
{
$config = $this->module->getConfig();

self::assertInternalType('array', $config);

// Assert there are no closures (which cannot be cached).
self::assertArraySubset($config, unserialize(serialize($config)));
}

public function testGetAutoLoaderConfig()
{
$config = $this->module->getAutoloaderConfig();

self::assertInternalType('array', $config);
}

public function testBootstrapBindsListeners()
{
$this->listener->expects(self::once())->method('attach');

$this->client->method('extensionLoaded')->willReturn(true);

$event = new MvcEvent();
$event->setApplication($this->application);

$this->services->setService(Client::class, $this->client);
$this->services->setService(ModuleOptions::class, $this->options);
$this->services->setService(ListenerAggregateInterface::class, $this->listener);

$this->options->setListeners([
ListenerAggregateInterface::class,
]);

$this->module->onBootstrap($event);
}

public function testBootstrapNotBindsListenersDueToMissingExtension()
{
$this->listener->expects(self::never())->method('attach');

$this->client->method('extensionLoaded')->willReturn(false);

$event = new MvcEvent();
$event->setApplication($this->application);

$this->services->setService(Client::class, $this->client);
$this->services->setService(ModuleOptions::class, $this->options);
$this->services->setService(ListenerAggregateInterface::class, $this->listener);

$this->options->setListeners([
ListenerAggregateInterface::class,
]);

$this->module->onBootstrap($event);
}
}