Book Image

CakePHP 2 Application Cookbook

By : James Watts, Cake Software Foundation, Inc.
Book Image

CakePHP 2 Application Cookbook

By: James Watts, Cake Software Foundation, Inc.

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Model unit testing


You might have already heard that using "fat models, skinny controllers" is a best practice. One of the main benefits of keeping all your business logic inside your model layer is the ability to develop simpler unit tests for your application. Model classes usually depend only on other models, behaviors, or utility classes (libs), allowing you to quickly start coding your unit tests without a ton of mock object configurations.

CakePHP integrates with PHPUnit and provides several tools to help you build your unit tests:

  • Base classes

  • CakeTestCase

  • ControllerTestCase or you can use PHPUnit_Framework_TestCase

  • Fixture integration

  • Test datasource configuration

  • Bake shell to build test files and fixtures

In this recipe, we'll go over most of these, and build a unit test for a CakePHP model.

Getting ready

First, we'll reuse the table named tasks from this chapter or create a new one using the following SQL statement:

CREATE TABLE tasks (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(255...