In this recipe, we will learn how to prepare our CakePHP application with all the elements needed to create our own unit tests, setting up the foundation for the rest of the recipes in this chapter.
To go through the recipes included in this chapter, we need some data to work with. Create the following tables by issuing these SQL statements:
CREATE TABLE `articles`( `id`INT UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `body` TEXT NOT NULL, PRIMARY KEY(`id`) ); CREATE TABLE `users`( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) NOT NULL, PRIMARY KEY(`id`) ); CREATE TABLE `votes`( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `article_id` INT NOT NULL, `user_id` INT NOT NULL, `vote` INT UNSIGNED NOT NULL, PRIMARY KEY(`id`), FOREIGN KEY `votes__articles`(`article_id`) REFERENCES `articles`(`id`), FOREIGN KEY `votes__users`(`user_id`) REFERENCES `users`(`id`) );
Create a controller in a file named articles_controller.php
and place it in your app/controllers
folder, with the following contents:
<?php class ArticlesController extends AppController { public function vote($id) { if (!empty($this->data)) { if ($this->Article->vote($id, $this->data)) { $this->Session->setFlash('Vote placed'), return $this->redirect(array('action'=>'index')); } else { $this->Session->setFlash('Please correct the errors'), } } } public function view($id) { $article = $this->Article->get($id); if (empty($article)) { $this->Session->setFlash('Article not found'), return $this->redirect(array('action' => 'index')); } $this->set(compact('article')); } } ?>
Create a file named article.php
and place it in your app/models
folder, with the following contents:
<?php class Article extends AppModel { public $hasMany = array('Vote'), public function get($id) { return $this->find('first', array( 'fields' => array( 'Article.*', 'AVG(Vote.vote) AS vote' ), 'joins' => array( array( 'type' => 'LEFT', 'table' => $this->Vote->getDataSource()- >fullTableName($this->Vote->table), 'alias' => 'Vote', 'conditions' => array( 'Vote.article_id = Article.id' ) ) ), 'conditions' => array('Article.id' => $id), 'group' => array( 'Article.id' ), 'recursive' => -1 )); } public function vote($id, $data = array()) { if (empty($data) || empty($data['Vote'])) { throw new Exception("No data specified"); } $data['Vote']['article_id'] = $id; $this->Vote->create($data); if (!$this->Vote->validates()) { return false; } $conditions = array( 'Vote.user_id' => $data['Vote']['user_id'], 'Vote.article_id' => $data['Vote']['article_id'] ); if ($this->Vote->hasAny($conditions)) { return false; } return ($this->Vote->save($data) !== false); } } ?>
Create a file named vote.php
and place it in your app/models
folder with the following contents:
<?php class Vote extends AppModel { public $belongsTo = array('Article', 'User'), public $validate = array( 'article_id' => array('required' => true, 'rule' => 'notEmpty'), 'user_id' => array('required' => true, 'rule' => 'notEmpty'), 'vote' => array( 'required' => array('required' => true, 'rule' => 'notEmpty'), 'range' => array( 'rule' => array('range', 0, 6), 'allowEmpty' => true ) ) ); } ?>
Create a folder named articles
and place it in your app/views
folder. Create a file named view.ctp
and place it in your app/views/articles
folder, with the following contents:
<h1><?php echo $article['Article']['title']; ?></h1> Vote: <span id="vote"><?php echo number_format($article[0]['vote'], 1); ?></span> <p><?php echo $article['Article']['body']; ?></p>
app/vendors
folder. You should now have a folder named simpletest
in app/vendors
. http://localhost/test.php
, you should see the list of test groups available in CakePHP as shown in the next screenshot:CakePHP uses the SimpleTest library as the backbone of its unit testing framework. Unless we have installed SimpleTest on our application, we will be unable to run any unit test. Installing the library is as simple as downloading the appropriate version and extracting its contents into our app/vendors
folder.
The framework includes a broad set of unit tests that cover almost every functionality implemented in the core. These unit tests allow the developer to report bugs against core functionality, have them solved, and make sure those bugs do not reappear in future releases.
18.118.163.250