Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. use Codeception\Configuration;
  5. use Codeception\Exception\ModuleConfigException;
  6.  
  7. /**
  8. * Use Codeception event hooks to modify setup an existing database (e.g copy of live or a development database) for tests
  9. * rather than rather than dumping and reloading a test database using the DB module
  10. *
  11. * 1. Add this file to the /_support/ directory
  12. * 2. Add your SQL files to the /_data/ directory
  13. * 3. Modify Codecption.yml to enable the extension and add DSN details e.g.
  14. *
  15. * extensions:
  16. * enabled:
  17. * - Codeception\Extension\RunFailed
  18. * - SetupDatabaseExtension
  19. * modules:
  20. * config:
  21. * Db:
  22. * dsn: 'mysql:host=localhost;dbname=uyls5'
  23. * user: 'root'
  24. * password: '123456'
  25. * dump: tests/_data/dump.sql
  26. * populate: false
  27. * cleanup: false
  28. *
  29. *
  30. */
  31. class SetupDatabaseExtension extends \Codeception\Extension
  32. {
  33. /**
  34. * Events to listen to
  35. * @var array
  36. */
  37. public static $events = array(
  38. 'suite.after' => 'afterSuite',
  39. 'suite.before' => 'beforeSuite'
  40. );
  41.  
  42. /**
  43. * @var \Codeception\Module\Db
  44. */
  45. protected $db = null;
  46.  
  47.  
  48. /**
  49. *
  50. * @param \Codeception\Event\SuiteEvent $e
  51. */
  52. public function beforeSuite(\Codeception\Event\SuiteEvent $e)
  53. {
  54.  
  55. print 'Running beforeSuite()...' . PHP_EOL;
  56.  
  57. $this->db = $this->getModule('Db')->dbh;
  58.  
  59. $this->setupExample();
  60.  
  61. // $this->setupSomethingElse();
  62.  
  63. // etc.
  64.  
  65. }
  66.  
  67. /**
  68. *
  69. * @param \Codeception\Event\SuiteEvent $e
  70. */
  71. public function afterSuite(\Codeception\Event\SuiteEvent $e)
  72. {
  73. print 'Running afterSuite()...' . PHP_EOL;
  74.  
  75. $this->db = $this->getModule('Db')->dbh;
  76.  
  77. $this->cleanupExample();
  78.  
  79. // $this->cleanupSomethingElse();
  80.  
  81. // etc.
  82. }
  83.  
  84.  
  85.  
  86. /**
  87. *
  88. */
  89. protected function setupExample()
  90. {
  91.  
  92. print 'Running setupExample()...' . PHP_EOL;
  93.  
  94. $query = $this->load('example_before');
  95.  
  96. $sth = $this->db->prepare($query);
  97.  
  98. $sth->execute();
  99.  
  100.  
  101.  
  102. }
  103.  
  104. /**
  105. * Load an SQL query from a file to reset the database after the test suite has run
  106. */
  107. protected function cleanupExample()
  108. {
  109.  
  110. print 'Running cleanupExample()...' . PHP_EOL;
  111.  
  112. $query = $this->load('example_after');
  113.  
  114. $sth = $this->db->prepare($query);
  115.  
  116. $sth->execute();
  117.  
  118. }
  119.  
  120. /**
  121. * Load an sqlstring from a file stored in the _data directory
  122. * @param unknown $file
  123. * @throws ModuleConfigException
  124. * @return string sqlstring
  125. */
  126. protected function load($file)
  127. {
  128.  
  129. $path = Configuration::dataDir() . $file . '.sql';
  130.  
  131. if (!file_exists($path)) {
  132. throw new ModuleConfigException(
  133. __CLASS__,
  134. "\nFile with dump doesn't exist.\n"
  135. . "Please, check path for sql file: "
  136. . $path
  137. );
  138. }
  139.  
  140. $sqlstring = file_get_contents($path);
  141.  
  142. return $sqlstring;
  143.  
  144. }
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement