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
Authors have earned$10,052,337writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
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
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), EPUB (for phones and tablets) and MOBI (for 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
Top Books
C++ Best Practices
Jason TurnerLevel up your C++, get the tools working for you, eliminate common problems, and move on to more exciting things!
Digital-First Events
Joep Piscaer and Jana BorutaThe only resource you will ever need to launch your digital events program.
Algebra-Driven Design
Sandy MaguireA how-to field guide on building leak-free abstractions and algebraically designing real-world applications.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
Continuous Delivery Pipelines
Dave FarleyThis practical handbook provides a step-by-step guide for you to get the best continuous delivery pipeline for your software.
Cloud Strategy
Gregor Hohpe“Strategy is the difference between making a wish and making it come true.” A successful migration to the cloud can transform your organization, but it shouldn’t be driven by wishes. This book tells you how to develop a sound strategy guided by frameworks and decision models without being overly abstract nor getting lost in product details.
node-opcua by example
Etienne RossignonGet the best out of node-opcua through a set of documented examples by the author himself that will allow you to create stunning OPCUA Servers or Clients.
Technical leadership and the balance with agility
Simon BrownA developer-friendly, practical and pragmatic guide to lightweight software architecture, technical leadership and the balance with agility.
Everyday Rails - RSpecによるRailsテスト入門
Junichi Ito (伊藤淳一), AKIMOTO Toshiharu, 魚振江, and Aaron SumnerRSpecを使ってRailsアプリケーションに信頼性の高いテストを書く実践的なアドバイスを提供します。詳細で丁寧な説明は本書のオリジナルコンテンツです。また、説明には実際に動かせるサンプルアプリケーションも使用します。本書は2017年版にアップデートされ、RSpec 3.6やRails 5.1といった新しい環境に対応しています!さあ、自信をもってテストできるようになりましょう!
Top Bundles
- #1
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
Modern C++ by Nicolai Josuttis
2 Books
- #4
Django for Beginners/APIs/Professionals
3 Books
- #5
Modern Management Made Easy
3 Books
Read all three Modern Management Made Easy books. Learn to manage yourself, lead and serve others, and lead the organization. - #6
Cisco CCNA 200-301 Complet
4 Books
Ce lot comprend les quatre volumes du guide préparation à l'examen de certification Cisco CCNA 200-301. - #7
Mastering Containers
2 Books
Docker and Kubernetes are taking the world by storm! These books will get you up-to-speed fast! Docker Deep Dive is over 400 pages long, and covers all objectives on the Docker Certified Associate exam.The Kubernetes Book includes everything you need to get up and running with Kubernetes! - #8
The Python Craftsman
3 Books
The Python Craftsman series comprises The Python Apprentice, The Python Journeyman, and The Python Master. The first book is primarily suitable for for programmers with some experience of programming in another language. If you don't have any experience with programming this book may be a bit daunting. You'll be learning not just a programming... - #9
CCDE Practical Studies (All labs)
3 Books
CCDE lab - #10
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité...