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

Creating a fixture


When you're testing your code, it's important to have a static and defined state before you start every test to ensure that every test is isolated and does not impact further testing logic upon execution. Fixtures help you configure the database state before every unit test is executed. These are simply files that describe database tables and contents, and they are located by convention in app/Test/Fixture/.

CakePHP provides both a fixture generation tool using the bake console shell as well as a test database configuration (the test database is used automatically when you run your tests). This way, you can keep your real data and test data in two different databases to avoid any unwanted interactions.

In this recipe, we'll build a fixture using the bake shell for a given model.

Getting ready

We'll first need a table of data to work with. So, create a table named blogs, using the following SQL statement:

CREATE TABLE blogs (
  id INT NOT NULL AUTO_INCREMENT,
  title VARCHAR...