選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

98 行
2.5KB

  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. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @since 2.0
  23. */
  24. class InitDbFixture extends DbFixture
  25. {
  26. /**
  27. * @var string the init script file that should be executed when loading this fixture.
  28. * This should be either a file path or path alias. Note that if the file does not exist,
  29. * no error will be raised.
  30. */
  31. public $initScript = '@app/tests/fixtures/initdb.php';
  32. /**
  33. * @var array list of database schemas that the test tables may reside in. Defaults to
  34. * `['']`, meaning using the default schema (an empty string refers to the
  35. * default schema). This property is mainly used when turning on and off integrity checks
  36. * so that fixture data can be populated into the database without causing problem.
  37. */
  38. public $schemas = [''];
  39. /**
  40. * @inheritdoc
  41. */
  42. public function beforeLoad()
  43. {
  44. $this->checkIntegrity(false);
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function afterLoad()
  50. {
  51. $this->checkIntegrity(true);
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function load()
  57. {
  58. $file = Yii::getAlias($this->initScript);
  59. if (is_file($file)) {
  60. require($file);
  61. }
  62. }
  63. /**
  64. * @inheritdoc
  65. */
  66. public function beforeUnload()
  67. {
  68. $this->checkIntegrity(false);
  69. }
  70. /**
  71. * @inheritdoc
  72. */
  73. public function afterUnload()
  74. {
  75. $this->checkIntegrity(true);
  76. }
  77. /**
  78. * Toggles the DB integrity check.
  79. * @param boolean $check whether to turn on or off the integrity check.
  80. */
  81. public function checkIntegrity($check)
  82. {
  83. foreach ($this->schemas as $schema) {
  84. $this->db->createCommand()->checkIntegrity($check, $schema)->execute();
  85. }
  86. }
  87. }