browser_test.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 779 行 · 第 1/3 页

PHP
779
字号
<?php
// $Id: browser_test.php 1624 2008-01-01 15:00:43Z pp11 $
require_once(dirname(__FILE__) . '/../autorun.php');
require_once(dirname(__FILE__) . '/../browser.php');
require_once(dirname(__FILE__) . '/../user_agent.php');
require_once(dirname(__FILE__) . '/../http.php');
require_once(dirname(__FILE__) . '/../page.php');
require_once(dirname(__FILE__) . '/../encoding.php');

Mock::generate('SimpleHttpResponse');
Mock::generate('SimplePage');
Mock::generate('SimpleForm');
Mock::generate('SimpleUserAgent');
Mock::generatePartial(
        'SimpleBrowser',
        'MockParseSimpleBrowser',
        array('_createUserAgent', '_parse'));
Mock::generatePartial(
        'SimpleBrowser',
        'MockUserAgentSimpleBrowser',
        array('_createUserAgent'));

class TestOfHistory extends UnitTestCase {

    function testEmptyHistoryHasFalseContents() {
        $history = &new SimpleBrowserHistory();
        $this->assertIdentical($history->getUrl(), false);
        $this->assertIdentical($history->getParameters(), false);
    }

    function testCannotMoveInEmptyHistory() {
        $history = &new SimpleBrowserHistory();
        $this->assertFalse($history->back());
        $this->assertFalse($history->forward());
    }

    function testCurrentTargetAccessors() {
        $history = &new SimpleBrowserHistory();
        $history->recordEntry(
                new SimpleUrl('http://www.here.com/'),
                new SimpleGetEncoding());
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.here.com/'));
        $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
    }

    function testSecondEntryAccessors() {
        $history = &new SimpleBrowserHistory();
        $history->recordEntry(
                new SimpleUrl('http://www.first.com/'),
                new SimpleGetEncoding());
        $history->recordEntry(
                new SimpleUrl('http://www.second.com/'),
                new SimplePostEncoding(array('a' => 1)));
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.second.com/'));
        $this->assertIdentical(
                $history->getParameters(),
                new SimplePostEncoding(array('a' => 1)));
    }

    function testGoingBackwards() {
        $history = &new SimpleBrowserHistory();
        $history->recordEntry(
                new SimpleUrl('http://www.first.com/'),
                new SimpleGetEncoding());
        $history->recordEntry(
                new SimpleUrl('http://www.second.com/'),
                new SimplePostEncoding(array('a' => 1)));
        $this->assertTrue($history->back());
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
        $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
    }

    function testGoingBackwardsOffBeginning() {
        $history = &new SimpleBrowserHistory();
        $history->recordEntry(
                new SimpleUrl('http://www.first.com/'),
                new SimpleGetEncoding());
        $this->assertFalse($history->back());
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
        $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
    }

    function testGoingForwardsOffEnd() {
        $history = &new SimpleBrowserHistory();
        $history->recordEntry(
                new SimpleUrl('http://www.first.com/'),
                new SimpleGetEncoding());
        $this->assertFalse($history->forward());
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
        $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
    }

    function testGoingBackwardsAndForwards() {
        $history = &new SimpleBrowserHistory();
        $history->recordEntry(
                new SimpleUrl('http://www.first.com/'),
                new SimpleGetEncoding());
        $history->recordEntry(
                new SimpleUrl('http://www.second.com/'),
                new SimplePostEncoding(array('a' => 1)));
        $this->assertTrue($history->back());
        $this->assertTrue($history->forward());
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.second.com/'));
        $this->assertIdentical(
                $history->getParameters(),
                new SimplePostEncoding(array('a' => 1)));
    }

    function testNewEntryReplacesNextOne() {
        $history = &new SimpleBrowserHistory();
        $history->recordEntry(
                new SimpleUrl('http://www.first.com/'),
                new SimpleGetEncoding());
        $history->recordEntry(
                new SimpleUrl('http://www.second.com/'),
                new SimplePostEncoding(array('a' => 1)));
        $history->back();
        $history->recordEntry(
                new SimpleUrl('http://www.third.com/'),
                new SimpleGetEncoding());
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.third.com/'));
        $this->assertIdentical($history->getParameters(), new SimpleGetEncoding());
    }

    function testNewEntryDropsFutureEntries() {
        $history = &new SimpleBrowserHistory();
        $history->recordEntry(
                new SimpleUrl('http://www.first.com/'),
                new SimpleGetEncoding());
        $history->recordEntry(
                new SimpleUrl('http://www.second.com/'),
                new SimpleGetEncoding());
        $history->recordEntry(
                new SimpleUrl('http://www.third.com/'),
                new SimpleGetEncoding());
        $history->back();
        $history->back();
        $history->recordEntry(
                new SimpleUrl('http://www.fourth.com/'),
                new SimpleGetEncoding());
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.fourth.com/'));
        $this->assertFalse($history->forward());
        $history->back();
        $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
        $this->assertFalse($history->back());
    }
}

