|
| 1 | +# PHP Integration Testing |
| 2 | + |
| 3 | +Integration testing library in PHP for databases and other common infrastructure related tests. |
| 4 | + |
| 5 | +It is developed as a set of extensions for PHPUnit that hooks on different events and executes your fixtures. |
| 6 | + |
| 7 | +Currently you can run custom fixtures on the following PHPUnit hooks: |
| 8 | + |
| 9 | +* BeforeFirstTest |
| 10 | +* BeforeTest |
| 11 | +* AfterTest |
| 12 | +* AfterLastTest |
| 13 | + |
| 14 | +## Road map |
| 15 | + |
| 16 | +* RabbitMQ integration |
| 17 | + |
| 18 | + |
| 19 | +## Requirements |
| 20 | + |
| 21 | +[PHPUnit](https://phpunit.readthedocs.io/en/9.1) |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +Via composer |
| 26 | + |
| 27 | +``` |
| 28 | +composer require --dev hrodic/php-integration-testing |
| 29 | +``` |
| 30 | + |
| 31 | +## Configuration |
| 32 | + |
| 33 | +On PHPUnit configuration XML file you must specify the extension with its configuration. |
| 34 | + |
| 35 | +If you need help with PHPUnit extensions, please refer to the [Official Documentation](https://phpunit.readthedocs.io/en/9.1/configuration.html#the-extensions-element) |
| 36 | + |
| 37 | +As an example, look into phpunit-integration.xml.dist. |
| 38 | + |
| 39 | + |
| 40 | +### PDO Driver |
| 41 | + |
| 42 | +If you need to test the integration of MySQL or MariaDB, use the PDO driver extension. |
| 43 | + |
| 44 | +``` |
| 45 | +<extensions> |
| 46 | + <extension class="IntegrationTesting\PHPUnit\Runner\Extension\PDODatabaseExtension"> |
| 47 | + <arguments> |
| 48 | + <object class="IntegrationTesting\PHPUnit\Runner\Extension\PDODatabaseExtensionConfig"> |
| 49 | + <arguments> |
| 50 | + <array> |
| 51 | + <element key="BEFORE_FIRST_TEST_PDO_FIXTURES_PATH"> |
| 52 | + <string>tests/fixtures/before-first-test</string> |
| 53 | + </element> |
| 54 | + <element key="BEFORE_TEST_PDO_FIXTURES_PATH"> |
| 55 | + <string>tests/fixtures/before-test</string> |
| 56 | + </element> |
| 57 | + <element key="AFTER_TEST_PDO_FIXTURES_PATH"> |
| 58 | + <string>tests/fixtures/after-test</string> |
| 59 | + </element> |
| 60 | + <element key="AFTER_LAST_TEST_PDO_FIXTURES_PATH"> |
| 61 | + <string>tests/fixtures/after-last-test</string> |
| 62 | + </element> |
| 63 | + </array> |
| 64 | + </arguments> |
| 65 | + </object> |
| 66 | + <object class="IntegrationTesting\Driver\PDOConnection"> |
| 67 | + <arguments> |
| 68 | + <string>mysql:host=localhost:3306;dbname=test;charset=utf8</string> |
| 69 | + <string>test</string> |
| 70 | + <string>test</string> |
| 71 | + </arguments> |
| 72 | + </object> |
| 73 | + </arguments> |
| 74 | + </extension> |
| 75 | +</extensions> |
| 76 | +``` |
| 77 | + |
| 78 | +The extension class is |
| 79 | +``` |
| 80 | +IntegrationTesting\PHPUnit\Runner\Extension\PDODatabaseExtension |
| 81 | +``` |
| 82 | + |
| 83 | +which requires a configuration and a PDO connection as arguments via XML config |
| 84 | + |
| 85 | +The configuration class allows you to define in which paths your fixtures will be located |
| 86 | + |
| 87 | +``` |
| 88 | +IntegrationTesting\PHPUnit\Runner\Extension\PDODatabaseExtensionConfig |
| 89 | +``` |
| 90 | + |
| 91 | +The config keys to define each hook type are: |
| 92 | + |
| 93 | +* BEFORE_FIRST_TEST_PDO_FIXTURES_PATH |
| 94 | +* BEFORE_TEST_PDO_FIXTURES_PATH |
| 95 | +* AFTER_TEST_PDO_FIXTURES_PATH |
| 96 | +* AFTER_LAST_TEST_PDO_FIXTURES_PATH |
| 97 | + |
| 98 | +The PDOConnection class is just a wrapper of the PDO PHP class. |
| 99 | + |
| 100 | +``` |
| 101 | +IntegrationTesting\Driver\PDOConnection |
| 102 | +``` |
| 103 | + |
| 104 | +It requires DSN, username and password of your database. |
| 105 | + |
| 106 | +### RabbitMQ driver |
| 107 | + |
| 108 | +@todo |
| 109 | + |
| 110 | +## Fixture creation |
| 111 | + |
| 112 | +A PDO fixture is just an SQL file. |
| 113 | + |
| 114 | +All the fixtures located in a specific hook category will be executed in order and inside a transaction. |
| 115 | + |
| 116 | +How you create the SQL and the integrity of the database in each stage is up to you. The library does not force you |
| 117 | +to follow any convention although is common to setup fixtures at the beginning and clean your mess after each test. |
| 118 | + |
| 119 | +You can create, insert, delete or whatever you configure your user to do. Remember, your testing database must be isolated |
| 120 | +from any real database! |
| 121 | + |
| 122 | +All four fixture hook types could be placed in the directory that you prefer. |
| 123 | + |
| 124 | +For BeforeTest and AfterTest hooks, which occur in every specific test, you can also provide specific fixtures to be executed |
| 125 | +just after the generic Before and After fixtures by implementing the interfaces WithBeforeTestFixtureName and/or WithAfterTestFixtureName. |
| 126 | + |
| 127 | +``` |
| 128 | +final class YourIntegrationTest extends TestCase implements WithBeforeTestFixtureName, WithAfterTestFixtureName |
| 129 | +{ |
| 130 | + private const FIXTURE_NAME = 'pdo-integration-test'; |
| 131 | +
|
| 132 | + public static function getAfterTestFixtureName(): string |
| 133 | + { |
| 134 | + return self::FIXTURE_NAME; |
| 135 | + } |
| 136 | +
|
| 137 | + public static function getBeforeTestFixtureName(): string |
| 138 | + { |
| 139 | + return self::FIXTURE_NAME; |
| 140 | + } |
| 141 | +
|
| 142 | + public function testYourRepositoryHere(): void |
| 143 | + { |
| 144 | + // arrange |
| 145 | + // act |
| 146 | + // assert against real database (your fixtures are already there!) |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +The Extension will check if the methods are defined, and use them to locate subdirectories inside the main |
| 152 | +BEFORE_TEST_PDO_FIXTURES_PATH and AFTER_TEST_PDO_FIXTURES_PATH directories. |
| 153 | + |
| 154 | +### Execution flow |
| 155 | + |
| 156 | +If you take a look onto the tests/fixtures folder, you will see an example on how you can organize your fixtures. |
| 157 | +You can have multiple SQL files and the extension will read and execute them in order. |
| 158 | + |
| 159 | +``` |
| 160 | +├── after-last-test # AFTER_LAST_TEST_PDO_FIXTURES_PATH, executed once, at the end |
| 161 | +│ └── 01.sql |
| 162 | +├── after-test # AFTER_TEST_PDO_FIXTURES_PATH, executed after each test |
| 163 | +│ ├── 01.sql |
| 164 | +│ └── pdo-integration-test # executed after each test inside the class that defines this fixture name |
| 165 | +│ └── 01.sql |
| 166 | +├── before-first-test # BEFORE_FIRST_TEST_PDO_FIXTURES_PATH, executed once, at the beginning |
| 167 | +│ └── 01.sql |
| 168 | +└── before-test # BEFORE_TEST_PDO_FIXTURES_PATH, executed before each test |
| 169 | + ├── 01.sql |
| 170 | + └── pdo-integration-test # executed before each test inside the class that defines this fixture name |
| 171 | + └── 01.sql |
| 172 | +``` |
| 173 | + |
| 174 | +## Troubleshooting |
| 175 | + |
| 176 | +Integration testing requires some infrastructure to be in place. |
| 177 | + |
| 178 | +This library assumes (you can check docker-compose.yml file for inspiration) that you have an accessible database |
| 179 | +or other infrastructure already in place and the database is created. |
0 commit comments