acceptance_test.php

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

PHP
1,633
字号
<?php
// $Id: acceptance_test.php 1700 2008-03-24 16:17:48Z lastcraft $
require_once(dirname(__FILE__) . '/../autorun.php');
require_once(dirname(__FILE__) . '/../compatibility.php');
require_once(dirname(__FILE__) . '/../browser.php');
require_once(dirname(__FILE__) . '/../web_tester.php');
require_once(dirname(__FILE__) . '/../unit_tester.php');

class SimpleTestAcceptanceTest extends WebTestCase {
    function samples() {
        return 'http://www.lastcraft.com/test/';
    }
}

class TestOfLiveBrowser extends UnitTestCase {
    function samples() {
        return SimpleTestAcceptanceTest::samples();
    }

    function testGet() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $this->assertTrue($browser->get($this->samples() . 'network_confirm.php'));
        $this->assertPattern('/target for the SimpleTest/', $browser->getContent());
        $this->assertPattern('/Request method.*?<dd>GET<\/dd>/', $browser->getContent());
        $this->assertEqual($browser->getTitle(), 'Simple test target file');
        $this->assertEqual($browser->getResponseCode(), 200);
        $this->assertEqual($browser->getMimeType(), 'text/html');
    }
    
    function testPost() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $this->assertTrue($browser->post($this->samples() . 'network_confirm.php'));
        $this->assertPattern('/target for the SimpleTest/', $browser->getContent());
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/', $browser->getContent());
    }
    
    function testAbsoluteLinkFollowing() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $browser->get($this->samples() . 'link_confirm.php');
        $this->assertTrue($browser->clickLink('Absolute'));
        $this->assertPattern('/target for the SimpleTest/', $browser->getContent());
    }
    
    function testRelativeEncodedeLinkFollowing() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $browser->get($this->samples() . 'link_confirm.php');
        $this->assertTrue($browser->clickLink("m鋜c阬 kiek'eboe"));
        $this->assertPattern('/target for the SimpleTest/', $browser->getContent());
    }
    
    function testRelativeLinkFollowing() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $browser->get($this->samples() . 'link_confirm.php');
        $this->assertTrue($browser->clickLink('Relative'));
        $this->assertPattern('/target for the SimpleTest/', $browser->getContent());
    }
    
    function testUnifiedClickLinkClicking() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $browser->get($this->samples() . 'link_confirm.php');
        $this->assertTrue($browser->click('Relative'));
        $this->assertPattern('/target for the SimpleTest/', $browser->getContent());
    }
    
    function testIdLinkFollowing() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $browser->get($this->samples() . 'link_confirm.php');
        $this->assertTrue($browser->clickLinkById(1));
        $this->assertPattern('/target for the SimpleTest/', $browser->getContent());
    }
    
    function testCookieReading() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $browser->get($this->samples() . 'set_cookies.php');
        $this->assertEqual($browser->getCurrentCookieValue('session_cookie'), 'A');
        $this->assertEqual($browser->getCurrentCookieValue('short_cookie'), 'B');
        $this->assertEqual($browser->getCurrentCookieValue('day_cookie'), 'C');
    }
    
    function testSimpleSubmit() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $browser->get($this->samples() . 'form.html');
        $this->assertTrue($browser->clickSubmit('Go!'));
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/', $browser->getContent());
        $this->assertPattern('/go=\[Go!\]/', $browser->getContent());
    }
    
    function testUnifiedClickCanSubmit() {
        $browser = &new SimpleBrowser();
        $browser->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
        $browser->get($this->samples() . 'form.html');
        $this->assertTrue($browser->click('Go!'));
        $this->assertPattern('/go=\[Go!\]/', $browser->getContent());
    }
}

class TestRadioFields extends SimpleTestAcceptanceTest {
	function testSetFieldAsInteger() {
		$this->get($this->samples() . 'form_with_radio_buttons.html');
		$this->assertTrue($this->setField('tested_field', 2));
		$this->clickSubmitByName('send');
		$this->assertEqual($this->getUrl(), $this->samples() . 'form_with_radio_buttons.html?tested_field=2&send=click+me');
	}

