BasePage.php 2.38 KB
Newer Older
1
<?php
Carsten Brandt committed
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
7 8 9

namespace yii\codeception;

10 11 12 13
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;

14
/**
15
 * BasePage is the base class for page classes that represent Web pages to be tested.
16
 *
17
 * @property string $url The URL to this page. This property is read-only.
18 19 20 21
 *
 * @author Mark Jebri <mark.github@yandex.ru>
 * @since 2.0
 */
22
abstract class BasePage extends Component
23
{
24 25 26 27 28 29
    /**
     * @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')`.
     */
    public $route;
30

31
    /**
32
     * @var \Codeception\Actor the testing guy object
33
     */
34
    protected $actor;
35

36

37 38
    /**
     * Constructor.
39
     *
40
     * @param \Codeception\Actor $I the testing guy object
41 42 43
     */
    public function __construct($I)
    {
44
        $this->actor = $I;
45
    }
46

47 48 49 50
    /**
     * 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.
51 52
     * @param array $params the GET parameters for creating the URL
     * @return string the URL to this page
53 54 55 56 57 58
     * @throws InvalidConfigException if [[route]] is not set or invalid
     */
    public function getUrl($params = [])
    {
        if (is_string($this->route)) {
            $params[0] = $this->route;
59

60 61 62 63 64 65 66 67 68 69
            return Yii::$app->getUrlManager()->createUrl($params);
        } elseif (is_array($this->route) && isset($this->route[0])) {
            return Yii::$app->getUrlManager()->createUrl(array_merge($this->route, $params));
        } else {
            throw new InvalidConfigException('The "route" property must be set.');
        }
    }

    /**
     * Creates a page instance and sets the test guy to use [[url]].
70
     * @param \Codeception\Actor $I the test guy instance
71 72
     * @param array $params the GET parameters to be used to generate [[url]]
     * @return static the page instance
73 74 75 76 77 78 79 80
     */
    public static function openBy($I, $params = [])
    {
        $page = new static($I);
        $I->amOnPage($page->getUrl($params));

        return $page;
    }
81
}