Tanveer’s Weblog

Entries categorized as ‘cakePHP’

[CakePHP] Unit Testing

July 10, 2008 · 2 Comments

What a fun !! :D
Today i test one of my Country Model using cakephp component. That why i write down this notes to make sure that i can use several times.

1. Download Simple Test unit testing suite.
2. Uncompress it in my app/vendors directory.
3. Change DEBUG level of 1 in my app/config/core.php file
4. Create a new database connection in app/config/database.php for only testing purpose like;

var $test = array(
‘driver’ => ‘mysql’,
‘persistent’ => true,
‘host’ => ‘localhost’,
‘login’ => ‘root’,
‘password’ => ”,
‘database’ => ‘db_name’,
‘prefix’ => ”,
‘encoding’ => ‘utf8′
);

5. Fixture : Create a file named country_test_fixture.php in your app/tests/fixtures directory, with the following content

<?php
class CountryTestFixture extends CakeTestFixture {

var $name = ‘CountryTest’;

var $import = array(‘model’ => ‘Country’, ‘records’ => true , ‘connection’ => ‘test’);

}
?>

6. Test Case : Create a file named country.test.php in your app/tests/cases/models directory, with the following contents:

<?php

loadModel(‘Country’);

class CountryTest extends Country {
var $name = ‘Country’;
//var $useDbConfig = ‘test_suite’;
}

class CountryTestCase extends CakeTestCase {

var $fixtures = array( ‘Country_test’ );

function testFindAll() {

$this->CountryTest =& new CountryTest();

$result = $this->CountryTest->findAll(“Country.country_id=1″);

$expected = array(

array (
‘Country’ => array( ‘country_id’ => 1, ‘region_id’ => 2 , ‘country’ => ‘Bangladesh’ ),

)

);

$this->assertEqual($result, $expected);

}

}
?>

7. Running My Test Case: Point my browser to http://tanveer-noman/myProject/test.php. Click on App Test Cases and find the link to your models/country.test.php. Click on that link.
Waoow!! I got a nice green screen saying that your test succeded.
;)

To know more

cake bake

Categories: Fun · Tips' n Tricks' · cakePHP
Tagged:

CakePHP: Creating a route usage https (SSL connection)

July 9, 2008 · 8 Comments

While playing in my workspace i need to make a route which should be use a secure path. That means https (SSL connection). So after googled for while i got the idea. Basically what i need is.. when an user in my sing in page will go through via the ssl connection. After entering user name and password will validated and redirect in to another location. So usually user will go through in https protocol then again redirected into http.

Here what i did;

Well, obviously i use cake’s component

1. Create a file ssl.php into app/controllers/components/
paste this code

<?php
class SslComponent extends Object {

var $components = array('RequestHandler');

var $Controller = null;

function initialize(&$Controller) {
$this->Controller = $Controller;
}

function force() {
if(!$this->RequestHandler->isSSL()) {
$this->Controller->redirect('https://'.$this->__url());
}
}

function unforce() {
if($this->RequestHandler->isSSL()) {
$this->Controller->redirect('http://'.$this->__urll());
}
}

function __url() {
$port = env('SERVER_PORT') == 80 ? '' : ':'.env('SERVER_PORT');

return env('SERVER_NAME').$port.env('REQUEST_URI');
}

function __urll() {
$port = env('SERVER_PORT') == 443 ? '' : ':'.env('SERVER_PORT');

return env('SERVER_NAME').$port.env('REQUEST_URI');
}

}
?>

you can find it also here but i added unforce() and __urll() ;)

3. Now, in my case only when user click in to sign in or lend now, I need the https connection. Thats why, in my home controller i added this beforeRender() method, but make sure you assign this Ssl component.

var $components = array( 'Ssl' );
public function beforeRender(){

$action = array( 'signin', 'lendnow' );

if( in_array( $this->params['action'] , $action ) ){

     $this->Ssl->force();
}else{
     $this->Ssl->unforce();
}

}

So, i will create a secure connection with my apache server.
4. User now enter their user name and password and submit

5. If anyone click beside this they will have only http connection

That’s it

enjoy ;)

Categories: Tips' n Tricks' · cakePHP
Tagged:

cakephp : $ajax.observeField and Session lost

July 2, 2008 · 3 Comments

