CodeIgniter Testing Guide
CodeIgniter Testing Guide
Beginners' Guide to Automated Testing in PHP.
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 EnvironmentTo 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 JargonWe 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 BasicsIIn 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 ApplicationYou'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 ModelsWe 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 ControllersWe 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 ControllersWe 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 ControllersIn 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 CodeceptionIn 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.
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
-
1.1 Primitive Example
-
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
-
3.1 Testing levels
-
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
-
4.1 Running PHPUnit
-
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
-
5.1 Functional Testing for Controller
-
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
-
9.1 Installing CodeIgniter Rest Server
-
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
-
10.1 Installing and Configuring Codeception
- 11. Congratulations
-
Appendix A
-
How to Speed Up Testing
- Speed Up without Code Modifications
-
How to Speed Up Testing
-
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
-
How to Read ci-phpunit-test
-
Appendix C
- References
- License Agreement for CodeIgniter and its User Guide
- License Agreement for CodeIgniter Rest Server
- License Agreement for Ion Auth
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them