CodeIgniter Testing Guide
CodeIgniter Testing Guide
$18.99
Minimum price
$25.99
Suggested price
CodeIgniter Testing Guide

This book is 100% complete

Completed on 2016-01-23

About the Book

Who Should Read This Book?

This book is for PHP developers who don't know Automated Testing or Unit Testing, or for those looking for help testing CodeIgniter applications.

If one or more of the lines below sounds familiar, this book is perfect for you!

  • I have never written test code.
  • I want to write test code, but I don't know how.
  • I tried to write test code in the past, but I couldn't quite figure it out.

Table of Contents

Chapter 1: What is Automated Testing?

Let's begin learning about automated testing. First we will explore the basic concepts of automated testing. We will find out why and what you should test. At the same time, I will explain the ideas and testing policies used by this book.

Chapter 2: Setting Up the Testing Environment

To run tests in your PHP environment, you will need to install some additional software. For this book, this includes CodeIgniter, PHPUnit and a tool which acts as a bridge between them, ci-phpunit-test.

Chapter 3: Test Jargon

We define test jargon here. One of the annoying and confusing things in testing is the new vocabulary required to understand it. By the end of this chapter we'll help you understand the difference between Unit, Integration, and System testing; Functional and Acceptance testing; Fixtures and Mocks; and more.

Chapter 4: PHPUnit Basics

IIn this chapter, we will learn the basics of PHPUnit. We will run PHPUnit and learn how to configure it. After that, we will study PHPUnit conventions and write our first test. We also cover PHPUnit functionality, data providers, fixtures, and assertions.

Chapter 5: Testing a Simple MVC Application

You've already learned how to write test code, so here we will write tests for a CodeIgniter Tutorial application. We will write tests for a controller and a model. In this chapter, we will use the database for model testing.

Chapter 6: Unit Testing for Models

We will learn more about testing models. We will write tests for models without using the database. To do this, we will learn about PHPUnit mock objects.

Chapter 7: Testing Controllers

We will learn more about testing controllers in this and the next two chapters. In this chapter, we will write tests for a controller for reviewing, and write tests with mocking models. We also will write test cases for authentication and redirects.

Chapter 8: Unit Testing CLI Controllers

We will continue learning to write tests for controllers. In this chapter, we will write unit tests for controllers, and learn about monkey patching.

Chapter 9: Testing REST Controllers

In this chapter, we will learn about testing REST controllers. You will learn how to send (emulate) requests with methods other than GET and POST.

Chapter 10: Browser Testing with Codeception

In previous chapters, we have been using PHPUnit. In this chapter, we will learn about another testing tool. We will install Codeception, learn to configure it, and write tests which work with the web browser.

About the Authors

Kenji Suzuki
Kenji Suzuki

Kenji Suzuki is a contributor to CodeIgniter. He is the author of ci-phpunit-test and my-ciunit, and a co-author of the first CodeIgniter book by a Japanese famous IT book publisher, "CodeIgniter tettei nyumon". He is a PHP expert, Certified PHP 5 Engineer Expert by Engineer Certification Corporation for PHP.

Mat Whitney
Mat Whitney

Mat Whitney is a contributor to CodeIgniter and currently maintains Bonfire, a projects started by Lonnie Ezell to provide a core set of tools for CodeIgniter projects. Mat is also the technical reviewer of Lonnie's book, Practical CodeIgniter 3.

Table of Contents

  • Preface
    • The Book at a Glance
    • What You Need for This Book
    • Who should read This Book?
    • Why PHPUnit?
    • Is This a CodeIgniter Book?
    • Is Testing PHP Applications Difficult?
      • Is Testing CodeIgniter Applications Difficult?
      • Testing is Fun and Easy
    • Conventions Used in This Book
    • Errata
  • 1. What is Automated Testing?
    • 1.1 Primitive Example
      • Manual Testing
      • Automated Testing
    • 1.2 Why should you write test code?
    • 1.3 Finding the Middle Way
    • 1.4 What should you test?
    • 1.5 TDD or Not TDD
  • 2. Setting Up the Testing Environment
    • 2.1 Installing CodeIgniter
    • 2.2 Installing ci-phpunit-test
      • Enabling Monkey Patching
    • 2.3 (Optional) Installing VisualPHPUnit
      • Installing Composer
      • Installing VisualPHPUnit
      • Installing PHPUnit
      • Configuring VisualPHPUnit
      • Configuring PHPUnit XML Configuration File
    • 2.4 Installing PHPUnit
    • 2.5 (Optional) Installing PsySH
    • 2.6 Installing via Composer
      • Installing Composer
      • Installing CodeIgniter via Composer
      • Installing ci-phpunit-test via Composer
      • Installing PHPUnit via Composer
      • (Optional) Installing PsySH via Composer
  • 3. Test Jargon
    • 3.1 Testing levels
      • Unit Testing
      • Integration Testing
      • System Testing
    • 3.2 Testing Types
      • Functional Testing
      • Database Testing
      • Browser Testing
      • Acceptance Testing
    • 3.3 Code Coverage
    • 3.4 Fixtures
    • 3.5 Test Doubles
      • Mocks and Stubs
  • 4. PHPUnit Basics
    • 4.1 Running PHPUnit
      • Running All Tests
      • Running a Specific Test Case
    • 4.2 Running PHPUnit via Web Browser
      • Running Web Server
      • Running All Tests
      • Running a Specific Test Case
    • 4.3 Configuring PHPUnit
      • XML Configuration File
      • Command Line Arguments and Options
    • 4.4 Understanding the Basics by Testing Libraries
      • Basic Conventions
      • Data Provider
      • Fixtures
      • Assertions
  • 5. Testing a Simple MVC Application
    • 5.1 Functional Testing for Controller
      • Controller to Handle Static Pages
      • Manual Testing with a Web Browser
      • Test Case for Page Controller
      • Checking Code Coverage
    • 5.2 Database Testing for Models
      • Preparing the Database
      • News Section
      • Manual Testing with a Web Browser
      • Database Fixtures
      • Test Case for the News Model
      • Checking Code Coverage
  • 6. Unit Testing for Models
    • 6.1 Why Should You Test Models First?
    • 6.2 PHPUnit Mock Objects
      • Playing with Mocks
      • Partial Mocks
      • Verifying Expectations
    • 6.3 Testing Models without Database
      • Testing the get_news() Method with Mocks
      • Testing the set_news() Method with Mocks
    • 6.4 With the Database or Without the Database?
      • Testing with Little Value
      • When You Write Tests without the Database
  • 7. Testing Controllers
    • 7.1 Why is Testing Controllers Difficult?
    • 7.2 Test Case for the News Controller
    • 7.3 Mocking Models
    • 7.4 Authentication and Redirection
      • Installing Ion Auth
      • Manual Testing with a Web Browser
      • Testing Redirection
      • Mocking Auth Objects
    • 7.5 What if My Controller Needs Something Else?
  • 8. Unit Testing CLI Controllers
    • 8.1 Dbfixture Controller
    • 8.2 Faking is_cli()
    • 8.3 Testing exit()
    • 8.4 Testing Exceptions
    • 8.5 Testing Output
    • 8.6 Monkey Patching
      • Patching Functions
      • Patching Class Methods
    • 8.7 Checking Code Coverage
  • 9. Testing REST Controllers
    • 9.1 Installing CodeIgniter Rest Server
      • Fixing the CodeIgniter Rest Server Code
    • 9.2 Testing GET Requests
      • Getting All of the Data
      • Getting One User’s Data
    • 9.3 Adding Request Headers
    • 9.4 Testing POST Requests
    • 9.5 Testing JSON Requests
    • 9.6 Testing DELETE Requests
  • 10. Browser Testing with Codeception
    • 10.1 Installing and Configuring Codeception
      • What is Codeception?
      • Installing Codeception
      • What is Selenium Server?
      • Installing Selenium Server
      • Initializing Codeception
      • Configuring Acceptance Tests
    • 10.2 Writing Tests
      • Conventions for Codeception Acceptance Tests
      • Writing Our First Test
    • 10.3 Running Tests
      • Running Selenium Server
      • Running the Web Server
      • Running Codeception
    • 10.4 Browser Testing: Pros and Cons
    • 10.5 Database Fixtures
    • 10.6 Test Case for the News Controller
      • Database Fixtures
      • Testing Page Contents
      • Testing Forms
      • NewsCept
    • 10.7 Testing with Google Chrome
      • Installing the ChromeDriver
      • Configuring Acceptance Tests
      • Running Selenium Server
      • Running Tests
  • 11. Congratulations
  • Appendix A
    • How to Speed Up Testing
      • Speed Up without Code Modifications
  • Appendix B
    • How to Read ci-phpunit-test
      • Structure of ci-phpunit-test
      • Bootstrap
      • Autoloader
      • Replaced Classes and Functions
      • New Functions
      • TestCase classes
      • Request related classes
      • Helper classes
      • Monkey Patch library
  • Appendix C
    • References
    • License Agreement for CodeIgniter and its User Guide
    • License Agreement for CodeIgniter Rest Server
    • License Agreement for Ion Auth

The Leanpub 45-day 100% Happiness Guarantee

Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Write and Publish on Leanpub

Authors, publishers and universities use Leanpub to publish amazing in-progress and completed books and courses, just like this one. You can use Leanpub to write, publish and sell your book or course as well! Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks. Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. It really is that easy.

Learn more about writing on Leanpub