SqliteDbTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. use Codeception\Util\ActionSequence;
  3. require_once \Codeception\Configuration::testsDir().'unit/Codeception/Module/Db/TestsForDb.php';
  4. /**
  5. * @group appveyor
  6. * @group db
  7. * Class SqliteDbTest
  8. */
  9. class SqliteDbTest extends TestsForDb
  10. {
  11. public function getPopulator()
  12. {
  13. if (getenv('APPVEYOR')) {
  14. $this->markTestSkipped('Disabled on Appveyor');
  15. }
  16. if (getenv('WERCKER_ROOT')) {
  17. $this->markTestSkipped('Disabled on Wercker CI');
  18. }
  19. $this->markTestSkipped('Currently Travis CI uses old SQLite :(');
  20. $config = $this->getConfig();
  21. @chmod('tests/data/sqlite.db', 0777);
  22. return 'cat '. $config['dump'] .' | sqlite3 tests/data/sqlite.db';
  23. }
  24. public function getConfig()
  25. {
  26. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  27. $dumpFile = 'dumps/sqlite-54.sql';
  28. } else {
  29. $dumpFile = 'dumps/sqlite.sql';
  30. }
  31. return [
  32. 'dsn' => 'sqlite:tests/data/sqlite.db',
  33. 'user' => 'root',
  34. 'password' => '',
  35. 'dump' => 'tests/data/' . $dumpFile,
  36. 'reconnect' => true,
  37. 'cleanup' => true,
  38. 'populate' => true
  39. ];
  40. }
  41. public function testConnectionIsResetOnEveryTestWhenReconnectIsTrue()
  42. {
  43. $testCase1 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  44. $testCase2 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  45. $testCase3 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  46. $this->module->_setConfig(['reconnect' => false]);
  47. $this->module->_beforeSuite();
  48. // Simulate a test that runs
  49. $this->module->_before($testCase1);
  50. $connection1 = spl_object_hash($this->module->dbh);
  51. $this->module->_after($testCase1);
  52. // Simulate a second test that runs
  53. $this->module->_before($testCase2);
  54. $connection2 = spl_object_hash($this->module->dbh);
  55. $this->module->_after($testCase2);
  56. $this->module->_afterSuite();
  57. $this->module->_setConfig(['reconnect' => true]);
  58. $this->module->_before($testCase3);
  59. $connection3 = spl_object_hash($this->module->dbh);
  60. $this->module->_after($testCase3);
  61. $this->assertEquals($connection1, $connection2);
  62. $this->assertNotEquals($connection3, $connection2);
  63. }
  64. public function testMultiDatabase()
  65. {
  66. $config = array_merge($this->getConfig(), [
  67. 'dsn' => 'sqlite:tests/data/sqlite1.db',
  68. 'cleanup' => false
  69. ]);
  70. $this->module->_reconfigure(
  71. [
  72. 'databases' => ['db2' => $config],
  73. ]
  74. );
  75. $this->module->_beforeSuite();
  76. $testDataInDb1 = ['name' => 'userdb1', 'email' => 'userdb1@example.org'];
  77. $testDataInDb2 = ['name' => 'userdb2', 'email' => 'userdb2@example.org'];
  78. $this->module->_insertInDatabase('users', $testDataInDb1);
  79. $this->module->seeInDatabase('users', $testDataInDb1);
  80. $this->module->amConnectedToDatabase('db2');
  81. $this->module->_insertInDatabase('users', $testDataInDb2);
  82. $this->module->seeInDatabase('users', $testDataInDb2);
  83. $this->module->dontSeeInDatabase('users', $testDataInDb1);
  84. }
  85. public function testDatabaseIsAlwaysDefaultBeforeTest()
  86. {
  87. $config = array_merge($this->getConfig(), ['dsn' => 'sqlite:tests/data/sqlite1.db']);
  88. $this->module->_reconfigure(
  89. [
  90. 'cleanup' => false,
  91. 'databases' => ['db2' => $config],
  92. ]
  93. );
  94. $this->module->_beforeSuite();
  95. $testCase1 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  96. $testCase2 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  97. $testDataInDb1 = ['name' => 'userdb1', 'email' => 'userdb1@example.org'];
  98. $testDataInDb2 = ['name' => 'userdb2', 'email' => 'userdb2@example.org'];
  99. // Simulate a test that runs
  100. $this->module->_before($testCase1);
  101. $this->module->_insertInDatabase('users', $testDataInDb1);
  102. $this->module->seeInDatabase('users', $testDataInDb1);
  103. $this->module->amConnectedToDatabase('db2');
  104. $this->module->_insertInDatabase('users', $testDataInDb2);
  105. $this->module->seeInDatabase('users', $testDataInDb2);
  106. $this->module->_after($testCase1);
  107. // Simulate a second test that runs
  108. $this->module->_before($testCase2);
  109. $this->module->dontSeeInDatabase('users', $testDataInDb2);
  110. $this->module->seeInDatabase('users', $testDataInDb1);
  111. $this->module->_after($testCase2);
  112. $this->module->_afterSuite();
  113. }
  114. public function testMultiDatabaseWithArray()
  115. {
  116. $config = array_merge($this->getConfig(), [
  117. 'dsn' => 'sqlite:tests/data/sqlite1.db',
  118. 'cleanup' => false
  119. ]);
  120. $this->module->_reconfigure(
  121. [
  122. 'databases' => ['db2' => $config],
  123. ]
  124. );
  125. $this->module->_beforeSuite();
  126. $testDataInDb1 = ['name' => 'userdb1', 'email' => 'userdb1@example.org'];
  127. $testDataInDb2 = ['name' => 'userdb2', 'email' => 'userdb2@example.org'];
  128. $this->module->_insertInDatabase('users', $testDataInDb1);
  129. $this->module->performInDatabase('db2', [
  130. 'haveInDatabase' => ['users', $testDataInDb2],
  131. 'seeInDatabase' => ['users', $testDataInDb2],
  132. ]);
  133. $this->module->seeInDatabase('users', $testDataInDb1);
  134. $this->module->dontSeeInDatabase('users', $testDataInDb2);
  135. }
  136. public function testMultiDatabaseWithActionSequence()
  137. {
  138. $config = array_merge($this->getConfig(), [
  139. 'dsn' => 'sqlite:tests/data/sqlite1.db',
  140. 'cleanup' => false
  141. ]);
  142. $this->module->_reconfigure(
  143. [
  144. 'databases' => ['db2' => $config],
  145. ]
  146. );
  147. $this->module->_beforeSuite();
  148. $testDataInDb1 = ['name' => 'userdb1', 'email' => 'userdb1@example.org'];
  149. $testDataInDb2 = ['name' => 'userdb2', 'email' => 'userdb2@example.org'];
  150. $this->module->_insertInDatabase('users', $testDataInDb1);
  151. $this->module->performInDatabase('db2', ActionSequence::build()
  152. ->haveInDatabase('users', $testDataInDb2)
  153. ->seeInDatabase('users', $testDataInDb2)
  154. );
  155. $this->module->seeInDatabase('users', $testDataInDb1);
  156. $this->module->dontSeeInDatabase('users', $testDataInDb2);
  157. }
  158. public function testMultiDatabaseWithAnonymousFunction()
  159. {
  160. $config = array_merge($this->getConfig(), [
  161. 'dsn' => 'sqlite:tests/data/sqlite1.db',
  162. 'cleanup' => false
  163. ]);
  164. $this->module->_reconfigure(
  165. [
  166. 'databases' => ['db2' => $config],
  167. ]
  168. );
  169. $this->module->_beforeSuite();
  170. $testDataInDb1 = ['name' => 'userdb1', 'email' => 'userdb1@example.org'];
  171. $testDataInDb2 = ['name' => 'userdb2', 'email' => 'userdb2@example.org'];
  172. $this->module->_insertInDatabase('users', $testDataInDb1);
  173. $this->module->performInDatabase('db2', function ($module) use ($testDataInDb2) {
  174. $module->_insertInDatabase('users', $testDataInDb2);
  175. $module->seeInDatabase('users', $testDataInDb2);
  176. });
  177. $this->module->seeInDatabase('users', $testDataInDb1);
  178. $this->module->dontSeeInDatabase('users', $testDataInDb2);
  179. }
  180. public function testMultiDatabaseWithUnknowDatabase()
  181. {
  182. $this->expectException(Codeception\Exception\ModuleConfigException::class);
  183. $this->module->amConnectedToDatabase('db2');
  184. }
  185. public function testMultiDatabaseWithRemoveInserted()
  186. {
  187. $testCase1 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  188. $testCase2 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  189. $config = array_merge($this->getConfig(), [
  190. 'dsn' => 'sqlite:tests/data/sqlite1.db',
  191. 'cleanup' => false
  192. ]);
  193. $this->module->_reconfigure(
  194. [
  195. 'databases' => ['db2' => $config],
  196. ]
  197. );
  198. $this->module->_beforeSuite();
  199. $this->module->_before($testCase1);
  200. $testDataInDb1 = ['name' => 'userdb1', 'email' => 'userdb1@example.org'];
  201. $testDataInDb2 = ['name' => 'userdb2', 'email' => 'userdb2@example.org'];
  202. $this->module->haveInDatabase('users', $testDataInDb1);
  203. $this->module->seeInDatabase('users', $testDataInDb1);
  204. $this->module->amConnectedToDatabase('db2', function ($module) use ($testDataInDb2) {
  205. $module->haveInDatabase('users', $testDataInDb2);
  206. $module->seeInDatabase('users', $testDataInDb2);
  207. });
  208. $this->module->_after($testCase1);
  209. $this->module->_before($testCase2);
  210. $this->module->dontSeeInDatabase('users', $testDataInDb1);
  211. $this->module->amConnectedToDatabase('db2', function ($module) use ($testDataInDb2) {
  212. $module->dontSeeInDatabase('users', $testDataInDb2);
  213. });
  214. $this->module->_after($testCase2);
  215. }
  216. }