class TestOfParsedPageAccess extends UnitTestCase {

    function &loadPage(&$page) {
        $response = &new MockSimpleHttpResponse($this);

        $agent = &new MockSimpleUserAgent($this);
        $agent->setReturnReference('fetchResponse', $response);

        $browser = &new MockParseSimpleBrowser($this);
        $browser->setReturnReference('_createUserAgent', $agent);
        $browser->setReturnReference('_parse', $page);
        $browser->SimpleBrowser();

        $browser->get('http://this.com/page.html');
        return $browser;
    }

    function testAccessorsWhenNoPage() {
        $agent = &new MockSimpleUserAgent($this);

        $browser = &new MockParseSimpleBrowser($this);
        $browser->setReturnReference('_createUserAgent', $agent);
        $browser->SimpleBrowser();

        $this->assertEqual($browser->getContent(), '');
    }

    function testParse() {
        $page = &new MockSimplePage();
        $page->setReturnValue('getRequest', "GET here.html\r\n\r\n");
        $page->setReturnValue('getRaw', 'Raw HTML');
        $page->setReturnValue('getTitle', 'Here');
        $page->setReturnValue('getFrameFocus', 'Frame');
        $page->setReturnValue('getMimeType', 'text/html');
        $page->setReturnValue('getResponseCode', 200);
        $page->setReturnValue('getAuthentication', 'Basic');
        $page->setReturnValue('getRealm', 'Somewhere');
        $page->setReturnValue('getTransportError', 'Ouch!');

        $browser = &$this->loadPage($page);

        $this->assertEqual($browser->getRequest(), "GET here.html\r\n\r\n");
        $this->assertEqual($browser->getContent(), 'Raw HTML');
        $this->assertEqual($browser->getTitle(), 'Here');
        $this->assertEqual($browser->getFrameFocus(), 'Frame');
        $this->assertIdentical($browser->getResponseCode(), 200);
        $this->assertEqual($browser->getMimeType(), 'text/html');
        $this->assertEqual($browser->getAuthentication(), 'Basic');
        $this->assertEqual($browser->getRealm(), 'Somewhere');
        $this->assertEqual($browser->getTransportError(), 'Ouch!');
    }

    function testLinkAffirmationWhenPresent() {
        $page = &new MockSimplePage();
        $page->setReturnValue('getUrlsByLabel', array('http://www.nowhere.com'));
        $page->expectOnce('getUrlsByLabel', array('a link label'));
        $browser = &$this->loadPage($page);
        $this->assertIdentical($browser->getLink('a link label'), 'http://www.nowhere.com');
    }

    function testLinkAffirmationByIdWhenPresent() {
        $page = &new MockSimplePage();
        $page->setReturnValue('getUrlById', 'a_page.com', array(99));
        $page->setReturnValue('getUrlById', false, array('*'));
        $browser = &$this->loadPage($page);
        $this->assertIdentical($browser->getLinkById(99), 'a_page.com');
        $this->assertFalse($browser->getLinkById(98));
    }

    function testSettingFieldIsPassedToPage() {
        $page = &new MockSimplePage();
        $page->expectOnce('setField', array(new SimpleByLabelOrName('key'), 'Value', false));
        $page->setReturnValue('getField', 'Value');
        $browser = &$this->loadPage($page);
        $this->assertEqual($browser->getField('key'), 'Value');
        $browser->setField('key', 'Value');
    }
}

class TestOfBrowserNavigation extends UnitTestCase {

    function &createBrowser(&$agent, &$page) {
        $browser = &new MockParseSimpleBrowser();
        $browser->setReturnReference('_createUserAgent', $agent);
        $browser->setReturnReference('_parse', $page);
        $browser->SimpleBrowser();
        return $browser;
    }

    function testClickLinkRequestsPage() {
        $agent = &new MockSimpleUserAgent();
        $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse());
        $agent->expectArgumentsAt(
                0,
                'fetchResponse',
                array(new SimpleUrl('http://this.com/page.html'), new SimpleGetEncoding()));
        $agent->expectArgumentsAt(
                1,
                'fetchResponse',
                array(new SimpleUrl('http://this.com/new.html'), new SimpleGetEncoding()));
        $agent->expectCallCount('fetchResponse', 2);

        $page = &new MockSimplePage();
        $page->setReturnValue('getUrlsByLabel', array(new SimpleUrl('http://this.com/new.html')));
        $page->expectOnce('getUrlsByLabel', array('New'));
        $page->setReturnValue('getRaw', 'A page');

        $browser = &$this->createBrowser($agent, $page);
        $browser->get('http://this.com/page.html');
        $this->assertTrue($browser->clickLink('New'));
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?