ZF-Autoloader not working in UnitTests on Ubuntu - zend-framework

i got a problem regarding Unit-testing a Zend-Framework application under Ubuntu 12.04. The project-structure is a default zend application whereas the models are defined as the following
./application
./models
./DbTable
./ProjectStatus.php (Application_Model_DbTable_ProjectStatus)
./Mappers
./ProjectStatus.php (Application_Model_Mapper_ProjectStatus)
./ProjectStatus.php (Application_Model_ProjectStatus)
The Problem here is with the Zend-specific autoloading. The naming convention here appears that the folder Mappers loads all classes with _Mapper but not _Mappers. This is some internal Zend behavior which is fine so far.
On my windows machine the phpunit runs without any Problems, trying to initiate all those classes.
On my Ubuntu machine however with jenkins running on it, phpunit fails to find the appropriate classes giving me the following error
Fatal error: Class 'Application_Model_Mapper_ProjectStatus' not found
in /var/lib/jenkins/jobs/PAM/workspace/tests/application/models/Mapper/ProjectStatusTest.php
on line 39
The error appears to really be that the Zend-Autoloader doesn't load from the ubuntu machine, but i can't figure out how or why this works. The question remains of why this is. I think i've double checked every point of contact with the zend autoloading stuff, but i just can't figure this out. I'll paste the - from my point of view relevant snippets - and hope someone of you has any insight to this.
Jenkins Snippet for PHPUnit
<target name="phpunit" description="Run unit tests with PHPUnit">
<exec executable="phpunit" failonerror="true">
<arg line="--configuration '${basedir}/tests/phpunit.xml' --coverage-clover '${basedir}/build/logs/clover.xml' --coverage-html '${basedir}/build/coverage/.' --log-junit '${basedir}/build/logs/junit.xml'" />
</exec>
</target>
./tests/phpunit.xml
<phpunit bootstrap="./bootstrap.php">
... this shouldn't be of relevance ...
</phpunit>
./tests/bootstrap.php
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
Any help will be appreciated.