Finlay i made the solution in my style… :)
In my cakePHP project when ever i made any Ajax request to my system… my session was lost.. and the new requested controller and action take the place on my session variable. I was totally mad.. because it takes my localization action away… The thing is when ever i change my combo box.. it calls the ajax request and reset my language session value and if any one click on the language flag on that moment my page layout was lost and broke down… :(

Solutions:

1. Change the CAKE_SECURITY in app core.php to a level of medium or low
(don't worry it will not make any hole in the system and someone says that it can be the solution anyway..)
2. In app_controller set
var $components=array('RequestHandler');
3. Now, inside the beforeRender() method...add
$current_session_value = $this->Session->read('my_sess_var') ;
// this is the session which i want to have it
//What i did just check is ther any Ajax request has occurred.. so i use isAjax()
if ( $this->RequestHandler->isAjax() ) {
$this->Session->write('my_sess_var', null ); //i reset that session which was use to store
$this->Session->write('my_sess_var', $current_session_value ); // re-assign again
}

yahoo…. my problem is solved… no i can change my language whenever i want…

:)

Categories: PHP · Tips' n Tricks' · cakePHP

open-flash-chart in cakephp

June 24, 2008 · 2 Comments

Today i implement open-flash-chart into one of my cakephp project. The main goal of my work was to show a line graph with total amount of loan per month from june 2007 to till. Then take some time in google to find out some useful tutorials and i made my one way.

First i made the helper as describe in open-flash-chart-helper-draw-charts-the-cake-way from bakery. nice tutorial.
not so hard to implemnt..
what i did just maintain the vendor path
vendor(‘open-flash-chart’); in the flash_chart helper class class file
That’s it…

Categories: cakePHP

mktime() First Day of the Last Week

May 7, 2008 · Leave a Comment

The mktime() function returns the Unix timestamp for a date.
This timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Assumed Moday is the firstday of the week.

<?php

//YESTERDAY
$yesterday = date("Y-m-d H:i:s",mktime(0, 0, 0, date("m")  , date("d")-1, date("Y")));

//THIS WEEK
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("n"), date("j"), date("Y")) - ((date("N")-1)*3600*24));
$toDate = CURRENT_TIMESTAMP;
//LAST WEEK
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("n"), date("j")-6, date("Y")) - ((date("N"))*3600*24));
$toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, date("n"), date("j"), date("Y")) - ((date("N"))*3600*24));

//THIS MONTH
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("m")  , 1, date("Y")));
$toDate = CURRENT_TIMESTAMP;

//LAST MONTH
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("m")-1  , 1 , date("Y")));
$toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, date("m"), date("d")-date("j"), date("Y")));

//THIS YEAR
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date(1)  , date(1), date("Y")));
$toDate = CURRENT_TIMESTAMP;

//LAST YEAR
$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, 1  , 1, date("Y")-1));
$toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, 12, 31, date("Y")-1));

?>

Categories: Fun · cakePHP

Thumbnail Generator in CakePHP

April 27, 2008 · Leave a Comment

Generating Thumbnail Class
<?php
class ImagesController extends AppController {

	var $name = 'Images';
	var $uses = null;

	// we want to ensure that the default layout doesn't get rendered around your image data
	// that was causing me lots of errors that i thought were BOM errors, until i commented
	// out the header call in the view file

	var $layout = null; 

	function thumbnail($filename='') {
		$this->set('image_file',$filename);
	}
}
?>


<?php
// create a thumbnail on the fly for product images

$parentImage = IMAGES_ROOT.'productImages/'.$image_file;
$thumbImage = IMAGES_ROOT.'productThumbs/'.$image_file;
if(!file_exists($thumbImage)){

// the thumbnail does not exist, create it before sending the location of it to the browser.
	ob_start();
	//header("Content-type: image/jpeg");
	$im = imagecreatefromjpeg($parentImage);
	$orig_height = imagesy($im);
	$orig_width = imagesx($im);
	$new_height = 60;
	$new_width = (int) (($new_height / $orig_height) * $orig_width);
	$new_im = imagecreatetruecolor($new_width,$new_height);
	imagecopyresampled($new_im,$im,0,0,0,0,$new_width,$new_height,$orig_width,$orig_height);
	imagejpeg($new_im);
	$image = ob_get_clean();
	$thumb_pointer = fopen($thumbImage,'w+');
	fputs($thumb_pointer,$image,strlen($image));
	fclose($thumb_pointer);
}
header("Location: /img/productThumbs/{$image_file}");
?>

Categories: cakePHP

A cheatsheet for CakePHP

April 27, 2008 · 1 Comment

A cheatsheet for CakePHP can be found at
http://cakephp.org/files/cakesheet.pdf

Categories: cakePHP

CakePHP resources

April 27, 2008 · Leave a Comment

Categories: cakePHP