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();
}

Monday, August 25, 2008

Remote Debugging Flash or Flex Apps using Flex IDE

Theres a lot of information about this already, just mentioning it one more time won't hurt:

1] Make sure your Flash app is compiled permitting debugging. If using Flex make sure its not the release compilation. Lets pretend the compiled program is uploaded to http://blah.com/flashapp/flashapp.html.

2] Create a new Flex project, you can name it anything.

3] Right click on the new project, goto Properties then Run/Debug Settings. Make a new configuration with the Debug/Profile/Run path pointing to the application you want to debug, in our case: http://blah.com/flashapp/flashapp.html.

4] Click Debug project. It will pop up the application you want to debug.

5] When the window pops up, right click the program. If you have Flash Debug Player installed, there will be a context menu item called Debugger. Click this.

6] Tell it you want to connect the debugger to localhost (which your Flex IDE started).

7] Done, you should be able to see trace statements :)

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.

Monday, August 11, 2008

Flex ArrayCollection CollectionEvent dispatching class changes

Lets say you have a class:


[ArrayElementType("com.my.AwesomeClass")]
private var myAC:ArrayCollection = new ArrayCollection();


your class is such that sometimes changing a property in AwesomeClass doesn't
make myAC dispatch CollectionEvent.COLLECTION_CHANGE. An example changing the
property awesomeClass.foo.x.

To tell myAC that an item is changed use ArrayCollection's itemUpdated method after changing an element in the ArrayCollection.


myAC.itemUpdated(awesomeClass);

Monday, August 4, 2008

Getting CakeAMFPHP w/ CakePHP 1.1 centralized database sessions working

CakePHP makes it a snap to create centralized database sessions.

However, If you wanted to use Flash and CakeAMFPHP with CakePHP's sessions, you'll find that it might not be working. Here are some fixes+workarounds to get it running.

Step 1] open /app/vendors/vendors/cakeamfphp/amf-core/app/Gateway.php
comment out line 93

//$this->filters['auth'] = 'authenticationFilter';


The auth filter was breaking the sessions for some reason.

Step 2] Open /app/config/core.php
change the CAKE_SESSION_SAVE to:

define('CAKE_SESSION_SAVE', 'database');


change CAKE_SESSION_COOKIE to your php cookie sessions name(look in phpinfo()):
for example mine is:

define('CAKE_SESSION_COOKIE', 'PHPSESSID');


change CAKE_SECURITY to low:

define('CAKE_SECURITY', 'low');


Step 3] create the sessions table in your $default(in database.php) database by importing the file at: /app/config/sql/sessions.sql


Step 4] open /cake/libs/session.php and comment out:
on line 132:

/*if (Configure::read('Session.checkAgent') === true) {
if (env('HTTP_USER_AGENT') != null) {
$this->_userAgent = md5(env('HTTP_USER_AGENT') . CAKE_SESSION_STRING);
}
}*/


on line 311:

if (/*Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read("Config.userAgent") && */$this->time <= $this->read("Config.time")) {


on line 541:

if (/*Configure::read('Session.checkAgent') === false || $this->_userAgent == $this->read("Config.userAgent") && */$this->time <= $this->read("Config.time")) {


the useragent works with the browser but we are connecting using flash to amf to for some reason this was breaking.



You are done, try playing around with the Session methods to see if your
sessions are being stored and retrieved from the database. For a tutorial on CakePHP sessions take a look here.



---edit----
still having problems? Try turning off cookies in your cake gateway.

Open /app/webroot/cake_gateway.php:

insert this at the top:

ini_set('session.use_trans_sid', 1); //make it so we can use cookie-less sessions
ini_set('session.use_cookies', 0);


open /cake/libs/session.php:

find all occurences of ini_set('session.use_trans_sid', 0);
and change them to

ini_set('session.use_trans_sid', 1);



in your delegate when you call the gateway it must pass in a PHPSESSID example:


//cookieless sessions
if (session_id != null) {
appendurl = '?PHPSESSID='+session_id;
}

var gateway:String="http://cakeamfphp/cake_gateway.php"+appendurl;