	function testSetFieldAsString() {
		$this->get($this->samples() . 'form_with_radio_buttons.html');
		$this->assertTrue($this->setField('tested_field', '2'));
		$this->clickSubmitByName('send');
		$this->assertEqual($this->getUrl(), $this->samples() . 'form_with_radio_buttons.html?tested_field=2&send=click+me');
	}
}

class TestOfLiveFetching extends SimpleTestAcceptanceTest {
    function setUp() {
        $this->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
    }
 
	function testFormWithArrayBasedInputs() {
		$this->get($this->samples() . 'form_with_array_based_inputs.php');
		$this->setField('value[]', '3', '1');
		$this->setField('value[]', '4', '2');
		$this->clickSubmit('Go');
        $this->assertPattern('/QUERY_STRING : value%5B%5D=3&value%5B%5D=4&submit=Go/');
	}

	function testFormWithQuotedValues() {
		$this->get($this->samples() . 'form_with_quoted_values.php');
		$this->assertField('a', 'default');
		$this->assertFieldById('text_field', 'default');
		$this->clickSubmit('Go');
        $this->assertPattern('/a=default&submit=Go/');
	}

    function testGet() {
        $this->assertTrue($this->get($this->samples() . 'network_confirm.php'));
        $this->assertEqual($this->getUrl(), $this->samples() . 'network_confirm.php');
        $this->assertText('target for the SimpleTest');
        $this->assertPattern('/Request method.*?<dd>GET<\/dd>/');
        $this->assertTitle('Simple test target file');
        $this->assertTitle(new PatternExpectation('/target file/'));
        $this->assertResponse(200);
        $this->assertMime('text/html');
        $this->assertHeader('connection', 'close');
        $this->assertHeader('connection', new PatternExpectation('/los/'));
    }
    
    function testSlowGet() {
        $this->assertTrue($this->get($this->samples() . 'slow_page.php'));
    }
    
    function testTimedOutGet() {
        $this->setConnectionTimeout(1);
        $this->ignoreErrors();
        $this->assertFalse($this->get($this->samples() . 'slow_page.php'));
    }
    
    function testPost() {
        $this->assertTrue($this->post($this->samples() . 'network_confirm.php'));
        $this->assertText('target for the SimpleTest');
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/');
    }
    
    function testGetWithData() {
        $this->get($this->samples() . 'network_confirm.php', array("a" => "aaa"));
        $this->assertPattern('/Request method.*?<dd>GET<\/dd>/');
        $this->assertText('a=[aaa]');
    }
    
    function testPostWithData() {
        $this->post($this->samples() . 'network_confirm.php', array("a" => "aaa"));
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/');
        $this->assertText('a=[aaa]');
    }

    function testPostWithRecursiveData() {
        $this->post($this->samples() . 'network_confirm.php', array("a" => "aaa"));
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/');
        $this->assertText('a=[aaa]');

        $this->post($this->samples() . 'network_confirm.php', array("a[aa]" => "aaa"));
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/');
        $this->assertText('a=[aa=[aaa]]');

        $this->post($this->samples() . 'network_confirm.php', array("a[aa][aaa]" => "aaaa"));
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/');
        $this->assertText('a=[aa=[aaa=[aaaa]]]');

        $this->post($this->samples() . 'network_confirm.php', array("a" => array("aa" => "aaa")));
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/');
        $this->assertText('a=[aa=[aaa]]');

        $this->post($this->samples() . 'network_confirm.php', array("a" => array("aa" => array("aaa" => "aaaa"))));
        $this->assertPattern('/Request method.*?<dd>POST<\/dd>/');
        $this->assertText('a=[aa=[aaa=[aaaa]]]');
    }

    function testRelativeGet() {
        $this->get($this->samples() . 'link_confirm.php');
        $this->assertTrue($this->get('network_confirm.php'));
        $this->assertText('target for the SimpleTest');
    }
    
    function testRelativePost() {
        $this->post($this->samples() . 'link_confirm.php');
        $this->assertTrue($this->post('network_confirm.php'));
        $this->assertText('target for the SimpleTest');
    }
}