I actually think the problem is with your "models/Mappers" folder. It should be "models/mappers" (all lowercase), and would explain why it works on Windows and not Linux.
As you can see from the Zend_Application_Module_Autoloader class:
$this->addResourceTypes(array(
'dbtable' => array(
'namespace' => 'Model_DbTable',
'path' => 'models/DbTable',
),
'mappers' => array(
'namespace' => 'Model_Mapper',
'path' => 'models/mappers',
),
However, as per my previous answer, I still believe you will need to bootstrap the application for all the default resources to be added automatically

From the ZF Manual:
Create a Model and Database Table
Before we get started, let's consider something: where will these classes live, and how will
we find them? The default project we created instantiates an
autoloader. We can attach other autoloaders to it so that it knows
where to find different classes. Typically, we want our various MVC
classes grouped under the same tree -- in this case, application/ --
and most often using a common prefix.
Zend_Controller_Front has a notion of "modules", which are individual
mini-applications. Modules mimic the directory structure that the zf
tool sets up under application/, and all classes inside them are
assumed to begin with a common prefix, the module name. application/
is itself a module -- the "default" or "application" module. As such,
we'll want to setup autoloading for resources within this directory.
Zend_Application_Module_Autoloader provides the functionality needed
to map the various resources under a module to the appropriate
directories, and provides a standard naming mechanism as well. An
instance of the class is created by default during initialization of
the bootstrap object; your application bootstrap will by default use
the module prefix "Application". As such, our models, forms, and table
classes will all begin with the class prefix "Application_".
Since Zend_Application_Module_Autoloader is loaded by default, you should only need to bootstrap your application (you don't have to run the front controller) as per the example at the of this answer.
If you don't want to bootstrap your application, you could short circuit the resource loading by initialising Zend_Application_Module_Autoloader yourself:
$autoloader = new Zend_Application_Module_Autoloader();
As you can see from the code, the __construct of this class calls initDefaultResourceTypes(), with all the goodies you are looking for:
class Zend_Application_Module_Autoloader extends Zend_Loader_Autoloader_Resource
{
/**
* Constructor
*
* #param array|Zend_Config $options
* #return void
*/
public function __construct($options)
{
parent::__construct($options);
$this->initDefaultResourceTypes();
}
/**
* Initialize default resource types for module resource classes
*
* #return void
*/
public function initDefaultResourceTypes()
{
$basePath = $this->getBasePath();
$this->addResourceTypes(array(
'dbtable' => array(
'namespace' => 'Model_DbTable',
'path' => 'models/DbTable',
),
'mappers' => array(
'namespace' => 'Model_Mapper',
'path' => 'models/mappers',
),
'form' => array(
'namespace' => 'Form',
'path' => 'forms',
),
'model' => array(
'namespace' => 'Model',
'path' => 'models',
),
'plugin' => array(
'namespace' => 'Plugin',
'path' => 'plugins',
),
'service' => array(
'namespace' => 'Service',
'path' => 'services',
),
'viewhelper' => array(
'namespace' => 'View_Helper',
'path' => 'views/helpers',
),
'viewfilter' => array(
'namespace' => 'View_Filter',
'path' => 'views/filters',
),
));
$this->setDefaultResourceType('model');
}
}
To only bootstrap your application without running the front controller in tests/bootstrap.php:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
$config = array(
APPLICATION_PATH . '/configs/application.ini'
);
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
array('config' => $config)
);
$application->bootstrap();

Since the conventional Autoloader did not work, i tried to manually do what any Zend application would do. Here's the bootstrap.php that worked out in the end
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
require_once 'Zend/Loader/Autoloader/Resource.php';
$resources = new Zend_Loader_Autoloader_Resource(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH
));
$resources->addResourceType('form','forms','Form');
$resources->addResourceType('model','models','Model');
$resources->addResourceType('dbtable','models/DbTable','Model_DbTable');
$resources->addResourceType('mapper','models/Mappers','Model_Mapper');
The logic usually is what Zend should figure out on his own. And in fact it does so on my local development machine running on windows. On ubuntu however i need to become specific.
Would be interesting to know why that is. If someone can explain the reasoning to me, then I'll probably end up giving you the right answer ;)

Related

Zend 2 Plugin Manager cant find class

Im new to Zend 2 and are trying to migrate my Zend 1 project to Zend 2. I had an Acl plugin in my Zend 1 project that I shared with several apps using symlink. I thought now that I migrate to Zend 2 I'd create my own package in the Vendor folder. I downloaded the Skeleton project and tried to add my plugin as this:
in the vendor folder I create myname\commons\Acl and added a my Module.php
in myname\commons\Acl i created src\WebAcl\Controller\Plugin and a added WebAclPlugin.php with the namespace WebAcl\Controller\Plugin
In my myname\commons\Acl I created ./config and added module.config.php with the content
return array(
// added for Acl ###################################
' controller_plugins' => array(
'invokables' => array(
'WebAclPlugin' => 'WebAcl\Controller\Plugin\WebAclPlugin',
)
),
// end: added for Acl ###################################
);
When I run this I get:
Fatal error: Class 'WebAcl\Controller\Plugin\WebAclPlugin' not found in AbstractPluginManager.php on line 170
What am I doing wrong?
Edit: If I in my module specify the classmap it works
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
)
But if I use "autoload" it doesnt work
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
Edit 2: This solved the problem:
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' *.str_replace("\\", "/", __NAMESPACE__),*
),
),
Still Im trying to figure out what composer.phar actually does? See additional question:
Additional question: I read that I should add my namespace in composer.json and run composer.phar update, which adds it to auto_namespace. I did this, but do I need to when I specify it in my module? Sorry if my questions are stupied.
The plugin manager will try to load the class using new which will make the registered autoloaders try to load the class. If there isn't an autoloader that can load this class, then you'll get the fatal error.
You don't say if myname\commons\Acl is a ZF2 module or a composer loaded package.
If it's a composer package, then you need to add:
"autoload": {
"psr-4": {
"WebAcl\\": "myname/commons/Acl/src/WebAcl"
}
}
to composer.json and then run composer.phar dumpautoload.
If you want myname/commons/Acl to be a module, then you need to add a Module.php to myname/commons/Acl that looks like this:
<?php
namespace WebAcl;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
You now need to tell your application's ModuleManager to load this module in application.config.php:
Add 'WebAcl' to the list of 'modules'
Add the path to the module in the 'module_paths' key:
'module_paths' => array(
'WebAcl' => "./vendor/myname/commons/Acl",
'./module',
'./vendor',
),
You have to do this as there isn't a direct mapping from the namespace name (WebApi) to the path name on disk like there usually is in the ./modules directory.
The ModuleManager should now find your module and the autoloader should be able to autoload any file in the WebAcl namespace within vendor/myname/commons/Acl/src/WebApi.
Of course, the composer route is easier if you don't need any other features of a ZF2 Module.

Zend: Using forms in modules

