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
Related
I'm starting to setup PHPUnit (v4.8) to be used in my 'legacy' code (it's not so legacy, but it have bad programming practices).
The structure of my folders is as follows
/home
-phpunit.xml
/folder1
/folder2
/folder3
/vendor
/tests
-Test1.php
/includes
-functions.php
/libs
-User.php
-TableClass.php
....
functions.php
<?php
//require_once $_SERVER['DOCUMENT_ROOT'] . "/home/vendor/autoload.php" ;
require_once $_SERVER['DOCUMENT_ROOT'] . "/home/includes/libs/table_classes/User.php" ;
?>
I have commented that line, because I think composer automatically loads it. Question 1, Am I Rigth? (because phpunit get automatically recognized inside my Test class...)
Test1.php
<?php
class Test1 extends PHPUnit_Framework_TestCase
{
public function testSomething()
{
// $something = getColNameByStatusId(1);
$this->assertEquals(1,2);
}
}
?>
phpunit.xml
<phpunit bootstrap="includes/functions.php" colors="true">
<testsuite name="Test1" >
<directory>./tests</directory>
</testsuite>
</phpunit>
Then I Execute phpunit in command line
My functions.php works fine in my code, of course with no composer integration, but when It's loaded with phpunit it 'breaks', I get the following error:
Warning: require_once(/home/includes/libs/table_classes/User.php): failed to open stream: No such file or directory in C:\wamp\www\home\includes\functions.php on line 18
I think I'm missing the 'loading' stuff for phpunit. My code doesn't use namespaces and PSR-0 neither PSR-4.
Question 2- How to properly load files in this case?
My goal is to load functions.php then it will load all other 'table' classes for doing my tests
Replace $_SERVER['DOCUMENT_ROOT'] with __DIR__ and adjusted the paths accordingly, and everything worked fine.
PHPUnit does not set $_SERVER['DOCUMENT_ROOT'] so It was not finding my files. Apache's do that. So the CLI of PHPUnit couldn't find it.
Hope it helps someone else.
I think it is better to start using PHPUnit by running
phpunit --generate-configuration
and follow some simple questions.
To autoload 'functions.php' and other table 'classes', you may try via composer.json autoload.
"autoload": {
"psr-4": {
"Model\\": "libs/"
}
}
Here is the link with autoload for your reference.
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
Can someone provide an example of how to unit test a Joomla 2.5 component? I'm working through this joomla mvc component example, which doesn't include unit tests, and I can't find a full example anywhere. My main questions are:
Where to put component test code
How to run component unit tests
Is my test code correct
The Joomla documentation seems incomplete and fragmented - I've read what I can find on unit testing (can't post links due to low rep), which seem to be about testing Joomla itself rather than extensions. The closest I've found is this, which I've based my dummy test on.
What I've done so far
Component directory structure:
helloworld/
admin/
...
tests/
bootstrap.php
phpunit.xml
modelHelloWorldsTest.php
site/
...
helloworld.xml
To run the tests, I install/copy the component to my Joomla installation. I then run the following command from ~joomla/administration/components/com_helloworld/tests:
php phpunit-4.2.phar --bootstrap bootstrap.php .
from which I receive
Fatal error: Class 'ContentController' not found in C:\inetpub\wwwroot\ws_cairnstest\administrator\components\com_helloworld\tests\modelsHelloWorldsTest.php on line 5
bootstrap.php:
<?php
error_reporting(E_ALL);
define('_JEXEC', 1);
define('BASEPATH',realpath(dirname(__FILE__).'/../../'));
define('JOOMLA_PATH',realpath(dirname(__FILE__).'/../../../../../'));
define('JOOMLA_ADMIN_PATH',realpath(dirname(__FILE__).'/../../../../'));
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['REQUEST_METHOD'] = 'GET';
if (file_exists(JOOMLA_ADMIN_PATH . '/defines.php'))
{
include_once JOOMLA_ADMIN_PATH . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', JOOMLA_ADMIN_PATH);
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
require_once JPATH_BASE . '/includes/helper.php';
require_once JPATH_BASE . '/includes/toolbar.php';
define('JPATH_COMPONENT',JOOMLA_ADMIN_PATH.'/components/com_content');
$app = JFactory::getApplication('administrator');
include BASEPATH.'/controller.php';
modelsHelloWorldsTest.php:
<?php
class HelloWorldsTest extends \PHPUnit_Framework_TestCase {
public function testList(){
$c = new ContentController();
$model = $c->getModel('helloworlds');
$worlds = $model->getItems();
var_dump($worlds);
$this->assertNotEmpty($worlds);
}
}
phpunit.xml:
<phpunit bootstrap="bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true">
</phpunit>
Unit tests for Joomla are typically written to use the PHP Unit framework
To answer your questions
Where to put component test code: at the top level of your repo as follows
Component repo directory structure:
src
admin/
site/
tests
How to run component unit tests: test execution follows this pattern
vendor/bin/phpunit --configuration phpunit.xml
additional details can be found in the php unit documentation https://phpunit.de/documentation.html
Is your test code correct: no
You failed to mock your class and methods with the getMockBuilder() for testing.
Here are some examples to look at
https://github.com/joomla-framework/database
https://github.com/joomla-extensions/weblinks
I want to call my zend-framework model and use there functions in AuthenticationControllerTest.php but I am getting error when i run it from terminal.
- -MyZendproject
- -application
-model
-testmodel
- +public
- -tests
- aplication
- controller
- .AuthenticationControllerTest.php
Here is my AuthenticationControllerTest.php file
<?php
require_once `PHPUnit/Framework/TestCase.php`;
defined(`APPLICATION_PATH`)
|| define(`APPLICATION_PATH`, realpath(dirname(__FILE__) . `/../application`));
// Define application environment
defined(`APPLICATION_ENV`) || define(`APPLICATION_ENV`, `tests`);
// 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`;
$application = new Zend_Application(
APPLICATION_ENV,
realpath(APPLICATION_PATH .`configs/application.ini`)
);
class AuthenticationControllerTest extends PHPUnit_Framework_TestCase
{
public function testLoginRetriespLogin() {
$testmodel = new Model_testmodel_Object();//my model
}
}
but when run it from terminal "phpunit AuthenticationControllerTest" it give me error:
$ phpunit AuthenticationControllerTest
..FPHP Fatal error: Class 'Model_testmodel_Object' not found in /var/www/versioned/pm160form/tests/application/controllers/AuthenticationControllerTest.php on line 146
Looks like your test suit folder is not resolving towards the actual application folder. See how you have
|| define(`APPLICATION_PATH`, realpath(dirname(__FILE__) . `/../application`));
but your dirname is currently pointing to the test/application/controller directory which, of course, does not have your actual application. Try pointing the application folder that is under the top root instead,
|| define(`APPLICATION_PATH`, realpath(dirname(__FILE__) . `/../../../application`));
Also, take care to follow the standard of using PHP Unit in Zend Framework as described at http://framework.zend.com/manual/1.12/en/zend.test.phpunit.html . The framework needs to be fully bootstraped before the autoloader can work.
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.