Unit Testing a Zend Framework 2 Module With Factory - php

I'm trying to Unit Test a ZF2 Module I've written, specifically, a service object.
But I'm getting stuck on how to get the service manager (which calls my factory object) into the test class properly. My factory object injects my modules entity object, the Doctrine entity manager, and my module's entity repository.
How do I ensure that the the factory is properly called during the Unit Test?

This is what I do in my bootstrap.php:
public static function init()
{
if (is_readable(__DIR__ . '/TestConfig.php')) {
$testConfig = include __DIR__ . '/TestConfig.php';
} else {
$testConfig = include __DIR__ . '/TestConfig.php.dist';
}
$zf2ModulePaths = array();
if (isset($testConfig['module_listener_options']['module_paths'])) {
$modulePaths = $testConfig['module_listener_options']['module_paths'];
foreach ($modulePaths as $modulePath) {
if (($path = static::findParentPath($modulePath)) ) {
$zf2ModulePaths[] = $path;
}
}
}
$zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
$zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService(
'ApplicationConfig',
$testConfig
);
$serviceManager->get('ModuleManager')->loadModules();
$serviceManager->setAllowOverride(true);
static::$serviceManager = $serviceManager;
}
public static function getServiceManager()
{
return static::$serviceManager;
}
And in your test class you can jus call Bootstrap::getServiceManager().

Related

getServiceLocator returning Null under PHPUnittests

