CDbTestCase.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * This file contains the CDbTestCase class.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('system.test.CTestCase');
  11. /**
  12. * CDbTestCase is the base class for test cases about DB-related features.
  13. *
  14. * CDbTestCase provides database fixture management with the help of {@link CDbFixtureManager}.
  15. * By declaring {@link fixtures} property, one can ensure the specified
  16. * tables have the expected fixture state when executing each test method.
  17. * In addition, CDbTestCase provides two ways to access the fixture data.
  18. *
  19. * For example, assume we declare {@link fixtures} to be:
  20. * <pre>
  21. * public $fixtures=array(
  22. * 'posts' => 'Post',
  23. * 'comments' => 'Comment',
  24. * );
  25. * </pre>
  26. *
  27. * We can access the original fixture data rows using <code>$this->posts</code>
  28. * <code>$this->posts['first post']</code>. We can also retrieve an ActiveRecord instance
  29. * corresponding to a fixture data row using <code>$this->posts('first post')</code>.
  30. * Note, here 'first post' refers to a key to a row in the original fixture data.
  31. *
  32. *
  33. * @author Qiang Xue <qiang.xue@gmail.com>
  34. * @package system.test
  35. * @since 1.1
  36. */
  37. abstract class CDbTestCase extends CTestCase
  38. {
  39. /**
  40. * @var array a list of fixtures that should be loaded before each test method executes.
  41. * The array keys are fixture names, and the array values are either AR class names
  42. * or table names. If table names, they must begin with a colon character (e.g. 'Post'
  43. * means an AR class, while ':post' means a table name).
  44. * Defaults to false, meaning fixtures will not be used at all.
  45. */
  46. protected $fixtures=false;
  47. /**
  48. * PHP magic method.
  49. * This method is overridden so that named fixture data can be accessed like a normal property.
  50. * @param string $name the property name
  51. * @throws Exception if unknown property is used
  52. * @return mixed the property value
  53. */
  54. public function __get($name)
  55. {
  56. if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
  57. return $rows;
  58. else
  59. throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
  60. }
  61. /**
  62. * PHP magic method.
  63. * This method is overridden so that named fixture ActiveRecord instances can be accessed in terms of a method call.
  64. * @param string $name method name
  65. * @param string $params method parameters
  66. * @throws Exception if unknown method is used
  67. * @return mixed the property value
  68. */
  69. public function __call($name,$params)
  70. {
  71. if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
  72. return $record;
  73. else
  74. throw new Exception("Unknown method '$name' for class '".get_class($this)."'.");
  75. }
  76. /**
  77. * @return CDbFixtureManager the database fixture manager
  78. */
  79. public function getFixtureManager()
  80. {
  81. return Yii::app()->getComponent('fixture');
  82. }
  83. /**
  84. * @param string $name the fixture name (the key value in {@link fixtures}).
  85. * @return array the named fixture data
  86. */
  87. public function getFixtureData($name)
  88. {
  89. return $this->getFixtureManager()->getRows($name);
  90. }
  91. /**
  92. * @param string $name the fixture name (the key value in {@link fixtures}).
  93. * @param string $alias the alias of the fixture data row
  94. * @return CActiveRecord the ActiveRecord instance corresponding to the specified alias in the named fixture.
  95. * False is returned if there is no such fixture or the record cannot be found.
  96. */
  97. public function getFixtureRecord($name,$alias)
  98. {
  99. return $this->getFixtureManager()->getRecord($name,$alias);
  100. }
  101. /**
  102. * Sets up the fixture before executing a test method.
  103. * If you override this method, make sure the parent implementation is invoked.
  104. * Otherwise, the database fixtures will not be managed properly.
  105. */
  106. protected function setUp()
  107. {
  108. parent::setUp();
  109. if(is_array($this->fixtures))
  110. $this->getFixtureManager()->load($this->fixtures);
  111. }
  112. }