Include module path phpunit - php

I have a problem including my module for my unit test, I have tried some things but nothings works
I have a directory src with my module and a directory test with bootstrap.php,phpunit.xml and my tests
Using set_include_path:
set_include_path(implode(PATH_SEPARATOR, array(
'.',
__DIR__ . '/path/to/mymodule',
get_include_path()
)));
adding with loader:
$loader->add('mymodule', '/path/to/mymodule');
And when I use get_include_path() I can see the path to my module but in my test he can't find classes from my module

Related

PHP Autoloader doesn't work on Ubuntu production server

I have to environments i'm working developing my API (PHP based):
Local development: Mac OS Yosemite - running PHP 5.5.20
Production server: Ubuntu server - running PHP 5.5.9
My code uses composer for auto loading as followed:
{
"require": {
"facebook/php-sdk": "#stable",
"everyman/neo4jphp": "dev-master",
"predis/predis": "1.0.1",
"aws/aws-sdk-php": "2.*"
},
"autoload": {
"psr-0": {
"PicoCore\\": "",
"PicoCore\\Authentication\\" : "PicoCore/authentication",
"PicoCore\\Aws\\" : "PicoCore/aws",
"PicoCore\\Cache\\" : "PicoCore/cache",
"PicoCore\\Database\\" : "PicoCore/database",
"PicoCore\\Database\\Managers\\" : "PicoCore/database/managers",
"PicoCore\\Facebook\\" : "PicoCore/facebook",
"PicoCore\\Objects\\" : "PicoCore/objects",
"PicoCore\\Rest\\" : "PicoCore/rest",
"PicoCore\\Configuration\\" : "PicoCore/configuration",
"PicoCore\\Configuration\\Api\\" : "PicoCore/configuration/api",
"PicoCore\\Configuration\\PictureReceiver\\" : "PicoCore/configuration/pictureReceiver",
"PicoCore\\Configuration\\PictureUploader\\" : "PicoCore/configuration/pictureUploader",
"PicoCore\\Scripts\\" : "PicoCore/scripts",
"PicoCore\\Times" : "PicoCore/times"
}
}
}
This is my loading function:
// autoload_namespaces.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'PicoCore\\Times' => array($baseDir . '/PicoCore/times'),
'PicoCore\\Scripts\\' => array($baseDir . '/PicoCore/scripts'),
'PicoCore\\Rest\\' => array($baseDir . '/PicoCore/rest'),
'PicoCore\\Objects\\' => array($baseDir . '/PicoCore/objects'),
'PicoCore\\Facebook\\' => array($baseDir . '/PicoCore/facebook'),
'PicoCore\\Database\\Managers\\' => array($baseDir . '/PicoCore/database/managers'),
'PicoCore\\Database\\' => array($baseDir . '/PicoCore/database'),
'PicoCore\\Configuration\\PictureUploader\\' => array($baseDir . '/PicoCore/configuration/pictureUploader'),
'PicoCore\\Configuration\\PictureReceiver\\' => array($baseDir . '/PicoCore/configuration/pictureReceiver'),
'PicoCore\\Configuration\\Api\\' => array($baseDir . '/PicoCore/configuration/api'),
'PicoCore\\Configuration\\' => array($baseDir . '/PicoCore/configuration'),
'PicoCore\\Cache\\' => array($baseDir . '/PicoCore/cache'),
'PicoCore\\Aws\\' => array($baseDir . '/PicoCore/aws'),
'PicoCore\\Authentication\\' => array($baseDir . '/PicoCore/authentication'),
'PicoCore\\' => array($baseDir . '/'),
'Guzzle\\Tests' => array($vendorDir . '/guzzle/guzzle/tests'),
'Guzzle' => array($vendorDir . '/guzzle/guzzle/src'),
'Everyman\\Neo4j' => array($vendorDir . '/everyman/neo4jphp/lib'),
'Aws' => array($vendorDir . '/aws/aws-sdk-php/src'),
);
When i'm running my code on my local development - it works perfectly, so I pushed it to my remote repository and pull it from the production server.
When i'm trying to run my code in my Production server I receive a Class not found error indicating that my autoloading didn't run properly.
Any ideas what could be the reason ?
UPDATE:
When i'm trying to load the external libs (like AWS) it does work, so the problem is defiantly something with how my folders are being loaded.
UPDATE:
This is the class i'm trying to load from index.php:
<?php
//require Pico Core autoload
require 'core/vendor/autoload.php';
//require Pico Api autoload
require 'vendor/autoload.php';
use PicoCore\Rest\ApiInitializer;
use PicoCore\Configuration\Error;
use PicoApi\Managers\ApiManager;
try {
//initialize the API
ApiInitializer::initialize();
//initialize a new Api with the request
$api = new ApiManager($_REQUEST['request']);
echo utf8_encode($api->processApi());
} catch (Exception $e) {
echo json_encode(Array(Error::jsonErrorField() => $e->getMessage()));
}
This is the error I receive Class 'PicoCore\Rest\ApiInitializer' not found
The structure is:
api
|
core
|
PicoCore
|
folders..
rest
|
ApiInitializer.php
folders...
vender
PicoApi (folder)
vendor (foldeR)
index.php
It's your wrong autoloading definition.
You want to use the class
PicoCore\Rest\ApiInitializer
And you have these files:
api/index.php
api/vendor/... (with your required external dependencies)
api/composer.json (with the autoloading mentioned in your question)
api/core/PicoCore/rest/ApiInitializer.php
And you have this autoloading configured:
"autoload": {
"psr-0": {
"PicoCore\Rest\" : "PicoCore/rest"
}
}
This is a mismatch. When you use the class PicoCore\Rest\ApiInitializer, composer will try to locate an entry with a prefix of that class. It will sucessfully detect that classes with prefix PicoCode\Rest can be found via PSR-0 rules in the directory (relative to the position of composer.json) PicoCore/rest.
First strange thing: There is an additional directory named "core" here, and you seem to include TWO autoloaders. Don't do that, Composer works best with only one autoloader.
Second thing: If I ignore that "core" directory for a bit, the PSR-0 rules state that the classname will be converted to a path - and then be searched in the path given for the prefix.
PicoCore\Rest\ApiInitializer as a path is PicoCore/Rest/ApiInitializer.php (note the uppercase "R" in "Rest"), and the path this is to be found is PicoCore/rest/PicoCore/Rest/ApiInitializer.php.
You don't have this file. And thats why Composer cannot find and load it. And this is even without counting this "core" directory level, it will also not be found if you used two composer.json files, one in api and one in api/core, and the one in api/core was used to find the class.
Suggestions:
Use PSR-4 for every class that is inside a namespace.
Don't lowercase namespace or classname parts for the filesystem.
Shorten your autoloading definition. If you'd rename all those lowercase directories below PicoCore into the proper uppercase variants that are being used in your namespace, you will only need one line of autoloading definition, defining the PicoCore prefix. The rest is done by PSR-4 (or PSR-0).
MacOS is using a case insensitive file system by default, and it is considered a bad thing to switch it to case sensitive: https://apple.stackexchange.com/questions/71357/how-to-check-if-my-hd-is-case-sensitive-or-not That's why it is working on Mac, but not Linux.

