Monday, January 19, 2009

Getting Zend Debugger to work with XAMPP Leopard with Zend Studio

1] Copy the Zend Debugger PHP Extension from Zend Studio to your webserver's PHP extension dir.


#cp '/Applications/Zend/Zend Studio for Eclipse - 6.1.0/plugins/org.zend.php.debug.debugger.macosx_5.2.14.v20080602/resources/php5/ZendDebugger.so' /Applications/xampp/xamppfiles/lib/php/php5/extensions/no-debug-non-zts-20060613/ZendDebugger.so


2] edit XAMPP's php.ini to include the extension (#sudo mate /Applications/xampp/etc/php.ini)


;extension=ZendDebugger.so
zend_extension=/Applications/xampp/xamppfiles/lib/php/php5/extensions/no-debug-non-zts-20060613/ZendDebugger.so
zend_debugger.allow_hosts=127.0.0.1
zend_debugger.expose_remotely=always

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

Thursday, January 15, 2009

Setting up Trac with Plesk

If your running a Plesk server and want to get trac running with mod_python, heres what I did to get them running nicely.

This tutorial assumes you have already installed Subversion, Trac, and that running your Trac daemon was successful.

Setup the trac subdomain in plesk:

1] Create a subdomain 'trac'
2] important: make sure python is unchecked->DISABLED (wierd i know!)

Creating a new project:

#trac-admin /disk1/trac/mynewproject initenv
#mkdir /disk1/trac/mynewproject/.egg-cache
#chmod 777 /disk1/trac/mynewproject/.egg-cache
#chmod -R 777 /disk1/trac/mynewproject/db
#chmod -R 777 /disk1/trac/mynewproject/attachments



Adding a new user:
#htpasswd /disk1/trac/.htpasswd mynewusername


Editing user permissions:
http://trac.edgewall.org/wiki/TracPermissions
If there are no trac admins yet, you must create one in the shell
an example:

#trac-admin /disk1/trac/myproject permission remove anonymous BROWSER_VIEW
#trac-admin /disk1/trac/myproject permission add auser TRAC_ADMIN


Editing trac vhost file:
Please note the PythonPath, your may be different. try typing #find -name trac to find the folder thats similar to mine.

#emacs /var/www/vhosts/yourdomain.com/subdomains/trac/conf/vhost.conf for the new location
example:

<location "/mynewproject">
# SetHandler mod_python
SetHandler python-program
# PythonDebug on
PythonPath "['/usr/lib/python2.4/site-packages/'] + sys.path"
PythonInterpreter main_interpreter
PythonHandler trac.web.modpython_frontend
PythonOption TracEnv /disk1/trac/mynewproject
PythonOption TracUriRoot /mynewproject
</location>
<location "/mynewproject/login">
AuthType Basic
AuthName "WillInteractive Trac Server"
AuthUserFile /disk1/trac/.htpasswd
Require valid-user
</location>


Restart your apache server
#service httpd restart

Next try to access it in your browser at:
http://yourdomain.com/mynewproject


Monday, January 12, 2009

Improving performance in your Flex Applications

Do you understand a day in the life of a Flex component (creation, initialization, invalidation/validation)? Have you heard of how Flex components have to run around this "elastic racetrack" (code execute/render) and how Flex components are structured to run optimally around this racetrack?

When extending a Flex component you need to know these things in order for your component to achieve the best performance possible.

Check out this Adobe Max presentation titled "Creating New Components in Flex 3" by Deepa Subramaniam (you wont be disappointed :P):
http://tv.adobe.com/#vi+f15384v1002

Wednesday, September 17, 2008

Setup Eclipse PDT a PHP IDE with Zend Debugger easily!

So, Dreamweaver was a good editor, but if you have a big PHP project, you will need some more firepower. I decided to install Eclipse PDT a PHP IDE with the Zend Debugger. I used WAMP as my webserver and firefox as my external browser.

I haven't used it yet but I have a feeling it will help speed up development :)

Here are the steps:

Step 1] Download and install Eclipse PDT 1.0.3 (Release) (example: C:\Program Files\PDT\)
http://download.eclipse.org/tools/pdt/downloads/release.php?release=R20080603

Step 2] Download and install WAMP or any server with PHP.
http://www.wampserver.com/en/download.php

Step 3] Download and install Zend Debugger (put the ZendDebugger.dll in your C:\wamp\php\ folder)
http://downloads.zend.com/pdt/server-debugger/
Put the dummy.php in your PHP project's webroot.

Step 4] open up C:\wamp\php\php.ini
change: implicit_flush = Off to implicit_flush = On
change: output_buffering=4096 to output_buffering = Off
at the bottom append:
[Zend]
zend_extension_ts = "C:\wamp\php\ZendDebugger.dll"
zend_debugger.allow_hosts=127.0.0.1/32, 192.168.0.0/255.255.0.0
zend_debugger.expose_remotely=always

Step 5] Install Subclipse by opening PDT goto Help->Find and Install Updates(optional)
Add remote server with URL: http://subclipse.tigris.org/


--------- Extra steps to start debugging with firefox (optional, sorry its a little rough, but these are just general steps) ------------
Step 1] Start PDT, goto Window->Preferences->General->Web Browser, select use external web browser, select firefox
Step 2] Make a new project, make a new file called index.php, right click on the file, goto run as, open run as dialog.
Step 3] Point WAMP to the new project (ex: http://helloworld)
Step 4] Go back to PDT, under PHP Web Page, create new. Under Name: write in Test index.php. Under Server Bugger use: Zend Debugger. On PHP server, make a new one pointing to http://helloworld Under file, manually type in index.php. URL should be left at auto generate.
Step 5] Goto the common tab, check Debug and Run under 'display in favorites menu'. Click apply settings.
Step 6] Edit index.php to do something like echo 'hello world';
Step 7] At the top toolbar if you click the debug arrow you should see test index.php in your favorites, click it. Firefox should popup while PDT is connected to the zend debugger.

Wednesday, September 10, 2008

CakePHP 1.19 Test Suite testing with Session Component

Hello, you might be having a tough time doing some integration testing on your controllers which use the Session component. Here are is an example to help you get started:


class Foo_ControllerTest extends UnitTestCase
{
function test1()
{

$usersController = &new UsersController();
$usersController->_initComponents();
$usersController->constructClasses();
$this->assertNotNull($usersController->Session);
$usersController->Session->_checkValid();

}
}


This creates the model + components in your controller. The sessions should be working normally now.

Take at look at this to see how they did unit testing on the Session component itself for more ideas (https://trac.cakephp.org/browser/branches/1.2.x.x/cake/tests/cases/libs/session.test.php?rev=7246).

Tuesday, September 9, 2008

cakephp 1.19 AppModel bug?

I've been using the $hasMany, $uses, etc properties in the AppModel to simply some of the querying operations. But it seems like sometimes it randomly deletes rows from some of the tables I've specified in the tables. Also, I don't always need to use those associations.


I've found that loading the model and using it as needed seemed like a better approach and it wasn't deleting data from tables mysteriously anymore..

To use another model only when you need it do something like this:


function amodelmethod()
{
loadModel('FooModel');
$fooModel = &new FooModel();
$fooModel->fooMethod();
}