I'm trying to test a simple controller that authenticates a user using the LdapAdapter and using the 'ldap' array from the configuration of the Application, but phpunit is returning the following error:
Fatal error: Uncaught Error: Call to a member function get() on null in /var/www/html/app/module/Auth/src/Auth/Controller/AuthController.php:53
Stack trace:
#0 /var/www/html/app/module/Auth/test/AuthTest/Controller/AuthControllerTest.php(37): Auth\Controller\AuthController->authenticate('myuser', 'mypassword')
#1 [internal function]: AuthTest\Controller\AlbumControllerTest->testLoginAction()
#2 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(863): ReflectionMethod->invokeArgs(Object(AuthTest\Controller\AlbumControllerTest), Array)
#3 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(741): PHPUnit_Framework_TestCase->runTest()
#4 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php(608): PHPUnit_Framework_TestCase->runBare()
#5 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestCase.php(697): PHPUnit_Framework_TestResult->run(Object(AuthTest\Controller\AlbumControllerTest))
#6 /var/www/html/vendor/phpunit/phpunit/src/Framework/TestSuite.php(733): PHPUnit_Framework_TestCase- in /var/www/html/app/module/Auth/src/Auth/Controller/AuthController.php on line 53
My Controller is the following:
<?php
namespace Auth\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Authentication\Adapter\Ldap as AuthAdapter;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Result;
use Auth\Form\AuthForm;
use Auth\Model\Auth;
class AuthController extends AbstractActionController
{
public function loginAction()
{
$form = new AuthForm();
$request = $this->getRequest();
if ($request->isPost()) {
$auth = new Auth();
$form->setInputFilter($auth->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()){
$auth->exchangeArray($form->getData());
$values = $form->getData();
$result = $this->authenticate($values['username'], $values['password']);
switch($result->getCode()) {
case Result::SUCCESS:
return $this->redirect()->toRoute('home');
break;
case Result::FAILURE:
break;
}
}
}
return array('form' => $form);
}
public function authenticate($username, $password){
$options = $this->getServiceLocator()->get('Config');
$authAdapter = new AuthAdapter($options['ldap'],
'username',
'password');
$authAdapter
->setIdentity($username)
->setCredential($password);
$auth = new AuthenticationService();
$result = $auth->authenticate($authAdapter);
return $result;
}
private function debug($var){
echo '<pre>';
var_dump($var);
echo '</pre>';
exit();
}
}
The TestCase:
namespace AuthTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
use Zend\Authentication\AuthenticationService;
use Auth\Controller\AuthController;
class AuthControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
public function setUp()
{
$this->setApplicationConfig(
include '/var/www/html/app/config/application.config.php'
);
parent::setUp();
}
public function testLoginAction()
{
#Basic Access to the page
$this->dispatch('/login');
$this->assertResponseStatusCode(200);
$data = array(
'identity' => 'myuser',
'credential' => 'mypassword',
);
$auth = new AuthController();
$auth->authenticate($data['identity'], $data['credential']);
$identity = new AuthenticationService();
$this->assertEquals($data['identity'], $identity->getIdentity());
}
}
PHPUnittest's BootStrap:
<?php
namespace AuthTest;
use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use RuntimeException;
error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);
/**
* Test bootstrap, for setting up autoloading
*/
class Bootstrap
{
protected static $serviceManager;
public static function init()
{
$zf2ModulePaths = array(dirname(dirname(__DIR__)));
if (($path = static::findParentPath('vendor'))) {
$zf2ModulePaths[] = $path;
}
if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) {
$zf2ModulePaths[] = $path;
}
static::initAutoloader();
// use ModuleManager to load this module and it's dependencies
$config = array(
'module_listener_options' => array(
'module_paths' => $zf2ModulePaths,
),
'modules' => array(
'Auth'
)
);
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService('ApplicationConfig', $config);
$serviceManager->get('ModuleManager')->loadModules();
static::$serviceManager = $serviceManager;
}
public static function chroot()
{
$rootPath = dirname(static::findParentPath('module'));
chdir($rootPath);
}
public static function getServiceManager()
{
return static::$serviceManager;
}
protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');
if (file_exists($vendorPath.'/autoload.php')) {
include $vendorPath.'/autoload.php';
}
if (! class_exists('Zend\Loader\AutoloaderFactory')) {
throw new RuntimeException(
'Unable to load ZF2. Run `php composer.phar install`'
);
}
AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
),
),
));
}
protected static function findParentPath($path)
{
$dir = __DIR__;
$previousDir = '.';
while (!is_dir($dir . '/' . $path)) {
$dir = dirname($dir);
if ($previousDir === $dir) {
return false;
}
$previousDir = $dir;
}
return $dir . '/' . $path;
}
}
Bootstrap::init();
Bootstrap::chroot();
In the functional tests it works as expected, but on php unittests the error occurs in the line 53 '$options = $this->getServiceLocator()->get('Config');'.
So, how can use or set ServiceLocator to work with phpunittests?
After so much struggling on this doubt and other tests which a controller that uses an ServiceLocator, I figure that the correct way to test a controllers Action is to Mock the object or/and his methods.
I don't feel totally comfortable with this solution, but for now, this is what it is.
An other feel is that mocking an behaviour of the code is something like that breaks the DRY principle: instead of use the real code I just create a mock that half behaves like it :S
Anyway, the following code does the job for me:
$authControllerMock = $this->getMockBuilder('Auth\Controller\AuthController')
->disableOriginalConstructor()
->getMock();
$authControllerMock->expects($this->any())
->method('authenticate')
->will($this->returnValue(true);
$serviceManager = $this->getApplicationServiceLocator();
$serviceManager->setAllowOverride(true);
$serviceManager->setService('Auth\Controller\Auth', $authControllerMock);
$this->dispatch('/usermgr');
self::assertMatchedRouteName('usermgr');
self::assertControllerClass('AuthController');
self::assertControllerName('auth\controller\auth');
self::assertResponseStatusCode('200');

OOP Based config file

I have a config file for my app but I am having trouble passing the object around as I need it to be available to my main index page and parent controller.
Currently I have to pass it as a parameter for the __Construct function in every controller I make, which certainly doesn't seem to be the best way to do it.
index.php
<?PHP
require 'config/conf.php';
$errors = array();
//Memcache the config file at some point.
define('DEBUG_MODE', 0);
define('SITE_KEY', '');
define('ROOT', 'http://manager.com/');
define('Vs', 'views/');
define('Cs', 'controllers/');
function __autoload($className) { // Autoload both controllers and models.
if (stristr($className, 'Model')) {
if (is_readable(Ms . $className . '.php')) {
include Ms . $className . '.php';
}
} else {
if (is_readable(Cs . $className . '.php')) {
include Cs . $className . '.php';
}
}
}
require 'libs/core/Controller.php';
require 'libs/core/View.php';
$Memcache = null;
if ($config['ADDITIONAL_LIBS']['MEMCACHED']) {
if (!class_exists('Memcache')) {
$errors[] = array('code' => 1, 'type' => 'error', 'title' => 'Memcached failed to load', 'msg' => 'Memcached is not installed or initialised properly.');
}
}
if ($config['SESSIONS']) {
require 'libs/core/Session.php';
}
if ($config['DATABASE']) {
define('Ms', 'models/');
require 'libs/core/Database.php';
require 'libs/core/Model.php';
}
if ($config['ADDITIONAL_LIBS']['UTIL']) {
if (file_exists('libs/extra/Util.php')) {
require 'libs/extra/Util.php';
} else {
$errors[] = loadFail('Util');
}
}
if ($config['ADDITIONAL_LIBS']['PBKDF2']) {
if (file_exists('libs/extra/PBKDF2.php')) {
require 'libs/extra/PBKDF2.php';
} else {
$errors[] = loadFail('PBKDF2');
}
}
if ($config['ADDITIONAL_LIBS']['MCAPI']) {
if (file_exists('libs/extra/MCAPI.php')) {
require 'libs/extra/MCAPI.php';
} else {
$errors[] = loadFail('MCAPI');
}
}
require 'libs/core/Router.php';
$Site = new Router($Config, $errors);
function loadFail($moduleName) {
return array('code' => 1, 'type' => 'error', 'title' => $moduleName . ' failed to load', 'msg' => $moduleName . '.php was not found in the "libs/extra/" directory.');
}
My conf file:
$Config = new Config();
$Config->set('HOME_PAGE', 'index');
$Config->set('MEMCACHE_ENABLE', true);
$Config->set('MEMCACHE_SERVERS', array(
array(
'SERVER' => 'localhost',
'PORT' => '11211'
)
));
class Config {
public $params;
function __construct() {
}
public function set($param, $value) {
$this->params[$param] = $value;
}
public function get($param) {
return $this->params[$param];
}
}
And my main controller class which I need the config object to be accesible from:
abstract class Controller {
public $view;
public $Memcache;
public function __construct($Config) {
// Autoload model if it exists...
$model = get_class($this) . 'Model';
if (is_readable(Ms . $model . '.php')) {
if ($Memcache) {
if (!$this->model = $Memcache->get($model)) {
$this->model = new $model;
}
}
}
$this->view = new View();
}
}
What is a slick and clean way to achieve what I want, which is basically to have a centralised configuration file who's parameters are available to both my main index file and parent controller?
Config is typically handled using the Registry pattern:
http://avedo.net/101/the-registry-pattern-and-php/
That's probably the most common/straightfoward way to do it. Some people prefer not to make config quite so global, in which case you would often use the Factory pattern and let the factory inject the config so that you don't have to explicitly do it with each instantiation.

Allow SVN commit with existing PreCommit hook warnings

I am using SVN precommit hooks to validate my code standard (PSR2) before being able to commit. This works perfectly with just one exception. My unit test (PHPUnit) files exist of my bootstrap class existing of all static unit test functions, but also enables error messages above the bootstrap class definition.
The PSR2 standard will give a warning when trying to commit this, because you cannot have code that is not in a class or function in a file that contains a class definition.
Does anyone have either a way to exclude this error in my codesniffer or a way to make my code valid (without putting the code to enable my error messages in each static function of the bootstrap class)?
Here's the file:
<?php
namespace AlbumTest;
use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;
error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);
class Bootstrap
{
protected static $serviceManager;
protected static $config;
protected static $bootstrap;
public static function init()
{
// Load the user-defined test configuration file, if it exists; otherwise, load
if (is_readable(__DIR__ . '/TestConfig.php')) {
$testConfig = include __DIR__ . '/TestConfig.php';
} else {
$testConfig = include __DIR__ . '/TestConfig.php.dist';
}
$zf2ModulePaths = array();
if (isset($testConfig['module_listener_options']['module_paths'])) {
$modulePaths = $testConfig['module_listener_options']['module_paths'];
foreach ($modulePaths as $modulePath) {
if (($path = static::findParentPath($modulePath)) ) {
$zf2ModulePaths[] = $path;
}
}
}
$zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
$zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS')
? ZF2_MODULES_TEST_PATHS : '');
static::initAutoloader();
// use ModuleManager to load this module and it's dependencies
$baseConfig = array(
'module_listener_options' => array(
'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
),
);
$config = ArrayUtils::merge($baseConfig, $testConfig);
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService('ApplicationConfig', $config);
$serviceManager->get('ModuleManager')->loadModules();
static::$serviceManager = $serviceManager;
static::$config = $config;
}
public static function getServiceManager()
{
return static::$serviceManager;
}
public static function getConfig()
{
return static::$config;
}
protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');
if (is_readable($vendorPath . '/autoload.php')) {
$loader = include $vendorPath . '/autoload.php';
} else {
$zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH')
? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library')
? $vendorPath . '/ZF2/library' : false));
if (!$zf2Path) {
throw new RuntimeException(
'Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.'
);
}
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
}
AutoloaderFactory::factory(
array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
),
),
)
);
}
protected static function findParentPath($path)
{
$dir = __DIR__;
$previousDir = '.';
while (!is_dir($dir . '/' . $path)) {
$dir = dirname($dir);
if ($previousDir === $dir) {
return false;
}
$previousDir = $dir;
}
return $dir . '/' . $path;
}
}
Bootstrap::init();
How does your precommit-hook look like? Could you just remove the first few lines dynamically before sending the to the Codesniffer?
Another solution would be to set the error_reporting in the init of your bootstrap

