Skip to content

Commit

Permalink
Add unit tests and fix error if Zend\Db\Adapter\Adapter is defined in…
Browse files Browse the repository at this point in the history
… service manager but not configured
  • Loading branch information
snapshotpl committed Apr 22, 2015
1 parent 2b316a8 commit 3880403
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/nbproject/
*.phar
vendor/
composer.lock
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@
"maximebf/debugbar": "1.*",
"zendframework/zendframework": "2.*"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-0": {
"ZfSnapPhpDebugBar": "src/"
},
"classmap": [
"./Module.php"
]
},
"autoload-dev": {
"psr-4": {
"ZfSnapPhpDebugBar\\Tests\\": "tests/"
}
}
}
2 changes: 2 additions & 0 deletions config/zfsnapphpdebugbar.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Enables/disables PHP Debug Bar
'enabled' => true,

'auto-append-assets' => true,

// ServiceManager keys to inject collectors
// http://phpdebugbar.com/docs/data-collectors.html
'collectors' => array(),
Expand Down
8 changes: 6 additions & 2 deletions src/ZfSnapPhpDebugBar/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ public function onBootstrap(EventInterface $e)
$application = $e->getApplication();
$serviceManager = $application->getServiceManager();
$config = $serviceManager->get('config');
$request = $application->getRequest();
$debugbarConfig = $config['php-debug-bar'];

if ($config['php-debug-bar']['enabled'] !== true) {
if ($debugbarConfig['enabled'] !== true || !($request instanceof \Zend\Http\PhpEnvironment\Request)) {
return;
}
$applicationEventManager = $application->getEventManager();
Expand All @@ -102,7 +104,9 @@ public function onBootstrap(EventInterface $e)
require __DIR__ . '/Functions.php';

// Auto enable assets
$viewRenderer->plugin('DebugBar')->appendAssets();
if ($debugbarConfig['auto-append-assets']) {
$viewRenderer->plugin('DebugBar')->appendAssets();
}

// Timeline
$measureListener = function(EventInterface $e) use ($timeCollector, &$lastMeasure) {
Expand Down
2 changes: 1 addition & 1 deletion src/ZfSnapPhpDebugBar/Service/PhpDebugBarFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function createService(Locator $serviceLocator)
$debugbar->addCollector(new ConfigCollector($appConfig, 'ApplicationConfig'));

// Db profiler
if ($serviceLocator->has('Zend\Db\Adapter\Adapter')) {
if ($serviceLocator->has('Zend\Db\Adapter\Adapter') && isset($config['db']['driver'])) {
$adapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
$this->prepareDbAdapter($adapter, $debugbar);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Functional/DbCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ZfSnapPhpDebugBar\Tests\Functional;

/**
* DbCollectorTest
*
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
class DbCollectorTest extends \Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase
{
protected function setUp()
{
$this->setApplicationConfig(include __DIR__.'/../assets/application.config.php');
}

public function testDbIsNotConfigured()
{
$this->dispatch('/');

$this->assertResponseStatusCode(200);
}
}
18 changes: 18 additions & 0 deletions tests/Functional/DummyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ZfSnapPhpDebugBar\Tests\Functional;

use Zend\Mvc\Controller\AbstractActionController;

/**
* DummyController
*
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
class DummyController extends AbstractActionController
{
public function indexAction()
{
return $this->getResponse();
}
}
12 changes: 12 additions & 0 deletions tests/assets/application.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return array(
'modules' => array(
'ZfSnapPhpDebugBar',
),
'module_listener_options' => array(
'config_glob_paths' => array(
__DIR__.'/local.config.php',
),
),
);
31 changes: 31 additions & 0 deletions tests/assets/local.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

return array(
'controllers' => array(
'invokables' => array(
'ZfSnapPhpDebugBar\Tests\Functional\DummyController' => 'ZfSnapPhpDebugBar\Tests\Functional\DummyController',
),
),
'php-debug-bar' => array(
'auto-append-assets' => false,
),
'router' => [
'routes' => [
'home' => [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/',
'defaults' => [
'controller' => 'ZfSnapPhpDebugBar\Tests\Functional\DummyController',
'action' => 'index',
],
],
],
],
],
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require_once '../vendor/autoload.php';
7 changes: 7 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="bootstrap.php" colors="true">
<testsuite name="ZfSnapPhpDebugBar">
<directory>./</directory>
</testsuite>
</phpunit>

0 comments on commit 3880403

Please sign in to comment.