O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Yii Testing

Book Description

Embrace 360-degree testing on your Yii 2 projects using Codeception

In Detail

Testing your application often and early is a key consideration in modern web development. PHP developers using the high-performance PHP framework Yii have access to Codeception, a testing suite that provides comprehensive support for unit, functional, and acceptance tests. With Codeception, you will quickly learn how to create fast, stable applications using a test-driven development methodology. Learning Yii Testing embraces a testing-first strategy to guide you in the implementation of all possible types of tests, covering the core features of Yii 2. Starting with the nuts and bolts of testing, you'll walk through Yii 2's new functionality and improvements and learn how to begin testing with it. You'll explore the world of Codeception and gain insights into key topics such as testing in isolation and component integration tests.

You'll also learn to analyze testing information and improve the quality of your code. At the end of the book, you will be able to use automation tools to reduce your testing workload.

What You Will Learn

  • Understand what testing is, why you need it, and what it encompasses
  • Explore testing using all the features and improvements made available with Yii 2
  • Embrace the Codeception testing framework and exploit its potential
  • Carefully manage the scope of tests according to the application you're writing
  • Write PHPUnit tests using the full range of its capabilities
  • Script functional tests and test REST interfaces
  • Write acceptance tests and make the best use of existing frameworks such as Selenium
  • Discover how to abstract the knowledge you've learned and apply it to any situation

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Learning Yii Testing
    1. Table of Contents
    2. Learning Yii Testing
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. The Testing Mindset
      1. Understanding the importance of testing
      2. Involving project management
        1. Estimating tasks
        2. Testing approaches
          1. Introducing Test Driven Development
        3. Planning tests
        4. Generating tests
      3. Obtaining the testing mindset
        1. Starting with no testing culture – a practical approach
      4. Summary
    9. 2. Tooling up for Testing
      1. Downloading and installing Yii 2
      2. Environment and workflow
      3. Introducing Composer
        1. Installing and using it
          1. The composer.json and composer.lock files
          2. Packages and Packagist
        2. Creating your first web app
        3. The CLI command line
      4. Finding your way around Yii 2
      5. Structure of the default web application
        1. Documentation and sample code
      6. Defining our working strategy
        1. Key features to be implemented
          1. User authentication REST interface
          2. User login from a modal window
      7. Introducing testing for our purposes
        1. Using a top-down approach versus a bottom-up approach
        2. What to test and what not to test
        3. The master test plan
      8. Summary
    10. 3. Entering Codeception
      1. Getting started with Codeception
        1. A modular framework rather than just another tool
        2. Outlining concepts behind Codeception
        3. Types of tests
          1. AcceptanceTester
          2. FunctionalTester
          3. UnitTester
        4. Other features provided by Codeception
      2. Installing Codeception in Yii 2
        1. Finding your way around Codeception
        2. Configuring Codeception
        3. Tests available in Yii 2
      3. Interacting with Codeception
        1. Creating tests
        2. Migrations on the test database
      4. Summary
    11. 4. Isolated Component Testing with PHPUnit
      1. Understanding the work to be done
      2. Using the User model
      3. Implementing the first unit test
        1. How much to care for other people's code
      4. Component testing of the model
        1. What's testing for PHPUnit
        2. Testing the methods inherited by IdentityInterface
        3. Using data providers for more flexibility
        4. Using fixtures to prepare the database
        5. Adding the remaining tests
      5. Implementing the ActiveRecord class and its methods
        1. Dealing with migrations
        2. The Gii code generation tool
      6. Seeing tests pass
        1. Using global fixtures
      7. Summary
    12. 5. Summoning the Test Doubles
      1. Dealing with external dependencies
      2. Isolating components with stubs
      3. Listening for calls with an observer
        1. Introducing mocking
          1. Getting to know the Yii virtual attributes
      4. Writing maintainable unit tests
        1. Using BDD specification testing
      5. Summary
    13. 6. Testing the API – PHPBrowser to the Rescue
      1. Functional tests in Yii 2
        1. Understanding and improving the available CEPTs
        2. Writing reusable page interactions
        3. Implementing fixtures
        4. Pitfalls of functional tests
      2. Functional tests for REST interfaces
        1. Defining the API endpoints
        2. Implementing the tests for the API
      3. Creating a RESTful web service with Yii 2
        1. Writing modular code in Yii
        2. Creating a module with Gii
        3. Using modules in Yii 2
        4. Converting our controller to be a REST controller
        5. Adding the access check and security layer
          1. Building the authentication layer
        6. Modifying the existing actions
        7. Adding a new endpoint with parameters
      4. Summary
    14. 7. Having Fun Doing Browser Testing
      1. Introducing Selenium WebDriver
        1. Installing and running Selenium Server
          1. Configuring Yii to work with Selenium
        2. Implementing WebDriver-led tests
      2. Creating acceptance tests
        1. Implementing the modal window
        2. Making the server side work
        3. Adding the JavaScript interaction
        4. Tying everything together
          1. Dealing with Yii 2 assets bundles
        5. Finalizing the tests
        6. Testing multiple browsers
        7. Understanding Selenium limits
      3. Summary
    15. 8. Analyzing Testing Information
      1. Improving the quality of your tests
        1. Enabling code coverage in Codeception
        2. Extracting the code coverage information for unit tests
        3. Generating a detailed coverage report of the unit tests
        4. Aggregating functional tests to unit tests
        5. Generating acceptance tests' coverage report
      2. Improving our code with the aid of additional tools
      3. Summary
    16. 9. Eliminating Stress with the Help of Automation
      1. Automating the build process
        1. Introducing continuous integration systems
        2. Available systems
        3. Installing and configuring Jenkins
          1. Understanding the Jenkins organization
          2. Installing the required plugins
      2. Creating the required build files
        1. Understanding the basic Ant structure
        2. Adjusting the build.xml file
        3. Preparing the environment for the build
        4. Adding the required configuration settings
        5. Adding Composer, Yii, and Codeception support in Ant
      3. Configuring the Jenkins build
        1. Generic build settings
        2. Build settings
        3. Postbuild settings
        4. Executing the job
      4. Going forward
      5. Summary
    17. Index