Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts

Sunday, January 18, 2009

Getting started with Unit Testing in your Zend Framework application Leopard

Hello, this guide is for someone who has no idea how to get started writing unit tests inside the Zend Framework application. I don't claim this to be the right way to do it, but its a step in the right direction for me.


Step 1] Install pear and PHPUnit



Here are the steps I used in Leopard.

First install PEAR:


#cd /tmp
#curl http://pear.php.net/go-pear > go-pear.php

#sudo php -q go-pear.php

press enter, choose option 1 the prefix directory as "/usr/local" this will install the pear executable to /usr/local/bin/ and the PEAR files to /usr/local/PEAR

After the install finishes try typing:

#pear


If it doesn't work, the install failed or the $PATH variable doesn't include the /usr/local/bin dir.


Next, install PHPUnit using pear:


#sudo pear channel-discover pear.phpunit.de
#sudo pear install phpunit/PHPUnit

If it gives you any warnings about folder permissions you might have to do something like #sudo chmod 777 the temp dir or wherever its complaining about.


Once pear installs PHPUnit try running it in the commandline

#phpunit


It should run ok. If you get any warnings you will have to look at your php config file.


If your php.ini isnt located in /etc/php.ini you'll have to create a symbolic link as such. For example, I develop using XAMPP so my php config is in /Applications/xampp/etc/php.ini.


#ln -s /Applications/xampp/etc/php.ini /etc/php.ini


Next edit /etc/php.ini to make sure the include_path to PHPUnit is correct


#sudo mate /etc/php.ini


Mine looks something like this.
include_path = ".:/php/includes:/usr/local/PEAR:/usr/local/PEAR/PHPUnit"


Try running PHPUnit again in the command line.

#phpunit


If it doesn't give any warnings, move on to step 2.


Step 2] Create the /tests and /tests/myapp folders.



Your Zend project structure should look something like this:


/myapp/
/myapp/application/
/myapp/application/controllers/
/myapp/application/models/
/myapp/application/views/
/myapp/application/views/scripts/
/myapp/library/
/myapp/library/Zend/
/myapp/tests/
/myapp/tests/myapp/



Step 3] Create the /myapp/application/controllers/IndexControllerTest.php file



This file is to test the IndexController.



class MyApp_public_IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

public function setUp()
{
$this->bootstrap = APPLICATION_PATH . '/bootstrap.php';
parent::setUp();
}

/**
* Tests FooController->barAction()
*/
public function testIndexAction()
{
// TODO Auto-generated FooControllerTest->testBarAction()

$this->dispatch ( '/index/index' );
$this->assertController ( 'index' );
$this->assertAction ( 'index' );
}
}


Step 4] Create the /myapp/tests/myapp/AllTests.php file






define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application/'));

set_include_path(
APPLICATION_PATH.'/../library'
. PATH_SEPARATOR . get_include_path()
);


if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'MyApp_AllTests::main');
}

require_once('Zend/Test/PHPUnit/ControllerTestCase.php');

require_once APPLICATION_PATH . 'controllers/IndexControllerTest.php';

class MyApp_AllTests
{
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}

public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('All Tests - MyApp');


$suite->addTestSuite('MyApp_public_IndexControllerTest');

return $suite;
}
}

if (PHPUnit_MAIN_METHOD == 'MyApp_AllTests::main')
{
MyApp_AllTests::main();
}



Step 5] Run AllTests.php



Open the terminal and navigate to the folder where you created AllTests.php


#cd XYZ/myapp/tests/myapp/
#phpunit AllTests.php

PHPUnit 3.3.10 by Sebastian Bergmann.

.

Time: 0 seconds

OK (1 test, 2 assertions)



Hope this started you off okay. Goodluck!


P.S.

Leopard's included PHP does not have PDO_Mysql included by default.
In your Zend_Config_Ini you can use mysqli instead.

Example /application/config/app.ini

[production]
webhost = www.awebsite.com
database.adapter = pdo_mysql
database.params.host = localhost
database.params.username = auser
database.params.password = apassword
database.params.dbname = adatabase

[development : production]

[testing : production]
database.params.dbname = adatabase
#leopard's compiled php doesn't have PDO_MYSQL
database.adapter = mysqli

Wednesday, August 13, 2008

CakePHP 1.1 Unit Testing Test Suite v 1.0.0.5 tutorial

So your stuck with CakePHP 1.1, but you need to add Unit Testing, what do you do?


You can grab Cake Test Suite v1.0.0.5 (using SimpleTest) here


The thing with this Test Suite is that I couldn't find documentation anywhere so I decided to write this little tutorial on how to get started.


Step 1: Setup
First make sure your CakePHP Project's debug level is set to at least 1.


Next, after unzipping the app folder into your project and trying to run the test controller(http://yourproject/test/) you might see that you will get this error:


Notice: Use of undefined constant CORE_PATH - assumed 'CORE_PATH' in D:\Flex Workspaces\cakeamfphp\cake\config\paths.php on line 44



To fix this open up /app/webroot/test/index.php and insert this on line 82:

if (!defined('CORE_PATH')) {
if (function_exists('ini_set')) {
ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'));
define('APP_PATH', null);
define('CORE_PATH', null);
} else {
define('APP_PATH', ROOT . DS . APP_DIR . DS);
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
}
}

require CORE_PATH.'cake'.DS.'bootstrap.php';


Step 2: Writing the Tests

Lets say for example, I have a model user.php that I want to test, I have to create this file as /app/tests/app/cases/models/User.test.php

It will look something like this:


class UserTest extends UnitTestCase {

function testSomething() {
loadModel('User');
$user = &new User();
$this->assertEqual($user->name, 'User');
}
}

?>



If testing a controller, you would want to use the loadController method.


If I wanted to make a group test of my models I would for example make a file
in app/tests/app/groups/Model.group.php:



require_once(TESTS.'/app/cases/models/User.test.php');
require_once(TESTS.'/app/cases/models/User_Builder.test.php');

class ModelTest extends GroupTest {

function ModelTest(){
$this->addTestCase(new UserTest());
$this->addTestCase(new User_BuilderTest());
}

}

?>





Hope this helps get you on the right track. Take a look at the simpletest docs for more information on how to unit test your project.