Testing a CakePHP controller

Keyboard Shortcuts

Controller testing is a different concept then testing a model and helper, because CakePHP controllers do not operate in isolation. Instead, controllers are tested through a process called integration testing. Create an integration test in CakePHP for testing a CakePHP controller action. Learn how to submit a form to test a controller method and ensure data was saved to your database.

- [Voiceover] Testing a Cake PHP Controller…is a different approach from what we have done…up to this point.…Not just is our approach different,…but the style of testing you perform is different.…Instead of performing a unit test,…where we call a method and see what the returns are,…instead in Controllers, we test them using…a pattern called Integration Testing.…This is where we test the integration…of all the parts working together,…and verify our Controller method…had all the side-effects we expected.…A good example of this is submitting a valid form.…

We want to verify not just that the flash message…was set but also that a new record was saved,…and we were redirected to the new location,…although side-effects need to occur for us…to reliably test the Controller method.…Let's take a look at testing the Controller now…with the BookmarksController.…Open up Test, TestCase, Controller,…BookmarksController, test.php.…We want to test the Add method…in the BookmarksController.…Recall what an Add method is doing…when we submit the form.…

Resume Transcript Auto-Scroll

Author

Released

3/22/2016

CakePHP is a registered trademark of Cake Software Foundation, Inc.. Cake Software Foundation, Inc. is not affiliated with Lynda.com, nor does it endorse, sponsor, or otherwise support Lynda.com services.

CakePHP is a critical framework for PHP developers. It helps them build complex web applications faster and more efficiently. If you want to use CakePHP 3 (the latest version of the framework) in your own development workflow, this is the place to start.

Justin Yost provides an overview of the underlying MVC pattern in CakePHP, and the installation and configuration process for Mac and Windows. He shows how to use the CakePHP shell to build your first basic CakePHP app, and then discusses each application element in depth: controllers, models, views, components, behaviors, helpers, and utilities. At each step, he discusses the relevant new features and enhancements in CakePHP 3, including new components; performance, session management, and ORM improvements; and localization.

In later chapters, the course gets a little more advanced. Watch these tutorials to learn how to send email with CakePHP, extend CakePHP with plugins, and write unit tests to identify and eliminate bugs in your code. Justin also shows how to add security to your CakePHP apps with a basic user authentication system.

Topics include:

Installing and configuring CakePHP

Using the CakePHP shell console

Creating CakePHP controllers

Saving data in a CakePHP model

Finding and deleting data

Working with entity methods

Creating CakePHP views

Using and customizing components to share functionality between controllers