MySqlDbTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. require_once \Codeception\Configuration::testsDir().'unit/Codeception/Module/Db/TestsForDb.php';
  3. /**
  4. * @group appveyor
  5. * @group db
  6. */
  7. class MySqlDbTest extends TestsForDb
  8. {
  9. public function getPopulator()
  10. {
  11. if (getenv('APPVEYOR')) {
  12. $this->markTestSkipped('Disabled on Appveyor');
  13. }
  14. if (getenv('WERCKER_ROOT')) {
  15. $this->markTestSkipped('Disabled on Wercker CI');
  16. }
  17. $config = $this->getConfig();
  18. $password = $config['password'] ? '-p'.$config['password'] : '';
  19. return "mysql -u \$user $password \$dbname < {$config['dump']}";
  20. }
  21. public function getConfig()
  22. {
  23. return [
  24. 'dsn' => 'mysql:host=localhost;dbname=codeception_test',
  25. 'user' => 'root',
  26. 'password' => getenv('APPVEYOR') ? 'Password12!' : '',
  27. 'dump' => 'tests/data/dumps/mysql.sql',
  28. 'reconnect' => true,
  29. 'cleanup' => true,
  30. 'populate' => true
  31. ];
  32. }
  33. /**
  34. * Overriden, Using MYSQL CONNECTION_ID to get current connection
  35. */
  36. public function testConnectionIsResetOnEveryTestWhenReconnectIsTrue()
  37. {
  38. $testCase1 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  39. $testCase2 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  40. $testCase3 = \Codeception\Util\Stub::makeEmpty('\Codeception\TestInterface');
  41. $this->module->_setConfig(['reconnect' => false]);
  42. $this->module->_beforeSuite();
  43. // Simulate a test that runs
  44. $this->module->_before($testCase1);
  45. $connection1 = $this->module->dbh->query('SELECT CONNECTION_ID()')->fetch(PDO::FETCH_COLUMN);
  46. $this->module->_after($testCase1);
  47. // Simulate a second test that runs
  48. $this->module->_before($testCase2);
  49. $connection2 = $this->module->dbh->query('SELECT CONNECTION_ID()')->fetch(PDO::FETCH_COLUMN);
  50. $this->module->_after($testCase2);
  51. $this->module->_afterSuite();
  52. $this->module->_setConfig(['reconnect' => true]);
  53. $this->module->_before($testCase3);
  54. $connection3 = $this->module->dbh->query('SELECT CONNECTION_ID()')->fetch(PDO::FETCH_COLUMN);
  55. $this->module->_after($testCase3);
  56. $this->assertEquals($connection1, $connection2);
  57. $this->assertNotEquals($connection3, $connection2);
  58. }
  59. public function testGrabColumnFromDatabase()
  60. {
  61. $emails = $this->module->grabColumnFromDatabase('users', 'email');
  62. $this->assertEquals(
  63. [
  64. 'davert@mail.ua',
  65. 'nick@mail.ua',
  66. 'miles@davis.com',
  67. 'charlie@parker.com',
  68. ],
  69. $emails);
  70. }
  71. }