How to call GetId3 classes within a namespaced application

I am sorry if this has once been asked but I lack understanding on how this should work.
I am building a File Server to manage uploading and downloading of music files and have found getID3 library that I can use to get information about the files being uploaded.
I have installed the library via composer. Below is my entry on composer.json in my root director
"james-heinrich/getid3": "dev-master"
Vendor folder was successfully updated with the library.
The library has not been added onto autoload_namespaces.php file in vendor/composer (see below):
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Symfony\\Component\\Filesystem\\' => array($vendorDir . '/symfony/filesystem'),
'Psr\\Log\\' => array($vendorDir . '/psr/log'),
'GetId3_' => array($vendorDir . '/phansys/getid3'),
);
Also under autoload_psr4.php file in vendor/composer the namespace for the library has not been registered either. I am not sure if this would be the case. Below is the content of the file:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
'FileServer\\' => array($baseDir . '/src'),
);
Question(s)
How can I access the class libraries from getID3? Is there any special operation that I need to perform to make these classes available to me?
NB: The demos folder of the library does not use namespaces but rather "require" to include class files and that in my understanding forfeits the point of composer and namespaces. Resorting to Phansys Symdony library didnt help at all as I was not even sure if the library can be used on standalone projects.
Thanking you in advance.
Command composer dump-autoload is the answer thanks to #Frank.

Ubuntu + Netbeans - Zend framework fatal error due to Include path

I'm experiencing a problem with my 'include' path on my fresh install of Zend framework. i'm running Ubuntu and using Netbeans. I installed Zend via the terminal and I have copied the appropriate files to my new project.
I am currently getting this while attempting to access localhost/demoZend/public
Warning: require_once(Zend/Application.php): failed to open stream: No such file or directory in /home/mastered/public_html/demoZend/public/index.php on line 18
Fatal error: require_once(): Failed opening required 'Zend/Application.php' (include_path='/home/mastered/public_html/demoZend/library:.:/opt/lampp/lib/php') in /home/mastered/public_html/demoZend/public/index.php on line 18
I have copied the 'Zend' folder with the framework into my project/library folder so the folder structure is /demoZend/library/Zend/
I have also changed the 'include' path in my php.ini from the advice of a tutorial. however this is where I have things confused.
The path reads:
include_path = ".:/usr/share/php:/usr/share/php/libzend-framework-php/"
in my apache2/php.ini aswell as my /etc/php5/cli/php.ini file.
My index.php file in my project reads as thus:
<?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') : 'production'));
// 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->bootstrap()
->run();
I haven't changed that as the file paths seem ok.
I should also point out I have my web documents stored in my '~/public_html' directory, I have checked the permissions on my 'demoZend/library' folder and they have all restrictions off.
It sounds like you're following a tutorial for Zend Framework 1, but you've downloaded Zend Framework 2. There is no Zend/Application.php in ZF2, so that's why PHP isn't finding the file.
You can either head back to the ZF downloads page and grab the latest release of ZF1 (currently 1.12.3), or find a ZF2 tutorial and learn ZF2 instead. If you're just getting started with ZF, I'd go with the latter option.

PHPUnit & Zend Framework

I'm new to unit tests, i have been working on this tutorial i found on internet :
http://blog.fedecarg.com/2008/12/27/testing-zend-framework-controllers/
My problem is i simply can't execute the tests displayed in the tutorial!
C
:\wamp\www\portailmg\dev\tests>phpunit PHPUnit 3.7.21 by Sebastian
Bergmann.
Configuration read from C:\wamp\www\portailmg\dev\tests\phpunit.xml
Time: 0 seconds, Memory: 4.00Mb
No tests executed!
Generating code coverage report in HTML format ... done
C:\wamp\www\portailmg\dev\tests>
My bootstrap.php which is the only file i edited because i had the following error :
Notice: Zend_Loader::Zend_Loader::registerAutoload is deprecated as of
1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead in /www/zf-tutorial/library/Zend/Loader.php
I tried to fix this with that :
This is because you have the lines:
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
(or similar) somewhere in your bootstrap system.
The easiest solution is to change them to:
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('App_');
Where 'App_' is the name of a directory on your include path that has classes within it that follow the Zend Framework naming convention, so change it as appropriate and add more if you need them.
My bootstrap:
<?php
error_reporting( E_ALL | E_STRICT );
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
date_default_timezone_set('Europe/London');
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../applications'));
define('APPLICATION_ENV', 'loc');
define('LIBRARY_PATH', realpath(dirname(__FILE__) . '/../library'));
define('TESTS_PATH', realpath(dirname(__FILE__)));
$_SERVER['SERVER_NAME'] = 'http://localhost';
$includePaths = array(LIBRARY_PATH, get_include_path());
set_include_path(implode(PATH_SEPARATOR, $includePaths));
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('LIBRARY_PATH');
Zend_Session::$_unitTestEnabled = true;
Zend_Session::start();
?>
Thanks in advance for your help
Your setup looks okay, although if I remember correctly zend framework 1 will only work with >=3.5.x so maybe downgrading from 3.7 to 3.5 may do the trick. Do ensure your phpunit.xml file is setup properly and pointing to the tests bootstrap and not your application bootstrap. Also be sure to be following the unit testing name conventions. See http://phpunit.de/manual/3.5/en/index.html

Moving index.php out of application root in zend frame work

I am trying to change the path of index.php.
I have a boot strap zend structure.
So I created a new folder named newapp, its path is root path where our index.php lies.
Then I copied the index.php to newapp so that I can load the application from newapp directory.
But problem is that I can't load configuration file from index.php that I copied. It thorws error which is given below.
Fatal error: Uncaught exception 'Zend_Config_Exception' with message
'parse_ini_file(E:\xampp\htdocs\..../configs/application.ini) [<a href='function.parse-
ini-file'>function.parse-ini-file</a>]: failed to open stream: No such file or directory'
in E:\xampp\htdocs\ZendFramework-1.12.0\library\Zend\Config\Ini.php:182
Note my folder structure
- trunk
- index.php
- application
- bootstrap.php
- configs
- config.ini
- newapp
- index.php (copy of above)
My actual index.php is given below
<?php
ini_set('memory_limit', '-1');
set_time_limit(0);
defined('BASE_PATH')|| define('BASE_PATH', realpath(dirname(__FILE__)));
define('APPLICATION_PATH', BASE_PATH . '/application');
set_include_path('../../library1.12.0/'. get_include_path());
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
defined('APPLICATION_ENV')|| define('APPLICATION_ENV',(getenv
('APPLICATION_ENV') ? getenv('APPLICATION_ENV'): 'staging_mysql'));
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
$application->run();
What should I change in index.php ?
I tried change the path of config file as
APPLICATION_PATH . '/../../configs/application.ini'
but didn't work
- Zend library is available beacuse I have set path variable to it from
enviornment variable settings of windows.
I tried this also realpath(dirname(__FILE__))."/.."
But application is not loaded.
try adding
set_include_path(
APPLICATION_PATH.'/../library1.12.0'.PATH_SEPARATOR.
APPLICATION_PATH.'/../library1.12.0/Zend'
);
defined('BASE_PATH') || define('BASE_PATH', realpath(dirname(__FILE__)));
define('APPLICATION_PATH', BASE_PATH . '/application');
set_include_path('../../library1.12.0/'. get_include_path());
Should be this in newapp/index.php with the lines below
defined('BASE_PATH')|| define('BASE_PATH', realpath(dirname(__FILE__)));
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', BASE_PATH . '/../../application');
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library1.12.0'),
get_include_path()
)));
NOTE:
folder 'library1.12.0' shoould be in application folder level and Zend folder should be inside it. Hope this will help you, i have tested it and its working.

Categories