Learning phpunit database test


Difficulties in database testing
The data in DB is equivalent to global variables, which can be modified by everyone and all codes, and the data is changing at any time; the ordinary test method can not guarantee that the test can pass every time
Special framework and means are needed for guarantee.

Dbunit is recommended for DB related testing
To install Dbunit, use Composer:

  "require": {
    "phpunit/dbunit": ">=1.2"

Main steps of DB test

1. Establish fixture
2. Business processing
3. Assertion

1. Establish fixture
Equivalent to initializing dB, where does the initialization data come from? text

  • getConnection
  • Read XML text (or: CVs, yaml, MySQL export data, array…)
  • Insert text content into DB

This ensures that the data in the DB is fixed before each test.

2. Business processing

3. Assertion

  • Use the interface provided by the framework to read: dataset, datatable, rowcount
  • Get expect data = > from XML text or DB
  • The data of assertion = > were compared

Recommended Today

ASP.NET Example of core MVC getting the parameters of the request

preface An HTTP request is a standard IO operation. The request is I, which is the input; the responsive o is the output. Any web development framework is actually doing these two things Accept the request and parse to get the parameters Render according to the parameters and output the response content So let’s learn […]