I'm trying to use forms with modules, they should be stored inside the module. So at first my filestructure:
application/
(...other directories)
modules/
group/
controllers/
IndexController.php
(...controllers)
forms/
Create.php
views/
scripts/
(...view scripts)
Bootstrap.php
Within the IndexController, I'm trying to set the Form by
new Group_Form_Create()
and the class in Create.php is of course Group_Form_Create. I get the following error message:
Fatal error: Class 'Group_Form_Create' not found in (...)\application\modules\group\controllers\IndexController.php on line 380
The Bootstrap.php with the class Group_Bootstrap is just an empty class.
Actually, I'm using the default Zend structure, but it woun't work anyway. Any ideas wheres the problems or what could be a possible solution?
In my module bootstrap (APPLICATION_PATH/modules/group/Bootstrap.php), if use the following code:
//Loads the autoloader resources
$this->_moduleName = 'group';
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH ."/modules/".$this->_moduleName."/",
'namespace' => '',
'resourceTypes' => array(
//Tells the application where to find the forms
'form' => array(
'path' => 'forms/',
'namespace' => ucfirst($this->_moduleName).'_Form_'
),
//Tells the application where to find the models
'model' => array(
'path' => 'models/',
'namespace' => ucfirst($this->_moduleName).'_Model_'
)
)
));
I then call the forms or models like this:
$frm = new Group_Form_Create();
I use the same snippet in all my modules and I only change the value of the $this->_moduleName; each time.
Hope this helps !
It sounds like your module bootstraps are not being run. These are triggered by the module resource, which is loaded if you have:
resources.modules[] = ""
in your application.ini. So add this if it is not present.
Ideally, it should work out of box.
Add this in your bootstrap:
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Group_',
'basePath' => dirname(__FILE__),
));
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
return $autoloader;
}

Zend Framework - Best Way To Autoload From /Application/Api Folder

I'm trying to autoload Classes from within a folder contained in the application itself.
E.G.
/Application
|->Models
|->Custom
|->Object.php
Is this the best way to do it (from bootstrap.php)?
public function _initAutoLoad()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'custom' => array(
'path' => 'custom/',
'namespace' => 'Custom',
))
));
}
Meaning from within any controller, I can call:
$object = new Custom_Object();
If you're not intending to prefix the class names with the application namespace (default Application), I'd simply put this stuff in library, eg
library/
Custom/
Object.php -> class Custom_Object
then add your Custom namespace to the autoloader in configuration (application.ini)
autoloadernamespaces[] = "Custom_"
If your class represents some kind of service, you could use the built-in Service resource type which is automatically autoloaded
application/
services/
Object.php -> class Application_Service_Object
Looks like the solution I had is the best... I can find anyways...

Zend Form Custom Validation Path issues

