jeudi 16 mars 2017

phpunit how to test a method which uses the broswer url

I have the following methods:

public function isAdmin()
{
    $adminUrl = $this->getPage(2)->httpUrl;
    /*
     * Remove all segments except the first in the URL path
     * to detect if current page is in admin view
     */
    $currentUrl = preg_replace( // http://ift.tt/2nqtKKM
        sprintf(
            '~(?<adminUrl>%s)(?<remove>.*)$~',
            addslashes($adminUrl)
        ),
        '$1',
        $this->getPage()->httpUrl
    );
    return $adminUrl == $currentUrl || (!$currentUrl && !$this->getPage() instanceof NullPage);
}
public function getPage($page = null)
{
    $wiredPage = $this->wire('page');
    if (!is_null($wiredPage) && is_null($page)) {
        $baseUrl = $this->wire('page')->url;
        $urlSegmentStr = $this->wire('input')->urlSegmentStr;
        if (strlen($urlSegmentStr)) {
            $baseUrl = rtrim($baseUrl, '/') . "/$urlSegmentStr/";
        }
        return $this->pages->get($baseUrl);
    }

    if (is_null($page)) {
        return $this->pages->get(parse_url(getenv('REQUEST_URI'), PHP_URL_PATH));
    }

    return $this->pages->get($page);
}

I check if for an admin page by getting the url to the admin page and compare it to the current url. Now I want to write a test for it with PHPunit but I have no idea how to test a method which result is based on urls like this. Is it possible to write a test for a case like this? How would I be able to do this?

Aucun commentaire:

Enregistrer un commentaire