class TestOfLinkFollowing extends SimpleTestAcceptanceTest {
    function setUp() {
        $this->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
    }
    
    function testLinkAssertions() {
        $this->get($this->samples() . 'link_confirm.php');
        $this->assertLink('Absolute', $this->samples() . 'network_confirm.php');
        $this->assertLink('Absolute', new PatternExpectation('/confirm/'));
        $this->assertClickable('Absolute');
    }
    
    function testAbsoluteLinkFollowing() {
        $this->get($this->samples() . 'link_confirm.php');
        $this->assertTrue($this->clickLink('Absolute'));
        $this->assertText('target for the SimpleTest');
    }
    
    function testRelativeLinkFollowing() {
        $this->get($this->samples() . 'link_confirm.php');
        $this->assertTrue($this->clickLink('Relative'));
        $this->assertText('target for the SimpleTest');
    }
    
    function testLinkIdFollowing() {
        $this->get($this->samples() . 'link_confirm.php');
        $this->assertLinkById(1);
        $this->assertTrue($this->clickLinkById(1));
        $this->assertText('target for the SimpleTest');
    }
    
    function testAbsoluteUrlBehavesAbsolutely() {
        $this->get($this->samples() . 'link_confirm.php');
        $this->get('http://www.lastcraft.com');
        $this->assertText('No guarantee of quality is given or even intended');
    }
    
    function testRelativeUrlRespectsBaseTag() {
        $this->get($this->samples() . 'base_tag/base_link.html');
        $this->click('Back to test pages');
        $this->assertTitle('Simple test target file');
    }
}

class TestOfLivePageLinkingWithMinimalLinks extends SimpleTestAcceptanceTest {
    function setUp() {
        $this->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
    }
    
    function testClickToExplicitelyNamedSelfReturns() {
        $this->get($this->samples() . 'front_controller_style/a_page.php');
        $this->assertEqual($this->getUrl(), $this->samples() . 'front_controller_style/a_page.php');
        $this->assertTitle('Simple test page with links');
        $this->assertLink('Self');
        $this->clickLink('Self');
        $this->assertTitle('Simple test page with links');
    }
    
    function testClickToMissingPageReturnsToSamePage() {
        $this->get($this->samples() . 'front_controller_style/a_page.php');
        $this->clickLink('No page');
        $this->assertTitle('Simple test page with links');
        $this->assertText('[action=no_page]');
    }
    
    function testClickToBareActionReturnsToSamePage() {
        $this->get($this->samples() . 'front_controller_style/a_page.php');
        $this->clickLink('Bare action');
        $this->assertTitle('Simple test page with links');
        $this->assertText('[action=]');
    }
    
    function testClickToSingleQuestionMarkReturnsToSamePage() {
        $this->get($this->samples() . 'front_controller_style/a_page.php');
        $this->clickLink('Empty query');
        $this->assertTitle('Simple test page with links');
    }
    
    function testClickToEmptyStringReturnsToSamePage() {
        $this->get($this->samples() . 'front_controller_style/a_page.php');
        $this->clickLink('Empty link');
        $this->assertTitle('Simple test page with links');
    }
    
    function testClickToSingleDotGoesToCurrentDirectory() {
        $this->get($this->samples() . 'front_controller_style/a_page.php');
        $this->clickLink('Current directory');
        $this->assertTitle(
                'Simple test front controller',
                '%s -> index.php needs to be set as a default web server home page');
    }
    
    function testClickBackADirectoryLevel() {
        $this->get($this->samples() . 'front_controller_style/');
        $this->clickLink('Down one');
        $this->assertPattern('|Index of .*?/test|i');
    }
}

class TestOfLiveFrontControllerEmulation extends SimpleTestAcceptanceTest {
    function setUp() {
        $this->addHeader('User-Agent: SimpleTest ' . SimpleTest::getVersion());
    }
    
    function testJumpToNamedPage() {
        $this->get($this->samples() . 'front_controller_style/');
        $this->assertText('Simple test front controller');
        $this->clickLink('Index');
        $this->assertResponse(200);

⌨️ 快捷键说明

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