CWebTestCase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * This file contains the CWebTestCase 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. require_once('PHPUnit/Extensions/SeleniumTestCase.php');
  12. /**
  13. * CWebTestCase is the base class for Web-based functional test case classes.
  14. *
  15. * It extends PHPUnit_Extensions_SeleniumTestCase and provides the database
  16. * fixture management feature like {@link CDbTestCase}.
  17. *
  18. * @property CDbFixtureManager $fixtureManager The database fixture manager.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @package system.test
  22. * @since 1.1
  23. */
  24. abstract class CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase
  25. {
  26. /**
  27. * @var array a list of fixtures that should be loaded before each test method executes.
  28. * The array keys are fixture names, and the array values are either AR class names
  29. * or table names. If table names, they must begin with a colon character (e.g. 'Post'
  30. * means an AR class, while ':Post' means a table name).
  31. * Defaults to false, meaning fixtures will not be used at all.
  32. */
  33. protected $fixtures=false;
  34. /**
  35. * PHP magic method.
  36. * This method is overridden so that named fixture data can be accessed like a normal property.
  37. * @param string $name the property name
  38. * @throws Exception if unknown property is used
  39. * @return mixed the property value
  40. */
  41. public function __get($name)
  42. {
  43. if(is_array($this->fixtures) && ($rows=$this->getFixtureManager()->getRows($name))!==false)
  44. return $rows;
  45. else
  46. throw new Exception("Unknown property '$name' for class '".get_class($this)."'.");
  47. }
  48. /**
  49. * PHP magic method.
  50. * This method is overridden so that named fixture ActiveRecord instances can be accessed in terms of a method call.
  51. * @param string $name method name
  52. * @param string $params method parameters
  53. * @return mixed the property value
  54. */
  55. public function __call($name,$params)
  56. {
  57. if(is_array($this->fixtures) && isset($params[0]) && ($record=$this->getFixtureManager()->getRecord($name,$params[0]))!==false)
  58. return $record;
  59. else
  60. return parent::__call($name,$params);
  61. }
  62. /**
  63. * @return CDbFixtureManager the database fixture manager
  64. */
  65. public function getFixtureManager()
  66. {
  67. return Yii::app()->getComponent('fixture');
  68. }
  69. /**
  70. * @param string $name the fixture name (the key value in {@link fixtures}).
  71. * @return array the named fixture data
  72. */
  73. public function getFixtureData($name)
  74. {
  75. return $this->getFixtureManager()->getRows($name);
  76. }
  77. /**
  78. * @param string $name the fixture name (the key value in {@link fixtures}).
  79. * @param string $alias the alias of the fixture data row
  80. * @return CActiveRecord the ActiveRecord instance corresponding to the specified alias in the named fixture.
  81. * False is returned if there is no such fixture or the record cannot be found.
  82. */
  83. public function getFixtureRecord($name,$alias)
  84. {
  85. return $this->getFixtureManager()->getRecord($name,$alias);
  86. }
  87. /**
  88. * Sets up the fixture before executing a test method.
  89. * If you override this method, make sure the parent implementation is invoked.
  90. * Otherwise, the database fixtures will not be managed properly.
  91. */
  92. protected function setUp()
  93. {
  94. parent::setUp();
  95. if(is_array($this->fixtures))
  96. $this->getFixtureManager()->load($this->fixtures);
  97. }
  98. }