InitDbFixture.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\test;
  8. use Yii;
  9. /**
  10. * InitDbFixture represents the initial state needed for DB-related tests.
  11. *
  12. * Its main task is to toggle integrity check of the database during data loading.
  13. * This is needed by other DB-related fixtures (e.g. [[ActiveFixture]]) so that they can populate
  14. * data into the database without triggering integrity check errors.
  15. *
  16. * Besides, DbFixture also attempts to load an [[initScript|initialization script]] if it exists.
  17. *
  18. * You should normally use InitDbFixture to prepare a skeleton test database.
  19. * Other DB fixtures will then add specific tables and data to this database.
  20. *
  21. * For more details and usage information on InitDbFixture, see the [guide article on fixtures](guide:test-fixtures).
  22. *
  23. * @author Qiang Xue <qiang.xue@gmail.com>
  24. * @since 2.0
  25. */
  26. class InitDbFixture extends DbFixture
  27. {
  28. /**
  29. * @var string the init script file that should be executed when loading this fixture.
  30. * This should be either a file path or [path alias](guide:concept-aliases). Note that if the file does not exist,
  31. * no error will be raised.
  32. */
  33. public $initScript = '@app/tests/fixtures/initdb.php';
  34. /**
  35. * @var array list of database schemas that the test tables may reside in. Defaults to
  36. * `['']`, meaning using the default schema (an empty string refers to the
  37. * default schema). This property is mainly used when turning on and off integrity checks
  38. * so that fixture data can be populated into the database without causing problem.
  39. */
  40. public $schemas = [''];
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function beforeLoad()
  45. {
  46. $this->checkIntegrity(false);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function afterLoad()
  52. {
  53. $this->checkIntegrity(true);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function load()
  59. {
  60. $file = Yii::getAlias($this->initScript);
  61. if (is_file($file)) {
  62. require $file;
  63. }
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function beforeUnload()
  69. {
  70. $this->checkIntegrity(false);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function afterUnload()
  76. {
  77. $this->checkIntegrity(true);
  78. }
  79. /**
  80. * Toggles the DB integrity check.
  81. * @param bool $check whether to turn on or off the integrity check.
  82. */
  83. public function checkIntegrity($check)
  84. {
  85. if (!$this->db instanceof \yii\db\Connection) {
  86. return;
  87. }
  88. foreach ($this->schemas as $schema) {
  89. $this->db->createCommand()->checkIntegrity($check, $schema)->execute();
  90. }
  91. }
  92. }