82 líneas
2.4KB

  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\codeception;
  8. use Yii;
  9. use yii\base\Component;
  10. use yii\base\InvalidConfigException;
  11. /**
  12. * BasePage is the base class for page classes that represent Web pages to be tested.
  13. *
  14. * @property string $url The URL to this page. This property is read-only.
  15. *
  16. * @author Mark Jebri <mark.github@yandex.ru>
  17. * @since 2.0
  18. */
  19. abstract class BasePage extends Component
  20. {
  21. /**
  22. * @var string|array the route (controller ID and action ID, e.g. `site/about`) to this page.
  23. * Use array to represent a route with GET parameters. The first element of the array represents
  24. * the route and the rest of the name-value pairs are treated as GET parameters, e.g. `array('site/page', 'name' => 'about')`.
  25. */
  26. public $route;
  27. /**
  28. * @var \Codeception\Actor the testing guy object
  29. */
  30. protected $actor;
  31. /**
  32. * Constructor.
  33. *
  34. * @param \Codeception\Actor $I the testing guy object
  35. */
  36. public function __construct($I)
  37. {
  38. $this->actor = $I;
  39. }
  40. /**
  41. * Returns the URL to this page.
  42. * The URL will be returned by calling the URL manager of the application
  43. * with [[route]] and the provided parameters.
  44. * @param array $params the GET parameters for creating the URL
  45. * @return string the URL to this page
  46. * @throws InvalidConfigException if [[route]] is not set or invalid
  47. */
  48. public function getUrl($params = [])
  49. {
  50. if (is_string($this->route)) {
  51. $params[0] = $this->route;
  52. return Yii::$app->getUrlManager()->createUrl($params);
  53. } elseif (is_array($this->route) && isset($this->route[0])) {
  54. return Yii::$app->getUrlManager()->createUrl(array_merge($this->route, $params));
  55. } else {
  56. throw new InvalidConfigException('The "route" property must be set.');
  57. }
  58. }
  59. /**
  60. * Creates a page instance and sets the test guy to use [[url]].
  61. * @param \Codeception\Actor $I the test guy instance
  62. * @param array $params the GET parameters to be used to generate [[url]]
  63. * @return static the page instance
  64. */
  65. public static function openBy($I, $params = [])
  66. {
  67. $page = new static($I);
  68. $I->amOnPage($page->getUrl($params));
  69. return $page;
  70. }
  71. }