Test-Driven Development with PHP 8

Book description

A PHP developer's practical guide to planning, writing, and deploying maintainable and extensible software solutions with automated tests in a well-structured way

Key Features

  • Learn to organize and translate business requirements into maintainable software
  • Minimize regressions and bugs while releasing new features or bug fixes
  • Build maintainable applications using SOLID principles

Book Description

PHP web developers end up building complex enterprise projects without prior experience in test-driven and behavior-driven development which results in software that’s complex and difficult to maintain. This step-by-step guide helps you manage the complexities of large-scale web applications. It takes you through the processes of working on a project, starting from understanding business requirements and translating them into actual maintainable software, to automated deployments.

You’ll learn how to break down business requirements into workable and actionable lists using Jira. Using those organized lists of business requirements, you’ll understand how to implement behavior-driven development (BDD) and test-driven development (TDD) to start writing maintainable PHP code. You’ll explore how to use the automated tests to help you stop introducing regressions to an application each time you release code by using continuous integration.

By the end of this book, you’ll have learned how to start a PHP project, break down the requirements, build test scenarios and automated tests, and write more testable and maintainable PHP code. By learning these processes, you’ll be able to develop more maintainable, and reliable enterprise PHP applications.

What you will learn

  • Understand how to start a software project
  • Discover how to use Jira as a tool to organize your tasks
  • Explore when and how to write unit, integration, and functional tests using PHPUnit
  • Write behavior-driven tests using Behat
  • Apply SOLID principles to help you write more testable code
  • Get the most out of your automated tests by using continuous integration
  • Use continuous delivery to help you prepare your application for deployment

Who this book is for

This book is for PHP software developers looking to implement TDD and BDD in their projects. An understanding of object-oriented programming is required to get the most out of this book. Professional software development experience will also be beneficial to understand real-life business cases.

Table of contents

  1. Test-Driven Development with PHP 8
  2. Foreword
  3. Contributors
  4. About the author
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  6. Part 1 – Technical Background and Setup
  7. Chapter 1: What Is Test-Driven Development and Why Use It in PHP?
    1. What is TDD?
    2. Common misconceptions about TDD
      1. Testing software is not my job as a developer; therefore, I don’t need TDD
      2. Developing with TDD is unnecessarily slow
      3. Writing automated or unit tests is TDD
      4. TDD is a silver bullet
    3. Why should we even consider TDD?
    4. What are we planning to achieve in this book?
    5. Summary
  8. Chapter 2: Understanding and Organizing the Business Requirements of Our Project
    1. Technical requirements
    2. The example business project
      1. The business scenario
      2. Making sense of the scenario and the problem
    3. Breaking down the business requirements into software features
      1. Creating a Jira Cloud account and initializing the Jira Cloud project
      2. Creating a Jira epic
      3. Writing user stories
      4. Integrating Jira with Bitbucket
    4. Summary
  9. Chapter 3: Setting Up Our Development Environment Using Docker Containers
    1. Technical requirements
      1. Quick Setup
    2. What is a container?
    3. Setting up our PHP development container
      1. Creating multiple containers
    4. Running Docker containers
      1. Building a Docker image and running containers
      2. Stopping containers
    5. Summary
  10. Chapter 4: Using Object-Oriented Programming in PHP
    1. Technical requirements
    2. Understanding OOP in PHP
      1. What is OOP, anyway?
    3. Classes versus objects
      1. References and objects in PHP
    4. Abstraction in OOP
    5. Encapsulation in OOP
      1. Visibility in PHP
      2. Accessors and mutators
    6. Inheritance in OOP
    7. Polymorphism in OOP
      1. Polymorphism with a PHP abstract class
    8. Polymorphism with a PHP interface
    9. PHP Standards Recommendations (PSRs)
      1. What is a PSR?
    10. Summary
  11. Part 2 – Implementing Test-Driven Development in a PHP Project
  12. Chapter 5: Unit Testing
    1. Technical requirements
      1. Preparing the development environment for the chapter
    2. Defining unit tests
    3. Test coverage
      1. Declaring annotations
      2. Adding more functions to the solution class
      3. Using PHP’s reflection feature to directly test for private methods
      4. Indirectly testing for private methods
    4. Using shell scripts to run tests
    5. Types of tests
      1. Basket 1 – unit tests
      2. Basket 2 – integration tests
    6. Integration test example
      1. Installing Doctrine with Symfony 6
      2. Doctrine and database
      3. Failing our first integration test
      4. Creating a Doctrine entity
      5. Creating a Doctrine table for the entity
      6. Creating a test database
      7. Putting things together
      8. Why did we use an integration test in the first place?
    7. Summary
  13. Chapter 6: Applying Behavior-Driven Development
    1. Technical requirements
      1. Preparing the development environment for the chapter
    2. What is BDD?
    3. Applying BDD using Behat and Gherkin
      1. What is Behat?
      2. What is Gherkin?
      3. What is Mink?
      4. Installing Behat and Mink
    4. Writing PHP code based on Gherkin
    5. Browser emulators and Mink
    6. Why bother with Gherkin, then?
    7. Summary
  14. Chapter 7: Building Solution Code with BDD and TDD
    1. Technical requirements
      1. Preparing the development environment for the chapter
    2. Implementing the Red-Green-Refactor pattern
    3. Writing tests and solution code for the example project
    4. Creating a Behat feature based on a Jira ticket
      1. Creating a Behat registration feature
    5. Passing the Behat registration feature
      1. Writing failing controller tests
      2. Implementing a registration solution using Symfony
      3. Passing the Behat feature
    6. Summary
  15. Chapter 8: Using TDD with SOLID Principles
    1. Technical requirements
      1. Preparing the development environment for the chapter
    2. Jira to BDD to TDD
      1. Gherkin feature
      2. Behat context
      3. Functional test
      4. Integration test
    3. TDD with the Single-Responsibility Principle
      1. Writing the solution code
    4. TDD with the Open-Closed Principle
    5. TDD with the Liskov Substitution Principle
    6. TDD with the Interface Segregation Principle
    7. TDD with the Dependency Inversion Principle
    8. Summary
  16. Part 3 – Deployment Automation and Monitoring
  17. Chapter 9: Continuous Integration
    1. Technical requirements
      1. Preparing the development environment for this chapter
    2. Running and passing all the Symfony application tests
      1. Setting up the local environment
    3. Using Bitbucket Pipelines for CI
    4. Summary
  18. Chapter 10: Continuous Delivery
    1. Technical requirements
    2. Setting up an AWS EC2 instance
    3. Creating an AWS CodeDeploy application
    4. Installing Docker and other dependencies inside the AWS EC2 instance
      1. Connecting to the EC2 instance
      2. Installing CodeDeploy Agent
      3. Installing Docker
      4. Installing docker-compose
      5. Attaching Elastic IP to the EC2 instance
    5. Continuous delivery with Bitbucket Pipelines and AWS CodeDeploy
      1. Bitbucket Pipelines settings
      2. Creating a CodeDeploy configuration file
      3. Running Bitbucket Pipelines
    6. Summary
  19. Chapter 11: Monitoring
    1. Technical requirements
    2. Setting up New Relic APM for PHP
      1. Creating a New Relic account and license key
      2. Installing a New Relic agent in the PHP 8 container
    3. Going through the application performance data
      1. Monitoring other containers in your setup
    4. Summary
  20. Index
    1. Why subscribe?
  21. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Test-Driven Development with PHP 8
  • Author(s): Rainier Sarabia
  • Release date: January 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781803230757