The issue:
Plugin by name 'Spam' was not found in
the registry; used paths:
Zend_Validate_: Zend/Validate/
I have this on my bootstrap.php file (it's NOT a class):
include_once 'config_root.php';
set_include_path ( $PATH );
require_once 'Initializer.php';
require_once "Zend/Loader.php";
require_once 'Zend/Loader/Autoloader.php';
// Set up autoload.
$loader = Zend_Loader_Autoloader::getInstance ();
$loader->setFallbackAutoloader ( true );
$loader->suppressNotFoundWarnings ( false );
//resource Loader
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
$resourceLoader->addResourceType('validate', 'validators/', 'My_Validate_');
$loader->pushAutoloader($resourceLoader);
I've named a file called Spam.php like this:
application/validators/Spam.php
class My_Validate_Spam extends Zend_Validate_Abstract {
On the form class I have:
//HONEY POT
$this->addElement(
'text', 'honeypot', array(
'label' => 'Honeypot',
'required' => false,
'class' => 'honeypot',
'decorators' => array('ViewHelper'),
'validators' => array(
array(
'validate' => 'Spam'
)
)
)
);
With all this, I'm getting:
Plugin by name 'Spam' was not found in
the registry; used paths:
Zend_Validate_: Zend/Validate/
Why ?
Thanks a lot.
You have to add the directory where you have your custom validators to your form elements prefix path. For example:
$elementPrefixPaths =
array(
array(
array(
'prefix' => 'My_Validate_',
'path' => 'My/Validate', // 'application/validators' in your case
'type' => 'validate',
)
)
);
$form->addElementPrefixPaths($elementPrefixPaths);
// or, if your're inside the form,
// $this->addElementPrefixPaths($elementPrefixPaths)
// before any elements make use of the validator.
The 'path' should be in your include path. You have to do the same with your custom filters. Also there is a similar approach for custom decorators and elements (which use the method setPrefixPaths() instead).
Read more here.
Your path is 'application/validators', but it would be better to follow ZF convention on class naming and path mirroring; as such you should put your validator in a directory such as 'My/Validate' You should follow this convention on all custom ZF extensions you develop (filters, helpers, plugins, etc). It will make your life easier in the long run. Also, as a final suggestion, don't use "My_" as your classes prefix, use something more personal, such as "Mem_" (considering your nickname).

Unable to load \Doctrine\ORM\Configuration() class with Doctrine 2 classloader in cli-config.php file when using the doctrine CLI

I followed the steps in the following article in order to integrate Zend Framework 1.11 and Doctrine 2:
http://jeboy25.blogspot.com/2010/08/doctrine-2-and-zend-framework-110.html
And I have 3 questions about the article:
1-In the "SchemaToolClass" section i don't understand why the author includes schema_tool.php at the bottom of ZendProject/public/index.php file after :
$application->bootstrap()
->run();
2-when i execute the command "php doctrine orm:schema-tool:create" i have the following error message in the command line:
HP Stack trace:
PHP 1. {main}() /Library/WebServer/Documents/carlending/application/tools/doctrine:0
PHP 2. include() /Library/WebServer/Documents/carlending/application/tools/doctrine:7
PHP 3. require() /Library/WebServer/Documents/carlending/application/tools/doctrine.php:41
PHP 4. Doctrine\Common\ClassLoader->loadClass($className = uninitialized)
the error occurs in the cli-config.php file at the line '$config = new \Doctrine\ORM\Configuration();'
3-Can you explain why the author puts the doctrine generated proxies and models inside the domain folder. Isn't it better that they reside in the models folder like any other model class.
Sometimes i also see some programmers using a 'generated' folder inside models.
If you managed to make a working integration of Zend 1.x and Doctrine i would be very happy if you could also send me a working project that would very helpful.
Thanks for your help.
After reading "Obtaining the EntityManager" section in http://www.doctrine-project.org/docs/orm/2.1/en/tutorials/getting-started-xml-edition.html
I think you need the following three lines to bootstrap:
use Doctrine\ORM\Tools\Setup;
require_once 'Doctrine/ORM/Tools/Setup.php';
Setup::registerAutoloadPEAR();
I've got it to work a few weeks ago, here's my code. Doctrine 2 is really nice :)
In my bootstrap
/**
* Initialize auto loader of Doctrine
*
* #return Doctrine_Manager
*/
protected function _initDoctrine() {
$this->bootstrap('autoload');
require_once('Doctrine/Common/ClassLoader.php');
// Create the doctrine autoloader and remove it from the spl autoload stack (it adds itself)
require_once 'Doctrine/Common/ClassLoader.php';
$doctrineAutoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');
//$doctrineAutoloader->register();
spl_autoload_unregister($doctrineAutoloader);
$autoloader = Zend_Loader_Autoloader::getInstance();
// Push the doctrine autoloader to load for the Doctrine\ namespace
$autoloader->pushAutoloader($doctrineAutoloader, 'Doctrine');
$classLoader = new \Doctrine\Common\ClassLoader('Entities', realpath(__DIR__ . '/models/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', realpath(__DIR__ . '/../library/Doctrine/'), 'loadClass');
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Symfony');
$doctrineConfig = $this->getOption('doctrine');
$config = new \Doctrine\ORM\Configuration();
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$driverImpl = new Doctrine\ORM\Mapping\Driver\YamlDriver(APPLICATION_PATH . '/../configs/mappings/yaml');
//$driverImpl = $config->newDefaultAnnotationDriver($doctrineConfig['path']['entities']);
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(APPLICATION_PATH . '/../proxies');
$config->setProxyNamespace('App\Proxies');
$connectionOptions = array(
'driver' => $doctrineConfig['conn']['driv'],
'user' => $doctrineConfig['conn']['user'],
'password' => $doctrineConfig['conn']['pass'],
'dbname' => $doctrineConfig['conn']['dbname'],
'host' => $doctrineConfig['conn']['host']
);
$registry = Zend_Registry::getInstance();
$registry->entitymanager = $em;
return $em;
}
Schema etc
I use yaml as you seen above, I haven't looked through the tutorial but I've used the command line tool that works like a charm, my doctrine.php (located in APPLICATION/bin):
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path()
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/../configs/application.ini'
);
$application->getBootstrap()->bootstrap('doctrine');
require_once __DIR__ . '/../Bootstrap.php';
$em = $application->getBootstrap()->getResource('doctrine');
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em, APPLICATION_PATH . "/configs/mappings")
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
You first have to generate your entities:
Generate all the models without deleting, creates also annotations -
./doctrine orm:generate-entities ~/Public/my_app/application/models/ --regenerate-entities 0 --generate-annotations 1
then generate your schema
./doctrine orm:schema-tool:create --dump-sql
or
./doctrine orm:schema-tool:update --dump-sql
the proxies are not really a part of your models, it's just used by Doctrine internally so I've put it as a separate entity from the models folder but I guess it doesn't really matter:
./doctrine orm:generate-proxies ~/Public/my_app/proxies/
Remember to add write permissions to the proxies for apache group.
Hmm... guess I didn't quite explain Jeboy's solution but perhaps my code helps you get started, it took me a while but once it's up and running it works like a charm :)
PS Don't forget the "namespace Entities;" in each of your models (it should be generated automatically)