Skip to content

Instantly share code, notes, and snippets.

@Hounddog
Created July 2, 2012 10:35
Show Gist options
  • Save Hounddog/3032554 to your computer and use it in GitHub Desktop.
Save Hounddog/3032554 to your computer and use it in GitHub Desktop.
Zf2 Module Testing
<?php
namespace SiteModuleTest;
use Zend\Loader\AutoloaderFactory;
use ApplicationModuleTest\ServiceManagerTestCase;
$basePath = __DIR__ . '/../../../../';
chdir(__DIR__);
$previousDir = '.';
while (!file_exists('config/application.config.php')) {
$dir = dirname(getcwd());
if ($previousDir === $dir) {
throw new RuntimeException(
'Unable to locate "config/application.config.php":'
. ' is SiteModule in a subdir of your application skeleton?'
);
}
$previousDir = $dir;
chdir($dir);
}
if (
!(@include_once __DIR__ . '/../vendor/autoload.php')
&& !(@include_once $basePath . 'vendor/autoload.php')
) {
throw new RuntimeException(
'vendor/autoload.php could not be found.'
. ' Did you run `php composer.phar install`?'
);
}
if (!$config = @include __DIR__ . '/TestConfiguration.php') {
$config = require __DIR__ . '/TestConfiguration.php.dist';
}
AutoloaderFactory::factory(
array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
'ApplicationModuleTest' =>
$basePath .
'module/Application/test/ApplicationModuleTest',
__NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
)
)
)
);
ServiceManagerTestCase::setServiceManagerConfiguration(
require 'config/application.config.php'
);
/module
/Site
/test
/Integration
/Unit
/SiteModuleTest
/Service
ServiceTest.php
Bootstrap.php
TestConfiguration.php.dist
phpunit.xml
<phpunit
bootstrap="./Bootstrap.php"
colors="true"
backupGlobals="false"
>
<testsuites>
<testsuite name="SiteModule Unit Test Suite">
<directory>./</directory>
</testsuite>
</testsuites>
</phpunit>
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/
namespace ApplicationModuleTest;
use PHPUnit_Framework_TestCase as BaseTestCase;
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Service\ServiceManagerConfiguration;
/**
* Base test case to be used when a new service manager instance is required
*/
class ServiceManagerTestCase extends BaseTestCase
{
private static $_configuration = array();
public static function setServiceManagerConfiguration(array $configuration)
{
static::$configuration = $configuration;
}
public static function getServiceManagerConfiguration()
{
return static::$configuration;
}
public function getServiceManager(array $configuration = null)
{
$configuration = $configuration ?:
static::getServiceManagerConfiguration();
$serviceManager = new ServiceManager(
new ServiceManagerConfiguration($configuration['service_manager'])
);
$serviceManager->setService('ApplicationConfiguration', $configuration);
/* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */
$moduleManager = $serviceManager->get('ModuleManager');
$moduleManager->loadModules();
return $serviceManager;
}
}
<?php
namespace SiteModuleTest\Service;
use PHPUnit_Framework_TestCase as BaseTestCase;
class SiteTest extends BaseTestCase
{
public function testGet()
{
$service = new \Application\Service\RestService;
}
}
<?php
return array(
'modules' => array(
'DysBase',
'Application',
'Site',
),
'module_listener_options' => array(
'config_cache_enabled' => false,
'cache_dir' => 'data/cache',
'module_paths' => array(
__DIR__ .'/../../../module',
__DIR__ .'/../../../vendor',
),
),
'service_manager' => array(
'use_defaults' => true,
),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment