Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. # Unit testing model using DBUnit PHPUnit extension
  2.  
  3. I assume, that you already have `phpunit` installed and added into `PATH`.
  4. Create `guestbook` database in `mysql`.
  5. Create `guestbook` table in that database.
  6.  
  7. ## Create model to test
  8. File `src\Guestbook.php`
  9. ```php
  10. <?php
  11.  
  12. namespace app\models;
  13.  
  14. use \PDO;
  15.  
  16. const DB_DSN = 'mysql:dbname=guestbook;host=localhost';
  17. const DB_USER = 'root';
  18. const DB_PASSWD = '';
  19.  
  20. class Guestbook
  21. {
  22. public $id;
  23. public $content;
  24. public $user;
  25. public $created;
  26.  
  27. protected $db;
  28.  
  29. public function __construct()
  30. {
  31. $options = [
  32. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  33. ];
  34.  
  35. $this->db = new PDO(DB_DSN, DB_USER, DB_PASSWD, $options);
  36. }
  37.  
  38. /**
  39. * Adds current instance into database.
  40. * Populates id with newly inserted row id.
  41. */
  42. public function add()
  43. {
  44. $sql = "insert into guestbook set `content`=:pContent, `user`=:pUser, `created`=:pCreated";
  45. $sth = $this->db->prepare($sql);
  46. $sth->execute(
  47. [
  48. ':pContent' => $this->content,
  49. ':pUser' => $this->user,
  50. ':pCreated' => $this->created
  51. ]
  52. );
  53. $this->id = $this->db->lastInsertId();
  54. }
  55. }
  56. ```
  57.  
  58. ## Preparing fixture
  59. File `tests/_data/guestbook.yml`:
  60. ```yaml
  61. guestbook:
  62. -
  63. id: 1
  64. content: "Hello buddy!"
  65. user: "joe"
  66. created: "2016-12-11 09:30:50"
  67. -
  68. id: 2
  69. content: "I like it!"
  70. user: "jane"
  71. created: "2016-12-10 11:20:15"
  72. ```
  73.  
  74. ## PHPUnit configuration file
  75. I enabled output colors and verbosity in configuration.
  76. Also created global variables to use in test classes to connect to the database.
  77. File `tests/phpunit.xml`
  78. ```xml
  79. <phpunit colors="true" verbose="true">
  80. <php>
  81. <includePath>src</includePath>
  82.  
  83. <var name="DB_DSN" value="mysql:dbname=guestbook;host=localhost"/>
  84. <var name="DB_NAME" value="guestbook"/>
  85. <var name="DB_USER" value="root"/>
  86. <var name="DB_PASSWD" value=""/>
  87. </php>
  88.  
  89. <testsuites>
  90. <testsuite name="GuestBook">
  91. <directory>tests</directory>
  92. </testsuite>
  93. </testsuites>
  94. </phpunit>
  95. ```
  96.  
  97. ## Test class
  98. File `tests/GuestbookTest.php`
  99. ```php
  100. <?php
  101.  
  102. // includePath configured in phpunit.xml, so following will work
  103. include_once 'Guestbook.php';
  104.  
  105. // if no includePath configured, then use following
  106. // include_once 'src/Guestbook.php';
  107.  
  108. use app\models\Guestbook;
  109.  
  110. class GuestbookTest extends PHPUnit_Extensions_Database_TestCase
  111. {
  112.  
  113. static private $pdo = null;
  114.  
  115. private $conn = null;
  116.  
  117. public function getConnection()
  118. {
  119. if (!$this->conn) {
  120. if (!self::$pdo) {
  121. self::$pdo = new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
  122. }
  123. $this->conn = $this->createDefaultDBConnection(self::$pdo, $GLOBALS['DB_NAME']);
  124. }
  125.  
  126. return $this->conn;
  127. }
  128.  
  129. public function getDataSet()
  130. {
  131. // fill tables with data
  132. return new PHPUnit_Extensions_Database_DataSet_YamlDataSet(__DIR__ . '/_data/guestbook.yml');
  133. }
  134.  
  135. public function testGuestbookHasTwoRows()
  136. {
  137. // there should be 2 rows in guestbook table
  138. $this->assertEquals(2, $this->getConnection()->getRowCount('guestbook'));
  139. }
  140.  
  141. public function testGuestbookAddThirdRow()
  142. {
  143. // add new row into guestbook table
  144. $gb = new Guestbook();
  145. $gb->content = 'Third';
  146. $gb->user = 'john';
  147. $gb->created = '2016-12-11 10:00:00';
  148. $gb->add();
  149.  
  150. // now there should be 3 rows in guestbook table
  151. $this->assertEquals(3, $this->getConnection()->getRowCount('guestbook'));
  152.  
  153. // get last inserted rows - 3rd row
  154. $queryTable = $this->getConnection()->createQueryTable('guestbook', 'select * from guestbook');
  155. $lastRow = $queryTable->getRow(2); // get 3rd row
  156.  
  157. // compare last row fields with guestbook object properties
  158. $this->assertEquals($gb->id, $lastRow['id']);
  159. $this->assertEquals($gb->content, $lastRow['content']);
  160. $this->assertEquals($gb->user, $lastRow['user']);
  161. $this->assertEquals($gb->created, $lastRow['created']);
  162. }
  163.  
  164. }
  165. ```
  166.  
  167. ## Run tests
  168. Change directory into project folder, then execute following commands:
  169. ```sh
  170. phpunit
  171. ```
  172.  
  173. Sample output:
  174. ```
  175. PHPUnit 5.7.2 by Sebastian Bergmann and contributors.
  176.  
  177. Runtime: PHP 5.6.23 with Xdebug 2.4.0
  178. Configuration: F:\OpenServer\domains\t2\phpunit.xml
  179.  
  180. .. 2 / 2 (100%)
  181.  
  182. Time: 464 ms, Memory: 9.50MB
  183.  
  184. OK (2 tests, 6 assertions)
  185. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement