BasePage.php 2.11 KB
Newer Older
1 2 3 4
<?php

namespace yii\codeception;

5 6 7 8
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;

9
/**
10
 * BasePage is the base class for page classes that represent Web pages to be tested.
11
 *
12
 * @property string $url The URL to this page. This property is read-only.
13 14 15 16
 *
 * @author Mark Jebri <mark.github@yandex.ru>
 * @since 2.0
 */
17
abstract class BasePage extends Component
18
{
19
	/**
20 21 22
	 * @var string|array the route (controller ID and action ID, e.g. `site/about`) to this page.
	 * Use array to represent a route with GET parameters. The first element of the array represents
	 * the route and the rest of the name-value pairs are treated as GET parameters, e.g. `array('site/page', 'name' => 'about')`.
23
	 */
24
	public $route;
25
	/**
26
	 * @var \Codeception\AbstractGuy the testing guy object
27
	 */
28 29
	protected $guy;

30 31 32 33
	/**
	 * Constructor.
	 * @param \Codeception\AbstractGuy the testing guy object
	 */
34 35 36 37
	public function __construct($I)
	{
		$this->guy = $I;
	}
38 39

	/**
40 41 42 43 44 45
	 * Returns the URL to this page.
	 * The URL will be returned by calling the URL manager of the application
	 * with [[route]] and the provided parameters.
	 * @param array $params the GET parameters for creating the URL
	 * @return string the URL to this page
	 * @throws InvalidConfigException if [[route]] is not set or invalid
46
	 */
47
	public function getUrl($params = [])
48
	{
49 50 51 52 53 54 55 56 57 58
		if (is_string($this->route)) {
			return Yii::$app->getUrlManager()->createUrl($this->route, $params);
		} elseif (is_array($this->route) && isset($this->route[0])) {
			$route = $this->route[0];
			$ps = $this->route;
			unset($this->route[0]);
			return Yii::$app->getUrlManager()->createUrl($route, array_merge($ps, $params));
		} else {
			throw new InvalidConfigException('The "route" property must be set.');
		}
59 60 61
	}

	/**
62 63 64 65
	 * Creates a page instance and sets the test guy to use [[url]].
	 * @param \Codeception\AbstractGuy $I the test guy instance
	 * @param array $params the GET parameters to be used to generate [[url]]
	 * @return static the page instance
66
	 */
67
	public static function openBy($I, $params = [])
68
	{
69 70 71
		$page = new static($I);
		$I->amOnPage($page->getUrl($params));
		return $page;
72 73
	}
}