php namespace and autoload

I have some class
/library/QPF/Loader.php
namespace QPF;
class Loader
{
protected static $loader = null;
public function __construct()
{
spl_autoload_register('QPF\Loader::_autoload');
}
public static function init()
{
if (null === self::$loader) {
self::$loader = new Loader();
}
return self::$loader;
}
public function _autoload($class)
{
//if (class_exists($class)) return true;
$classFile = str_replace('\\', '/', $class) . '.php';
require_once $classFile;
if (!class_exists($class)) throw new Extension('Not found class');
}
}
/library/Version.php
namespace QPF;
class Version
{
public function getVersion()
{
return '0.1';
}
}
/public/index.php
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/../library');
define('APPLICATION_PATH', dirname(__FILE__) . '/../application');
require_once 'QPF/Loader.php';
QPF\Loader::init();
echo 'start';
use QPF;
$v = new QPF\Version();
var_dump($v);
echo 'ss';
Version class loading, but var_dump show what it's empty class without function getVersion();
startobject(QPF\Version)#2 (0) { } ss
Methods do not show up in var_dump or print_r output, as they are not part of the state of the object. Try calling the method; it should work as expected.

Issue with Zend framework controller

I am a newbie to the Zend framework.
I am getting an error while loading my index controller:
Fatal error: Class 'Places' not found in C:\xampp\htdocs\zend\book\application\controllers\IndexController.php on line 36
My bootstrapper code is
<?php
class Bootstrap
{
public function __construct($configSection)
{
$rootDir = dirname(dirname(__FILE__));
define('ROOT_DIR', $rootDir);
set_include_path(get_include_path(). PATH_SEPARATOR . ROOT_DIR . '/library/'. PATH_SEPARATOR . ROOT_DIR .
'/application/models/');
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
// Load configuration
Zend_Registry::set('configSection',$configSection);
$config = new Zend_Config_Ini(ROOT_DIR.'/application/config.ini',$configSection);
Zend_Registry::set('config', $config);
date_default_timezone_set($config->date_default_timezone);
// configure database and store to the registry
$db = Zend_Db::factory($config->db);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Registry::set('db', $db);
}
public function configureFrontController()
{
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory(ROOT_DIR .'/application/controllers');
}
public function runApp()
{
$this->configureFrontController();
// run!
$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();
}
}
I have a model:
<?php
class Places extends Zend_Db_Table
{
protected $_name = 'places'; //table name
function fetchLatest($count = 10)
{
return $this->fetchAll(null,'date_created DESC', $count);
}
}
My index controller code is:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->title = 'Welcome';
$placesFinder = new Places();
$this->view->places = $places->fetchLatest();
}
}
I am using ZF version 1.10.4
There is a good chance you are missing somethign in your class declaration
try:
<?php
class Models_Places extends Zend_Db_Table
{
protected $_name = 'places'; //table name
function fetchLatest($count = 10)
{
return $this->fetchAll(null,'date_created DESC', $count);
}
}
The Zend autoloader class will look into Models/places.php for your class.
Also you could initialise the models and default module in bootstrap with:
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => dirname(__FILE__),
));
$autoloader->addResourceType('models', 'models/', 'Models');
return $autoloader;
}
After having done that your class should be named Models_Places.
Check out the docs about autoloading.
Well, personally, I use extended controllers which contain few util methods I use very often. Here is a snippet of my extended controller:
<?php
class My_MyController extends Zend_Controller_Action
{
protected $_tables = array();
protected function _getTable($table)
{
if (false === array_key_exists($table, $this->_tables)) {
include APPLICATION_PATH . '/modules/'
. $this->_request->getModuleName() . '/models/' . $table . '.php';
$this->_tables[$table] = new $table();
}
return $this->_tables[$table];
}
}
You just need to define the APPLICATION_PATH in index.php. Then your controller could look like this:
<?php
class IndexController extends My_MyController
{
public function indexAction()
{
// get model
$model = $this->_getTable('ModelName');
}
}
Path where you store the My_Controller must also be in your include path.

Categories