summaryrefslogtreecommitdiff
path: root/tests/UnitTests/simpletest/test
diff options
context:
space:
mode:
Diffstat (limited to 'tests/UnitTests/simpletest/test')
-rw-r--r--tests/UnitTests/simpletest/test/acceptance_test.php1106
-rw-r--r--tests/UnitTests/simpletest/test/adapter_test.php74
-rw-r--r--tests/UnitTests/simpletest/test/all_tests.php31
-rw-r--r--tests/UnitTests/simpletest/test/authentication_test.php141
-rw-r--r--tests/UnitTests/simpletest/test/browser_test.php870
-rw-r--r--tests/UnitTests/simpletest/test/dumper_test.php87
-rw-r--r--tests/UnitTests/simpletest/test/encoding_test.php134
-rw-r--r--tests/UnitTests/simpletest/test/errors_test.php139
-rw-r--r--tests/UnitTests/simpletest/test/expectation_test.php282
-rw-r--r--tests/UnitTests/simpletest/test/form_test.php326
-rw-r--r--tests/UnitTests/simpletest/test/frames_test.php569
-rw-r--r--tests/UnitTests/simpletest/test/http_test.php567
-rw-r--r--tests/UnitTests/simpletest/test/live_test.php47
-rw-r--r--tests/UnitTests/simpletest/test/options_test.php95
-rw-r--r--tests/UnitTests/simpletest/test/page_test.php792
-rw-r--r--tests/UnitTests/simpletest/test/parse_error_test.php10
-rw-r--r--tests/UnitTests/simpletest/test/parser_test.php648
-rw-r--r--tests/UnitTests/simpletest/test/real_sites_test.php29
-rw-r--r--tests/UnitTests/simpletest/test/remote_test.php17
-rw-r--r--tests/UnitTests/simpletest/test/shell_test.php38
-rw-r--r--tests/UnitTests/simpletest/test/shell_tester_test.php37
-rw-r--r--tests/UnitTests/simpletest/test/simple_mock_test.php648
-rw-r--r--tests/UnitTests/simpletest/test/socket_test.php26
-rw-r--r--tests/UnitTests/simpletest/test/tag_test.php462
-rw-r--r--tests/UnitTests/simpletest/test/test_with_parse_error.php8
-rw-r--r--tests/UnitTests/simpletest/test/unit_tester_test.php52
-rw-r--r--tests/UnitTests/simpletest/test/unit_tests.php49
-rw-r--r--tests/UnitTests/simpletest/test/url_test.php369
-rw-r--r--tests/UnitTests/simpletest/test/user_agent_test.php546
-rw-r--r--tests/UnitTests/simpletest/test/visual_test.php386
-rw-r--r--tests/UnitTests/simpletest/test/web_tester_test.php133
-rw-r--r--tests/UnitTests/simpletest/test/xml_test.php189
32 files changed, 8907 insertions, 0 deletions
diff --git a/tests/UnitTests/simpletest/test/acceptance_test.php b/tests/UnitTests/simpletest/test/acceptance_test.php
new file mode 100644
index 00000000..7cb73afb
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/acceptance_test.php
@@ -0,0 +1,1106 @@
+<?php
+ // $Id: acceptance_test.php,v 1.42 2005/02/22 02:17:06 lastcraft Exp $
+ require_once(dirname(__FILE__) . '/../options.php');
+ require_once(dirname(__FILE__) . '/../browser.php');
+ require_once(dirname(__FILE__) . '/../web_tester.php');
+ require_once(dirname(__FILE__) . '/../unit_tester.php');
+
+ class TestOfLiveBrowser extends UnitTestCase {
+
+ function testGet() {
+ $browser = &new SimpleBrowser();
+ $browser->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+
+ $this->assertTrue($browser->get('http://www.lastcraft.com/test/network_confirm.php'));
+ $this->assertWantedPattern('/target for the SimpleTest/', $browser->getContent());
+ $this->assertWantedPattern('/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 ' . SimpleTestOptions::getVersion());
+ $this->assertTrue($browser->post('http://www.lastcraft.com/test/network_confirm.php'));
+ $this->assertWantedPattern('/target for the SimpleTest/', $browser->getContent());
+ $this->assertWantedPattern('/Request method.*?<dd>POST<\/dd>/', $browser->getContent());
+ }
+
+ function testAbsoluteLinkFollowing() {
+ $browser = &new SimpleBrowser();
+ $browser->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ $browser->get('http://www.lastcraft.com/test/link_confirm.php');
+ $this->assertTrue($browser->clickLink('Absolute'));
+ $this->assertWantedPattern('/target for the SimpleTest/', $browser->getContent());
+ }
+
+ function testRelativeLinkFollowing() {
+ $browser = &new SimpleBrowser();
+ $browser->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ $browser->get('http://www.lastcraft.com/test/link_confirm.php');
+ $this->assertTrue($browser->clickLink('Relative'));
+ $this->assertWantedPattern('/target for the SimpleTest/', $browser->getContent());
+ }
+
+ function testIdLinkFollowing() {
+ $browser = &new SimpleBrowser();
+ $browser->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ $browser->get('http://www.lastcraft.com/test/link_confirm.php');
+ $this->assertTrue($browser->clickLinkById(1));
+ $this->assertWantedPattern('/target for the SimpleTest/', $browser->getContent());
+ }
+
+ function testCookieReading() {
+ $browser = &new SimpleBrowser();
+ $browser->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ $browser->get('http://www.lastcraft.com/test/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 ' . SimpleTestOptions::getVersion());
+ $browser->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($browser->clickSubmit('Go!'));
+ $this->assertWantedPattern('/Request method.*?<dd>POST<\/dd>/', $browser->getContent());
+ $this->assertWantedPattern('/go=\[Go!\]/', $browser->getContent());
+ }
+ }
+
+ class TestOfLiveFetching extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testGet() {
+ $this->assertTrue($this->get('http://www.lastcraft.com/test/network_confirm.php'));
+ $this->assertTrue($this->getUrl() == 'http://www.lastcraft.com/test/network_confirm.php');
+ $this->assertWantedPattern('/target for the SimpleTest/');
+ $this->assertWantedPattern('/Request method.*?<dd>GET<\/dd>/');
+ $this->assertTitle('Simple test target file');
+ $this->assertResponse(200);
+ $this->assertMime('text/html');
+ }
+
+ function testSlowGet() {
+ $this->assertTrue($this->get('http://www.lastcraft.com/test/slow_page.php'));
+ }
+
+ function testTimedOutGet() {
+ $this->setConnectionTimeout(1);
+ $this->assertFalse($this->get('http://www.lastcraft.com/test/slow_page.php'));
+ }
+
+ function testPost() {
+ $this->assertTrue($this->post('http://www.lastcraft.com/test/network_confirm.php'));
+ $this->assertWantedText('target for the SimpleTest');
+ $this->assertWantedPattern('/Request method.*?<dd>POST<\/dd>/');
+ }
+
+ function testGetWithData() {
+ $this->get('http://www.lastcraft.com/test/network_confirm.php', array("a" => "aaa"));
+ $this->assertWantedPattern('/Request method.*?<dd>GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ }
+
+ function testPostWithData() {
+ $this->post('http://www.lastcraft.com/test/network_confirm.php', array("a" => "aaa"));
+ $this->assertWantedPattern('/Request method.*?<dd>POST<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ }
+
+ function testRelativeGet() {
+ $this->get('http://www.lastcraft.com/test/link_confirm.php');
+ $this->assertTrue($this->get('network_confirm.php'));
+ $this->assertWantedText('target for the SimpleTest');
+ }
+
+ function testRelativePost() {
+ $this->post('http://www.lastcraft.com/test/link_confirm.php');
+ $this->assertTrue($this->post('network_confirm.php'));
+ $this->assertWantedText('target for the SimpleTest');
+ }
+
+ function testAbsoluteLinkFollowing() {
+ $this->get('http://www.lastcraft.com/test/link_confirm.php');
+ $this->assertLink('Absolute');
+ $this->assertTrue($this->clickLink('Absolute'));
+ $this->assertWantedText('target for the SimpleTest');
+ }
+
+ function testRelativeLinkFollowing() {
+ $this->get('http://www.lastcraft.com/test/link_confirm.php');
+ $this->assertTrue($this->clickLink('Relative'));
+ $this->assertWantedText('target for the SimpleTest');
+ }
+
+ function testLinkIdFollowing() {
+ $this->get('http://www.lastcraft.com/test/link_confirm.php');
+ $this->assertLinkById(1);
+ $this->assertTrue($this->clickLinkById(1));
+ $this->assertWantedText('target for the SimpleTest');
+ }
+
+ function testAbsoluteUrlBehavesAbsolutely() {
+ $this->get('http://www.lastcraft.com/test/link_confirm.php');
+ $this->get('http://www.lastcraft.com');
+ $this->assertWantedText('No guarantee of quality is given or even intended');
+ }
+ }
+
+ class TestOfLivePageLinkingWithMinimalLinks extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testClickToExplicitelyNamedSelfReturns() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/a_page.php');
+ $this->assertTrue($this->getUrl() == 'http://www.lastcraft.com/test/front_controller_style/a_page.php');
+ $this->assertTitle('Simple test page with links');
+ $this->clickLink('Self');
+ $this->assertTitle('Simple test page with links');
+ }
+
+ function testClickToMissingPageReturnsToSamePage() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/a_page.php');
+ $this->clickLink('No page');
+ $this->assertTitle('Simple test page with links');
+ $this->assertWantedText('[action=no_page]');
+ }
+
+ function testClickToBareActionReturnsToSamePage() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/a_page.php');
+ $this->clickLink('Bare action');
+ $this->assertTitle('Simple test page with links');
+ $this->assertWantedText('[action=]');
+ }
+
+ function testClickToSingleQuestionMarkReturnsToSamePage() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/a_page.php');
+ $this->clickLink('Empty query');
+ $this->assertTitle('Simple test page with links');
+ }
+
+ function testClickToEmptyStringReturnsToSamePage() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/a_page.php');
+ $this->clickLink('Empty link');
+ $this->assertTitle('Simple test page with links');
+ }
+
+ function testClickToSingleDotGoesToCurrentDirectory() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/a_page.php');
+ $this->clickLink('Current directory');
+ $this->assertTitle('Simple test front controller');
+ }
+
+ function testClickBackADirectoryLevel() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->clickLink('Down one');
+ $this->assertWantedText('Index of /test');
+ }
+ }
+
+ class TestOfLiveFrontControllerEmulation extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testJumpToNamedPage() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->assertWantedText('Simple test front controller');
+ $this->clickLink('Index');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=index]');
+ }
+
+ function testJumpToUnnamedPage() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->clickLink('No page');
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+ $this->assertWantedText('[action=no_page]');
+ }
+
+ function testJumpToUnnamedPageWithBareParameter() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->clickLink('Bare action');
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+ $this->assertWantedText('[action=]');
+ }
+
+ function testJumpToUnnamedPageWithEmptyQuery() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->clickLink('Empty query');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertWantedPattern('/raw get data.*?\[\].*?get data/si');
+ }
+
+ function testJumpToUnnamedPageWithEmptyLink() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->clickLink('Empty link');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertWantedPattern('/raw get data.*?\[\].*?get data/si');
+ }
+
+ function testJumpBackADirectoryLevel() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->clickLink('Down one');
+ $this->assertWantedText('Index of /test');
+ }
+
+ function testSubmitToNamedPage() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->assertWantedText('Simple test front controller');
+ $this->clickSubmit('Index');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Index]');
+ }
+
+ function testSubmitToSameDirectory() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/index.php');
+ $this->clickSubmit('Same directory');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Same+directory]');
+ }
+
+ function testSubmitToEmptyAction() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/index.php');
+ $this->clickSubmit('Empty action');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Empty+action]');
+ }
+
+ function testSubmitToNoAction() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/index.php');
+ $this->clickSubmit('No action');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=No+action]');
+ }
+
+ function testSubmitBackADirectoryLevel() {
+ $this->get('http://www.lastcraft.com/test/front_controller_style/');
+ $this->clickSubmit('Down one');
+ $this->assertWantedText('Index of /test');
+ }
+ }
+
+ class TestOfLiveHeaders extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testConfirmingHeaderExistence() {
+ $this->get('http://www.lastcraft.com/');
+ $this->assertHeader('content-type');
+ $this->assertHeader('content-type', 'text/html');
+ $this->assertHeaderPattern('content-type', '/HTML/i');
+ $this->assertNoUnwantedHeader('WWW-Authenticate');
+ }
+ }
+
+ class TestOfLiveRedirects extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testNoRedirects() {
+ $this->setMaximumRedirects(0);
+ $this->get('http://www.lastcraft.com/test/redirect.php');
+ $this->assertTitle('Redirection test');
+ }
+
+ function testRedirects() {
+ $this->setMaximumRedirects(1);
+ $this->get('http://www.lastcraft.com/test/redirect.php');
+ $this->assertTitle('Simple test target file');
+ }
+
+ function testRedirectLosesGetData() {
+ $this->get('http://www.lastcraft.com/test/redirect.php', array('a' => 'aaa'));
+ $this->assertNoUnwantedText('a=[aaa]');
+ }
+
+ function testRedirectKeepsExtraRequestDataOfItsOwn() {
+ $this->get('http://www.lastcraft.com/test/redirect.php');
+ $this->assertWantedText('r=[rrr]');
+ }
+
+ function testRedirectLosesPostData() {
+ $this->post('http://www.lastcraft.com/test/redirect.php', array('a' => 'aaa'));
+ $this->assertTitle('Simple test target file');
+ $this->assertNoUnwantedText('a=[aaa]');
+ }
+
+ function testRedirectWithBaseUrlChange() {
+ $this->get('http://www.lastcraft.com/test/base_change_redirect.php');
+ $this->assertTitle('Simple test target file in folder');
+ $this->get('http://www.lastcraft.com/test/path/base_change_redirect.php');
+ $this->assertTitle('Simple test target file');
+ }
+
+ function testRedirectWithDoubleBaseUrlChange() {
+ $this->get('http://www.lastcraft.com/test/double_base_change_redirect.php');
+ $this->assertTitle('Simple test target file');
+ }
+ }
+
+ class TestOfLiveCookies extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testCookieSetting() {
+ $this->setCookie('a', 'Test cookie a', 'www.lastcraft.com');
+ $this->setCookie('b', 'Test cookie b', 'www.lastcraft.com', 'test');
+ $this->get('http://www.lastcraft.com/test/network_confirm.php');
+ $this->assertWantedPattern('/Test cookie a/');
+ $this->assertWantedPattern('/Test cookie b/');
+ $this->assertCookie('a');
+ $this->assertCookie('b', 'Test cookie b');
+ $this->assertTrue($this->getCookie('a') == 'Test cookie a');
+ $this->assertTrue($this->getCookie('b') == 'Test cookie b');
+ }
+
+ function testCookieReading() {
+ $this->get('http://www.lastcraft.com/test/set_cookies.php');
+ $this->assertCookie('session_cookie', 'A');
+ $this->assertCookie('short_cookie', 'B');
+ $this->assertCookie('day_cookie', 'C');
+ }
+
+ function testTemporaryCookieExpiry() {
+ $this->get('http://www.lastcraft.com/test/set_cookies.php');
+ $this->restart();
+ $this->assertNoCookie('session_cookie');
+ $this->assertCookie('day_cookie', 'C');
+ }
+
+ function testTimedCookieExpiry() {
+ $this->get('http://www.lastcraft.com/test/set_cookies.php');
+ $this->ageCookies(3600);
+ $this->restart(time() + 60); // Includes a 60 sec. clock drift margin.
+ $this->assertNoCookie('session_cookie');
+ $this->assertNoCookie('hour_cookie');
+ $this->assertCookie('day_cookie', 'C');
+ }
+
+ function testOfClockOverDrift() {
+ $this->get('http://www.lastcraft.com/test/set_cookies.php');
+ $this->restart(time() + 160); // Allows sixty second drift.
+ $this->assertNoCookie(
+ 'short_cookie',
+ '%s->Please check your computer clock setting if you are not using NTP');
+ }
+
+ function testOfClockUnderDrift() {
+ $this->get('http://www.lastcraft.com/test/set_cookies.php');
+ $this->restart(time() + 40); // Allows sixty second drift.
+ $this->assertCookie(
+ 'short_cookie',
+ 'B',
+ '%s->Please check your computer clock setting if you are not using NTP');
+ }
+
+ function testCookiePath() {
+ $this->get('http://www.lastcraft.com/test/set_cookies.php');
+ $this->assertNoCookie("path_cookie", "D");
+ $this->get('./path/show_cookies.php');
+ $this->assertWantedPattern('/path_cookie/');
+ $this->assertCookie("path_cookie", "D");
+ }
+ }
+
+ class TestOfLiveForms extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testSimpleSubmit() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedPattern('/Request method.*?<dd>POST<\/dd>/');
+ $this->assertWantedText('go=[Go!]');
+ }
+
+ function testDefaultFormValues() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertField('a', '');
+ $this->assertField('b', 'Default text');
+ $this->assertField('c', '');
+ $this->assertField('d', 'd1');
+ $this->assertField('e', false);
+ $this->assertField('f', 'on');
+ $this->assertField('g', 'g3');
+ $this->assertField('h', 2);
+ $this->assertField('go', 'Go!');
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('go=[Go!]');
+ $this->assertWantedText('a=[]');
+ $this->assertWantedText('b=[Default text]');
+ $this->assertWantedText('c=[]');
+ $this->assertWantedText('d=[d1]');
+ $this->assertNoUnwantedText('e=[');
+ $this->assertWantedText('f=[on]');
+ $this->assertWantedText('g=[g3]');
+ }
+
+ function testFormSubmissionByLabel() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->setField('a', 'aaa');
+ $this->setField('b', 'bbb');
+ $this->setField('c', 'ccc');
+ $this->setField('d', 'D2');
+ $this->setField('e', 'on');
+ $this->setField('f', false);
+ $this->setField('g', 'g2');
+ $this->setField('h', 1);
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[aaa]');
+ $this->assertWantedText('b=[bbb]');
+ $this->assertWantedText('c=[ccc]');
+ $this->assertWantedText('d=[d2]');
+ $this->assertWantedText('e=[on]');
+ $this->assertNoUnwantedText('f=[');
+ $this->assertWantedText('g=[g2]');
+ }
+
+ function testAdditionalFormValues() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmit('Go!', array('add' => 'A')));
+ $this->assertWantedText('go=[Go!]');
+ $this->assertWantedText('add=[A]');
+ }
+
+ function testFormSubmissionByName() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmitByName('go'));
+ $this->assertWantedText('go=[Go!]');
+ }
+
+ function testFormSubmissionByNameAndadditionalParameters() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmitByName('go', array('add' => 'A')));
+ $this->assertWantedText('go=[Go!]');
+ $this->assertWantedText('add=[A]');
+ }
+
+ function testFormSubmissionBySubmitButtonLabeledSubmit() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmitByName('test'));
+ $this->assertWantedText('test=[Submit]');
+ }
+
+ function testFormSubmissionWithIds() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertFieldById(1, '');
+ $this->assertFieldById(2, 'Default text');
+ $this->assertFieldById(3, '');
+ $this->assertFieldById(4, 'd1');
+ $this->assertFieldById(5, false);
+ $this->setFieldById(1, 'aaa');
+ $this->setFieldById(2, 'bbb');
+ $this->setFieldById(3, 'ccc');
+ $this->setFieldById(4, 'D2');
+ $this->setFieldById(5, 'on');
+ $this->assertTrue($this->clickSubmitById(99));
+ $this->assertWantedText('a=[aaa]');
+ $this->assertWantedText('b=[bbb]');
+ $this->assertWantedText('c=[ccc]');
+ $this->assertWantedText('d=[d2]');
+ $this->assertWantedText('e=[on]');
+ $this->assertWantedText('go=[Go!]');
+ }
+
+ function testImageSubmissionByLabel() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickImage('Image go!', 10, 12));
+ $this->assertWantedText('go_x=[10]');
+ $this->assertWantedText('go_y=[12]');
+ }
+
+ function testImageSubmissionByLabelWithAdditionalParameters() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickImage('Image go!', 10, 12, array('add' => 'A')));
+ $this->assertWantedText('add=[A]');
+ }
+
+ function testImageSubmissionByName() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickImageByName('go', 10, 12));
+ $this->assertWantedText('go_x=[10]');
+ $this->assertWantedText('go_y=[12]');
+ }
+
+ function testImageSubmissionById() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickImageById(97, 10, 12));
+ $this->assertWantedText('go_x=[10]');
+ $this->assertWantedText('go_y=[12]');
+ }
+
+ function testButtonSubmissionByLabel() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->clickSubmit('Button go!', 10, 12));
+ $this->assertWantedPattern('/go=\[ButtonGo\]/s');
+ }
+
+ function testSelfSubmit() {
+ $this->get('http://www.lastcraft.com/test/self_form.php');
+ $this->assertNoUnwantedPattern('/<p>submitted<\/p>/i');
+ $this->assertNoUnwantedPattern('/<p>wrong form<\/p>/i');
+ $this->assertTitle('Test of form self submission');
+ $this->assertTrue($this->clickSubmit());
+ $this->assertWantedPattern('/<p>submitted<\/p>/i');
+ $this->assertNoUnwantedPattern('/<p>wrong form<\/p>/i');
+ $this->assertTitle('Test of form self submission');
+ }
+
+ function testSettingOfBlankOption() {
+ $this->get('http://www.lastcraft.com/test/form.html');
+ $this->assertTrue($this->setField('d', ''));
+ $this->clickSubmit('Go!');
+ $this->assertWantedText('d=[]');
+ }
+ }
+
+ class TestOfLiveMultiValueWidgets extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testDefaultFormValueSubmission() {
+ $this->get('http://www.lastcraft.com/test/multiple_widget_form.html');
+ $this->assertField('a', array('a2', 'a3'));
+ $this->assertField('b', array('b2', 'b3'));
+ $this->assertField('c[]', array('c2', 'c3'));
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[a2, a3]');
+ $this->assertWantedText('b=[b2, b3]');
+ $this->assertWantedText('c=[c2, c3]');
+ }
+
+ function testSubmittingMultipleValues() {
+ $this->get('http://www.lastcraft.com/test/multiple_widget_form.html');
+ $this->setField('a', array('a1', 'a4'));
+ $this->assertField('a', array('a1', 'a4'));
+ $this->assertField('a', array('a4', 'a1'));
+ $this->setField('b', array('b1', 'b4'));
+ $this->assertField('b', array('b1', 'b4'));
+ $this->setField('c[]', array('c1', 'c4'));
+ $this->assertField('c[]', array('c1', 'c4'));
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[a1, a4]');
+ $this->assertWantedText('b=[b1, b4]');
+ $this->assertWantedText('c=[c1, c4]');
+ }
+
+ function testSavantStyleHiddenFieldDefaults() {
+ $this->get('http://www.lastcraft.com/test/savant_style_form.html');
+ $this->assertField('a', array('a0'));
+ $this->assertField('b', array('b0'));
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[a0]');
+ $this->assertWantedText('b=[b0]');
+ }
+
+ function testSavantStyleHiddenDefaultsAreOverridden() {
+ $this->get('http://www.lastcraft.com/test/savant_style_form.html');
+ $this->assertTrue($this->setField('a', array('a1')));
+ $this->assertTrue($this->setField('b', 'b1'));
+ $this->assertTrue($this->clickSubmit('Go!'));
+ $this->assertWantedText('a=[a1]');
+ $this->assertWantedText('b=[b1]');
+ }
+
+ function testSavantStyleFormSettingById() {
+ $this->get('http://www.lastcraft.com/test/savant_style_form.html');
+ $this->assertFieldById(1, array('a0'));
+ $this->assertFieldById(4, array('b0'));
+ $this->assertTrue($this->setFieldById(2, 'a1'));
+ $this->assertTrue($this->setFieldById(5, 'b1'));
+ $this->assertTrue($this->clickSubmitById(99));
+ $this->assertWantedText('a=[a1]');
+ $this->assertWantedText('b=[b1]');
+ }
+ }
+
+ class TestOfLiveHistoryNavigation extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testRetry() {
+ $this->get('http://www.lastcraft.com/test/cookie_based_counter.php');
+ $this->assertWantedPattern('/count: 1/i');
+ $this->retry();
+ $this->assertWantedPattern('/count: 2/i');
+ $this->retry();
+ $this->assertWantedPattern('/count: 3/i');
+ }
+
+ function testOfBackButton() {
+ $this->get('http://www.lastcraft.com/test/1.html');
+ $this->clickLink('2');
+ $this->assertTitle('2');
+ $this->assertTrue($this->back());
+ $this->assertTitle('1');
+ $this->assertTrue($this->forward());
+ $this->assertTitle('2');
+ $this->assertFalse($this->forward());
+ }
+
+ function testGetRetryResubmitsData() {
+ $this->assertTrue($this->get(
+ 'http://www.lastcraft.com/test/network_confirm.php?a=aaa'));
+ $this->assertWantedPattern('/Request method.*?<dd>GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ $this->retry();
+ $this->assertWantedPattern('/Request method.*?<dd>GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ }
+
+ function testGetRetryResubmitsExtraData() {
+ $this->assertTrue($this->get(
+ 'http://www.lastcraft.com/test/network_confirm.php',
+ array('a' => 'aaa')));
+ $this->assertWantedPattern('/Request method.*?<dd>GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ $this->retry();
+ $this->assertWantedPattern('/Request method.*?<dd>GET<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ }
+
+ function testPostRetryResubmitsData() {
+ $this->assertTrue($this->post(
+ 'http://www.lastcraft.com/test/network_confirm.php',
+ array('a' => 'aaa')));
+ $this->assertWantedPattern('/Request method.*?<dd>POST<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ $this->retry();
+ $this->assertWantedPattern('/Request method.*?<dd>POST<\/dd>/');
+ $this->assertWantedText('a=[aaa]');
+ }
+
+ function testGetRetryResubmitsRepeatedData() {
+ $this->assertTrue($this->get(
+ 'http://www.lastcraft.com/test/network_confirm.php?a=1&a=2'));
+ $this->assertWantedPattern('/Request method.*?<dd>GET<\/dd>/');
+ $this->assertWantedText('a=[1, 2]');
+ $this->retry();
+ $this->assertWantedPattern('/Request method.*?<dd>GET<\/dd>/');
+ $this->assertWantedText('a=[1, 2]');
+ }
+ }
+
+ class TestOfLiveAuthentication extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testChallengeFromProtectedPage() {
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->assertResponse(401);
+ $this->assertAuthentication('Basic');
+ $this->assertRealm('SimpleTest basic authentication');
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->retry();
+ $this->assertResponse(200);
+ }
+
+ function testEncodedAuthenticationFetchesPage() {
+ $this->get('http://test:secret@www.lastcraft.com/test/protected/');
+ $this->assertResponse(200);
+ }
+
+ function testRealmExtendsToWholeDirectory() {
+ $this->get('http://www.lastcraft.com/test/protected/1.html');
+ $this->authenticate('test', 'secret');
+ $this->clickLink('2');
+ $this->assertResponse(200);
+ $this->clickLink('3');
+ $this->assertResponse(200);
+ }
+
+ function testRedirectKeepsAuthentication() {
+ $this->get('http://www.lastcraft.com/test/protected/local_redirect.php');
+ $this->authenticate('test', 'secret');
+ $this->assertTitle('Simple test target file');
+ }
+
+ function testSessionRestartLosesAuthentication() {
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->restart();
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->assertResponse(401);
+ }
+ }
+
+ class TestOfLoadingFrames extends WebTestCase {
+
+ function setUp() {
+ $this->addHeader('User-Agent: SimpleTest ' . SimpleTestOptions::getVersion());
+ }
+
+ function testNoFramesContentWhenFramesDisabled() {
+ $this->ignoreFrames();
+ $this->get('http://www.lastcraft.com/test/one_page_frameset.html');
+ $this->assertTitle('Frameset for testing of SimpleTest');
+ $this->assertWantedText('This content is for no frames only');
+ }
+
+ function testPatternMatchCanReadTheOnlyFrame() {
+ $this->get('http://www.lastcraft.com/test/one_page_frameset.html');
+ $this->assertWantedText('A target for the SimpleTest test suite');
+ $this->assertNoUnwantedText('This content is for no frames only');
+ }
+
+ function testMessyFramesetResponsesByName() {
+ $this->assertTrue($this->get(
+ 'http://www.lastcraft.com/test/messy_frameset.html'));
+ $this->assertTitle('Frameset for testing of SimpleTest');
+
+ $this->assertTrue($this->setFrameFocus('Front controller'));
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+
+ $this->assertTrue($this->setFrameFocus('One'));
+ $this->assertResponse(200);
+ $this->assertLink('2');
+
+ $this->assertTrue($this->setFrameFocus('Frame links'));
+ $this->assertResponse(200);
+ $this->assertLink('Set one to 2');
+
+ $this->assertTrue($this->setFrameFocus('Counter'));
+ $this->assertResponse(200);
+ $this->assertWantedText('Count: 1');
+
+ $this->assertTrue($this->setFrameFocus('Redirected'));
+ $this->assertResponse(200);
+ $this->assertWantedText('r=rrr');
+
+ $this->assertTrue($this->setFrameFocus('Protected'));
+ $this->assertResponse(401);
+
+ $this->assertTrue($this->setFrameFocus('Protected redirect'));
+ $this->assertResponse(401);
+
+ $this->assertTrue($this->setFrameFocusByIndex(1));
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+
+ $this->assertTrue($this->setFrameFocusByIndex(2));
+ $this->assertResponse(200);
+ $this->assertLink('2');
+
+ $this->assertTrue($this->setFrameFocusByIndex(3));
+ $this->assertResponse(200);
+ $this->assertLink('Set one to 2');
+
+ $this->assertTrue($this->setFrameFocusByIndex(4));
+ $this->assertResponse(200);
+ $this->assertWantedTExt('Count: 1');
+
+ $this->assertTrue($this->setFrameFocusByIndex(5));
+ $this->assertResponse(200);
+ $this->assertWantedText('r=rrr');
+
+ $this->assertTrue($this->setFrameFocusByIndex(6));
+ $this->assertResponse(401);
+
+ $this->assertTrue($this->setFrameFocusByIndex(7));
+ }
+
+ function testReloadingFramesetPage() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->assertWantedText('Count: 1');
+ $this->retry();
+ $this->assertWantedText('Count: 2');
+ $this->retry();
+ $this->assertWantedText('Count: 3');
+ }
+
+ function testReloadingSingleFrameWithCookieCounter() {
+ $this->get('http://www.lastcraft.com/test/counting_frameset.html');
+ $this->setFrameFocus('a');
+ $this->assertWantedText('Count: 1');
+ $this->setFrameFocus('b');
+ $this->assertWantedText('Count: 2');
+
+ $this->setFrameFocus('a');
+ $this->retry();
+ $this->assertWantedText('Count: 3');
+ $this->retry();
+ $this->assertWantedText('Count: 4');
+ $this->setFrameFocus('b');
+ $this->assertWantedText('Count: 2');
+ }
+
+ function testReloadingFrameWhenUnfocusedReloadsWholeFrameset() {
+ $this->get('http://www.lastcraft.com/test/counting_frameset.html');
+ $this->setFrameFocus('a');
+ $this->assertWantedText('Count: 1');
+ $this->setFrameFocus('b');
+ $this->assertWantedText('Count: 2');
+
+ $this->clearFrameFocus('a');
+ $this->retry();
+
+ $this->assertTitle('Frameset for testing of SimpleTest');
+ $this->setFrameFocus('a');
+ $this->assertWantedText('Count: 3');
+ $this->setFrameFocus('b');
+ $this->assertWantedText('Count: 4');
+ }
+
+ function testClickingNormalLinkReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('2');
+ $this->assertLink('3');
+ $this->assertWantedText('Simple test front controller');
+ }
+
+ function testJumpToNamedPageReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->clickLink('Index');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=index]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testJumpToUnnamedPageReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('No page');
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+ $this->assertWantedText('[action=no_page]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testJumpToUnnamedPageWithBareParameterReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Bare action');
+ $this->assertResponse(200);
+ $this->assertWantedText('Simple test front controller');
+ $this->assertWantedText('[action=]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testJumpToUnnamedPageWithEmptyQueryReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Empty query');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertWantedPattern('/raw get data.*?\[\].*?get data/si');
+ $this->assertWantedPattern('/Count: 1/');
+ }
+
+ function testJumpToUnnamedPageWithEmptyLinkReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Empty link');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertWantedPattern('/raw get data.*?\[\].*?get data/si');
+ $this->assertWantedPattern('/Count: 1/');
+ }
+
+ function testJumpBackADirectoryLevelReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Down one');
+ $this->assertWantedPattern('/index of \/test/i');
+ $this->assertWantedPattern('/Count: 1/');
+ }
+
+ function testSubmitToNamedPageReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->clickSubmit('Index');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Index]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testSubmitToSameDirectoryReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickSubmit('Same directory');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Same+directory]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testSubmitToEmptyActionReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickSubmit('Empty action');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=Empty+action]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testSubmitToNoActionReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickSubmit('No action');
+ $this->assertResponse(200);
+ $this->assertWantedText('[action=No+action]');
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testSubmitBackADirectoryLevelReplacesJustThatFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickSubmit('Down one');
+ $this->assertWantedPattern('/index of \/test/i');
+ $this->assertWantedPattern('/Count: 1/');
+ }
+
+ function testTopLinkExitsFrameset() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->clickLink('Exit the frameset');
+ $this->assertTitle('Simple test target file');
+ }
+
+ function testLinkInOnePageCanLoadAnother() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->assertNoLink('3');
+ $this->clickLink('Set one to 2');
+ $this->assertLink('3');
+ $this->assertNoLink('2');
+ $this->assertTitle('Frameset for testing of SimpleTest');
+ }
+ }
+
+ class TestOfFrameAuthentication extends WebTestCase {
+
+ function testUnauthenticatedFrameSendsChallenge() {
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->setFrameFocus('Protected');
+ $this->assertAuthentication('Basic');
+ $this->assertRealm('SimpleTest basic authentication');
+ $this->assertResponse(401);
+ }
+
+ function testCanReadFrameFromAlreadyAuthenticatedRealm() {
+ $this->get('http://www.lastcraft.com/test/protected/');
+ $this->authenticate('test', 'secret');
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->setFrameFocus('Protected');
+ $this->assertResponse(200);
+ $this->assertWantedText('A target for the SimpleTest test suite');
+ }
+
+ function testCanAuthenticateFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->setFrameFocus('Protected');
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->assertWantedText('A target for the SimpleTest test suite');
+ $this->clearFrameFocus();
+ $this->assertWantedText('Count: 1');
+ }
+
+ function testCanAuthenticateRedirectedFrame() {
+ $this->get('http://www.lastcraft.com/test/messy_frameset.html');
+ $this->setFrameFocus('Protected redirect');
+ $this->assertResponse(401);
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->assertWantedText('A target for the SimpleTest test suite');
+ $this->clearFrameFocus();
+ $this->assertWantedText('Count: 1');
+ }
+ }
+
+ class TestOfNestedFrames extends WebTestCase {
+
+ function testCanNavigateToSpecificContent() {
+ $this->get('http://www.lastcraft.com/test/nested_frameset.html');
+ $this->assertTitle('Nested frameset for testing of SimpleTest');
+
+ $this->assertWantedPattern('/This is frame A/');
+ $this->assertWantedPattern('/This is frame B/');
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertLink('2');
+ $this->assertLink('Set one to 2');
+ $this->assertWantedPattern('/Count: 1/');
+ $this->assertWantedPattern('/r=rrr/');
+
+ $this->setFrameFocus('pair');
+ $this->assertWantedPattern('/This is frame A/');
+ $this->assertWantedPattern('/This is frame B/');
+ $this->assertNoUnwantedPattern('/Simple test front controller/');
+ $this->assertNoLink('2');
+
+ $this->setFrameFocus('aaa');
+ $this->assertWantedPattern('/This is frame A/');
+ $this->assertNoUnwantedPattern('/This is frame B/');
+
+ $this->clearFrameFocus();
+ $this->assertResponse(200);
+ $this->setFrameFocus('messy');
+ $this->assertResponse(200);
+ $this->setFrameFocus('Front controller');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/Simple test front controller/');
+ $this->assertNoLink('2');
+ }
+
+ function testReloadingFramesetPage() {
+ $this->get('http://www.lastcraft.com/test/nested_frameset.html');
+ $this->assertWantedPattern('/Count: 1/');
+ $this->retry();
+ $this->assertWantedPattern('/Count: 2/');
+ $this->retry();
+ $this->assertWantedPattern('/Count: 3/');
+ }
+
+ function testRetryingNestedPageOnlyRetriesThatSet() {
+ $this->get('http://www.lastcraft.com/test/nested_frameset.html');
+ $this->assertWantedPattern('/Count: 1/');
+ $this->setFrameFocus('messy');
+ $this->retry();
+ $this->assertWantedPattern('/Count: 2/');
+ $this->setFrameFocus('Counter');
+ $this->retry();
+ $this->assertWantedPattern('/Count: 3/');
+
+ $this->clearFrameFocus();
+ $this->setFrameFocus('messy');
+ $this->setFrameFocus('Front controller');
+ $this->retry();
+
+ $this->clearFrameFocus();
+ $this->assertWantedPattern('/Count: 3/');
+ }
+
+ function testAuthenticatingNestedPage() {
+ $this->get('http://www.lastcraft.com/test/nested_frameset.html');
+ $this->setFrameFocus('messy');
+ $this->setFrameFocus('Protected');
+ $this->assertAuthentication('Basic');
+ $this->assertRealm('SimpleTest basic authentication');
+ $this->assertResponse(401);
+
+ $this->authenticate('test', 'secret');
+ $this->assertResponse(200);
+ $this->assertWantedPattern('/A target for the SimpleTest test suite/');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/adapter_test.php b/tests/UnitTests/simpletest/test/adapter_test.php
new file mode 100644
index 00000000..d7f67caf
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/adapter_test.php
@@ -0,0 +1,74 @@
+<?php
+ // $Id: adapter_test.php,v 1.7 2005/01/13 01:31:57 lastcraft Exp $
+
+ class SameTestClass {
+ }
+
+ class TestOfPearAdapter extends PHPUnit_TestCase {
+
+ function testBoolean() {
+ $this->assertTrue(true, "PEAR true");
+ $this->assertFalse(false, "PEAR false");
+ }
+
+ function testName() {
+ $this->assertTrue($this->getName() == get_class($this));
+ }
+
+ function testPass() {
+ $this->pass("PEAR pass");
+ }
+
+ function testNulls() {
+ $value = null;
+ $this->assertNull($value, "PEAR null");
+ $value = 0;
+ $this->assertNotNull($value, "PEAR not null");
+ }
+
+ function testType() {
+ $this->assertType("Hello", "string", "PEAR type");
+ }
+
+ function testEquals() {
+ $this->assertEquals(12, 12, "PEAR identity");
+ $this->setLooselyTyped(true);
+ $this->assertEquals("12", 12, "PEAR equality");
+ }
+
+ function testSame() {
+ $same = &new SameTestClass();
+ $this->assertSame($same, $same, "PEAR same");
+ }
+
+ function testRegExp() {
+ $this->assertRegExp('/hello/', "A big hello from me", "PEAR regex");
+ }
+ }
+
+ class TestOfPhpUnitAdapter extends TestCase {
+ function TestOfPhpUnitAdapter() {
+ $this->TestCase("TestOfPhpUnitAdapter");
+ }
+
+ function testBoolean() {
+ $this->assert(true, "PHP Unit true");
+ }
+
+ function testName() {
+ $this->assertTrue($this->name() == "TestOfPhpUnitAdapter");
+ }
+
+ function testEquals() {
+ $this->assertEquals(12, 12, "PHP Unit equality");
+ }
+
+ function testMultilineEquals() {
+ $this->assertEquals("a\nb\n", "a\nb\n", "PHP Unit equality");
+ }
+
+ function testRegExp() {
+ $this->assertRegexp('/hello/', "A big hello from me", "PEAR regex");
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/all_tests.php b/tests/UnitTests/simpletest/test/all_tests.php
new file mode 100644
index 00000000..d5e8a9de
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/all_tests.php
@@ -0,0 +1,31 @@
+<?php
+ // $Id: all_tests.php,v 1.20 2005/02/05 04:51:31 lastcraft Exp $
+ define('TEST', __FILE__);
+ require_once('../unit_tester.php');
+ require_once('../shell_tester.php');
+ require_once('../reporter.php');
+ require_once('../mock_objects.php');
+ require_once('unit_tests.php');
+
+ // Uncomment and modify the following line if you are accessing
+ // the net via a proxy server.
+ //
+ // SimpleTestOptions::useProxy('http://my-proxy', 'optional username', 'optional password');
+
+ class AllTests extends GroupTest {
+ function AllTests() {
+ $this->GroupTest('All tests for SimpleTest ' . SimpleTestOptions::getVersion());
+ $this->addTestCase(new UnitTests());
+ $this->addTestFile('shell_test.php');
+ $this->addTestFile('live_test.php');
+ $this->addTestFile('acceptance_test.php');
+ $this->addTestFile('real_sites_test.php');
+ }
+ }
+
+ $test = new AllTests();
+ if (SimpleReporter::inCli()) {
+ exit ($test->run(new TextReporter()) ? 0 : 1);
+ }
+ $test->run(new HtmlReporter());
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/authentication_test.php b/tests/UnitTests/simpletest/test/authentication_test.php
new file mode 100644
index 00000000..3a12a2ff
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/authentication_test.php
@@ -0,0 +1,141 @@
+<?php
+ // $Id: authentication_test.php,v 1.8 2004/09/24 22:55:10 lastcraft Exp $
+ require_once(dirname(__FILE__) . '/../authentication.php');
+ require_once(dirname(__FILE__) . '/../http.php');
+ Mock::generate('SimpleHttpRequest');
+
+ class TestOfRealm extends UnitTestCase {
+
+ function testWithinSameUrl() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/hello.html'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/hello.html')));
+ }
+
+ function testInsideWithLongerUrl() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/hello.html')));
+ }
+
+ function testBelowRootIsOutside() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/more/hello.html')));
+ }
+
+ function testOldNetscapeDefinitionIsOutside() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/'));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/pathmore/hello.html')));
+ }
+
+ function testDifferentPageNameStillInside() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/hello.html'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/goodbye.html')));
+ }
+
+ function testNewUrlInSameDirectoryDoesNotChangeRealm() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/hello.html'));
+ $realm->stretch(new SimpleUrl('http://www.here.com/path/goodbye.html'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/index.html')));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/index.html')));
+ }
+
+ function testNewUrlMakesRealmTheCommonPath() {
+ $realm = &new SimpleRealm(
+ 'Basic',
+ new SimpleUrl('http://www.here.com/path/here/hello.html'));
+ $realm->stretch(new SimpleUrl('http://www.here.com/path/there/goodbye.html'));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/here/index.html')));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/there/index.html')));
+ $this->assertTrue($realm->isWithin(
+ new SimpleUrl('http://www.here.com/path/index.html')));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/index.html')));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/paths/index.html')));
+ $this->assertFalse($realm->isWithin(
+ new SimpleUrl('http://www.here.com/pathindex.html')));
+ }
+ }
+
+ class TestOfAuthenticator extends UnitTestCase {
+
+ function testNoRealms() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectNever('addHeaderLine');
+ $authenticator = &new SimpleAuthenticator();
+ $authenticator->addHeaders($request, new SimpleUrl('http://here.com/'));
+ $request->tally();
+ }
+
+ function &createSingleRealm() {
+ $authenticator = &new SimpleAuthenticator();
+ $authenticator->addRealm(
+ new SimpleUrl('http://www.here.com/path/hello.html'),
+ 'Basic',
+ 'Sanctuary');
+ $authenticator->setIdentityForRealm('www.here.com', 'Sanctuary', 'test', 'secret');
+ return $authenticator;
+ }
+
+ function testOutsideRealm() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectNever('addHeaderLine');
+ $authenticator = &$this->createSingleRealm();
+ $authenticator->addHeaders(
+ $request,
+ new SimpleUrl('http://www.here.com/hello.html'));
+ $request->tally();
+ }
+
+ function testWithinRealm() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectOnce('addHeaderLine');
+ $authenticator = &$this->createSingleRealm();
+ $authenticator->addHeaders(
+ $request,
+ new SimpleUrl('http://www.here.com/path/more/hello.html'));
+ $request->tally();
+ }
+
+ function testRestartingClearsRealm() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectNever('addHeaderLine');
+ $authenticator = &$this->createSingleRealm();
+ $authenticator->restartSession();
+ $authenticator->addHeaders(
+ $request,
+ new SimpleUrl('http://www.here.com/hello.html'));
+ $request->tally();
+ }
+
+ function testDifferentHostIsOutsideRealm() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->expectNever('addHeaderLine');
+ $authenticator = &$this->createSingleRealm();
+ $authenticator->addHeaders(
+ $request,
+ new SimpleUrl('http://here.com/path/hello.html'));
+ $request->tally();
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/browser_test.php b/tests/UnitTests/simpletest/test/browser_test.php
new file mode 100644
index 00000000..b7d23fc4
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/browser_test.php
@@ -0,0 +1,870 @@
+<?php
+ // $Id: browser_test.php,v 1.100 2005/02/22 02:53:49 lastcraft Exp $
+
+ 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->getMethod(), false);
+ $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(
+ 'GET',
+ new SimpleUrl('http://www.here.com/'),
+ new SimpleFormEncoding());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.here.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testSecondEntryAccessors() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'POST',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding(array('a' => 1)));
+ $this->assertIdentical($history->getMethod(), 'POST');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.second.com/'));
+ $this->assertIdentical(
+ $history->getParameters(),
+ new SimpleFormEncoding(array('a' => 1)));
+ }
+
+ function testGoingBackwards() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'POST',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding(array('a' => 1)));
+ $this->assertTrue($history->back());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testGoingBackwardsOffBeginning() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $this->assertFalse($history->back());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testGoingForwardsOffEnd() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $this->assertFalse($history->forward());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.first.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testGoingBackwardsAndForwards() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'POST',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding(array('a' => 1)));
+ $this->assertTrue($history->back());
+ $this->assertTrue($history->forward());
+ $this->assertIdentical($history->getMethod(), 'POST');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.second.com/'));
+ $this->assertIdentical(
+ $history->getParameters(),
+ new SimpleFormEncoding(array('a' => 1)));
+ }
+
+ function testNewEntryReplacesNextOne() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'POST',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding(array('a' => 1)));
+ $history->back();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.third.com/'),
+ new SimpleFormEncoding());
+ $this->assertIdentical($history->getMethod(), 'GET');
+ $this->assertIdentical($history->getUrl(), new SimpleUrl('http://www.third.com/'));
+ $this->assertIdentical($history->getParameters(), new SimpleFormEncoding());
+ }
+
+ function testNewEntryDropsFutureEntries() {
+ $history = &new SimpleBrowserHistory();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.first.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.second.com/'),
+ new SimpleFormEncoding());
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.third.com/'),
+ new SimpleFormEncoding());
+ $history->back();
+ $history->back();
+ $history->recordEntry(
+ 'GET',
+ new SimpleUrl('http://www.fourth.com/'),
+ new SimpleFormEncoding());
+ $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($this);
+ $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($this);
+ $page->setReturnValue('getUrlsByLabel', array('http://www.nowhere.com'));
+ $page->expectOnce('getUrlsByLabel', array('a link label'));
+
+ $browser = &$this->loadPage($page);
+ $this->assertTrue($browser->isLink('a link label'));
+
+ $page->tally();
+ }
+
+ function testLinkAffirmationByIdWhenPresent() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlById', true, array(99));
+ $page->setReturnValue('getUrlById', false, array('*'));
+
+ $browser = &$this->loadPage($page);
+ $this->assertTrue($browser->isLinkById(99));
+ $this->assertFalse($browser->isLinkById(98));
+
+ $page->tally();
+ }
+
+ function testFormHandling() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getField', 'Value');
+ $page->expectOnce('getField', array('key'));
+ $page->expectOnce('setField', array('key', 'Value'));
+ $page->setReturnValue('getFieldById', 'Id value');
+ $page->expectOnce('getFieldById', array(99));
+ $page->expectOnce('setFieldById', array(99, 'Id value'));
+
+ $browser = &$this->loadPage($page);
+ $this->assertEqual($browser->getField('key'), 'Value');
+ $this->assertEqual($browser->getFieldById(99), 'Id value');
+ $browser->setField('key', 'Value');
+ $browser->setFieldById(99, 'Id value');
+
+ $page->tally();
+ }
+ }
+
+ class TestOfBrowserNavigation extends UnitTestCase {
+
+ function &createBrowser(&$agent, &$page) {
+ $browser = &new MockParseSimpleBrowser($this);
+ $browser->setReturnReference('_createUserAgent', $agent);
+ $browser->setReturnReference('_parse', $page);
+ $browser->SimpleBrowser();
+ return $browser;
+ }
+
+ function testClickLinkRequestsPage() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(
+ 0,
+ 'fetchResponse',
+ array('GET', new SimpleUrl('http://this.com/page.html'), false));
+ $agent->expectArgumentsAt(
+ 1,
+ 'fetchResponse',
+ array('GET', new SimpleUrl('http://this.com/new.html'), false));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $page = &new MockSimplePage($this);
+ $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'));
+
+ $agent->tally();
+ $page->tally();
+ }
+
+ function testClickLinkWithUnknownFrameStillRequestsWholePage() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(
+ 0,
+ 'fetchResponse',
+ array('GET', new SimpleUrl('http://this.com/page.html'), false));
+ $target = new SimpleUrl('http://this.com/new.html');
+ $target->setTarget('missing');
+ $agent->expectArgumentsAt(
+ 1,
+ 'fetchResponse',
+ array('GET', $target, false));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $parsed_url = new SimpleUrl('http://this.com/new.html');
+ $parsed_url->setTarget('missing');
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlsByLabel', array($parsed_url));
+ $page->setReturnValue('hasFrames', false);
+ $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'));
+
+ $agent->tally();
+ $page->tally();
+ }
+
+ function testClickingMissingLinkFails() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlsByLabel', array());
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $this->assertTrue($browser->get('http://this.com/page.html'));
+ $this->assertFalse($browser->clickLink('New'));
+ }
+
+ function testClickIndexedLink() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(
+ 1,
+ 'fetchResponse',
+ array('GET', new SimpleUrl('1.html'), false));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue(
+ 'getUrlsByLabel',
+ array(new SimpleUrl('0.html'), new SimpleUrl('1.html')));
+ $page->setReturnValue('getRaw', 'A page');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickLink('New', 1));
+
+ $agent->tally();
+ }
+
+ function testClinkLinkById() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(1, 'fetchResponse', array(
+ 'GET',
+ new SimpleUrl('http://this.com/link.html'),
+ false));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlById', new SimpleUrl('http://this.com/link.html'));
+ $page->expectOnce('getUrlById', array(2));
+ $page->setReturnValue('getRaw', 'A page');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickLinkById(2));
+
+ $agent->tally();
+ $page->tally();
+ }
+
+ function testClickingMissingLinkIdFails() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getUrlById', false);
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertFalse($browser->clickLink(0));
+ }
+
+ function testSubmitFormByLabel() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(1, 'fetchResponse', array(
+ 'POST',
+ new SimpleUrl('http://this.com/handler.html'),
+ new SimpleFormEncoding(array('a' => 'A'))));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitButtonByLabel', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitButtonByLabel', array('Go', false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormBySubmitLabel', $form);
+ $page->expectOnce('getFormBySubmitLabel', array('Go'));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickSubmit('Go'));
+
+ $agent->tally();
+ $page->tally();
+ $form->tally();
+ }
+
+ function testDefaultSubmitFormByLabel() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(1, 'fetchResponse', array(
+ 'GET',
+ new SimpleUrl('http://this.com/page.html'),
+ new SimpleFormEncoding(array('a' => 'A'))));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/page.html'));
+ $form->setReturnValue('getMethod', 'get');
+ $form->setReturnValue('submitButtonByLabel', new SimpleFormEncoding(array('a' => 'A')));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormBySubmitLabel', $form);
+ $page->expectOnce('getFormBySubmitLabel', array('Submit'));
+ $page->setReturnValue('getRaw', 'stuff');
+ $page->setReturnValue('getUrl', new SimpleUrl('http://this.com/page.html'));
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickSubmit());
+
+ $agent->tally();
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByName() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitButtonByName', new SimpleFormEncoding(array('a' => 'A')));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormBySubmitName', $form);
+ $page->expectOnce('getFormBySubmitName', array('me'));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickSubmitByName('me'));
+
+ $page->tally();
+ }
+
+ function testSubmitFormById() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitButtonById', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitButtonById', array(99, false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormBySubmitId', $form);
+ $page->expectOnce('getFormBySubmitId', array(99));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickSubmitById(99));
+
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByImageLabel() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitImageByLabel', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitImageByLabel', array('Go!', 10, 11, false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormByImageLabel', $form);
+ $page->expectOnce('getFormByImageLabel', array('Go!'));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickImage('Go!', 10, 11));
+
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByImageName() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitImageByName', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitImageByName', array('a', 10, 11, false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormByImageName', $form);
+ $page->expectOnce('getFormByImageName', array('a'));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickImageByName('a', 10, 11));
+
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByImageId() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submitImageById', new SimpleFormEncoding(array('a' => 'A')));
+ $form->expectOnce('submitImageById', array(99, 10, 11, false));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormByImageId', $form);
+ $page->expectOnce('getFormByImageId', array(99));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->clickImageById(99, 10, 11));
+
+ $page->tally();
+ $form->tally();
+ }
+
+ function testSubmitFormByFormId() {
+ $agent = &new MockSimpleUserAgent($this);
+ $agent->setReturnReference('fetchResponse', new MockSimpleHttpResponse($this));
+ $agent->expectArgumentsAt(1, 'fetchResponse', array(
+ 'POST',
+ new SimpleUrl('http://this.com/handler.html'),
+ new SimpleFormEncoding(array('a' => 'A'))));
+ $agent->expectCallCount('fetchResponse', 2);
+
+ $form = &new MockSimpleForm($this);
+ $form->setReturnValue('getAction', new SimpleUrl('http://this.com/handler.html'));
+ $form->setReturnValue('getMethod', 'post');
+ $form->setReturnValue('submit', new SimpleFormEncoding(array('a' => 'A')));
+
+ $page = &new MockSimplePage($this);
+ $page->setReturnReference('getFormById', $form);
+ $page->expectOnce('getFormById', array(33));
+ $page->setReturnValue('getRaw', 'stuff');
+
+ $browser = &$this->createBrowser($agent, $page);
+ $browser->get('http://this.com/page.html');
+ $this->assertTrue($browser->submitFormById(33));
+
+ $agent->tally();
+ $page->tally();
+ }
+ }
+
+ class TestOfBrowserFrames extends UnitTestCase {
+
+ function &createBrowser(&$agent) {
+ $browser = &new MockUserAgentSimpleBrowser($this);
+ $browser->setReturnReference('_createUserAgent', $agent);
+ $browser->SimpleBrowser();
+ return $browser;
+ }
+
+ function &createUserAgent($pages) {
+ $agent = &new MockSimpleUserAgent($this);
+ foreach ($pages as $url => $raw) {
+ $url = new SimpleUrl($url);
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', $url);
+ $response->setReturnValue('getContent', $raw);
+ $agent->setReturnReference('fetchResponse', $response, array('*', $url, '*'));
+ }
+ return $agent;
+ }
+
+ function testSimplePageHasNoFrames() {
+ $browser = &$this->createBrowser($this->createUserAgent(
+ array('http://site.with.no.frames/' => 'A non-framed page')));
+ $this->assertEqual(
+ $browser->get('http://site.with.no.frames/'),
+ 'A non-framed page');
+ $this->assertIdentical($browser->getFrames(), 'http://site.with.no.frames/');
+ }
+
+ function testFramesetWithNoFrames() {
+ $browser = &$this->createBrowser($this->createUserAgent(
+ array('http://site.with.no.frames/' => '<frameset></frameset>')));
+ $this->assertEqual(
+ $browser->get('http://site.with.no.frames/'),
+ '');
+ $this->assertIdentical($browser->getFrames(), array());
+ }
+
+ function testFramesetWithSingleFrame() {
+ $frameset = '<frameset><frame name="a" src="frame.html"></frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.one.frame/' => $frameset,
+ 'http://site.with.one.frame/frame.html' => 'A frame')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.one.frame/'),
+ 'A frame');
+ $this->assertIdentical(
+ $browser->getFrames(),
+ array('a' => 'http://site.with.one.frame/frame.html'));
+ }
+
+ function testTitleTakenFromFramesetPage() {
+ $frameset = '<title>Frameset title</title>' .
+ '<frameset><frame name="a" src="frame.html"></frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.one.frame/' => $frameset,
+ 'http://site.with.one.frame/frame.html' => '<title>Page title</title>')));
+
+ $browser->get('http://site.with.one.frame/');
+ $this->assertEqual($browser->getTitle(), 'Frameset title');
+ }
+
+ function testFramesetWithSingleUnnamedFrame() {
+ $frameset = '<frameset><frame src="frame.html"></frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.one.frame/' => $frameset,
+ 'http://site.with.one.frame/frame.html' => 'One frame')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.one.frame/'),
+ 'One frame');
+ $this->assertIdentical(
+ $browser->getFrames(),
+ array(1 => 'http://site.with.one.frame/frame.html'));
+ }
+
+ function testFramesetWithMultipleFrames() {
+ $frameset = '<frameset>' .
+ '<frame name="a" src="frame_a.html">' .
+ '<frame name="b" src="frame_b.html">' .
+ '<frame name="c" src="frame_c.html">' .
+ '</frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.frames/' => $frameset,
+ 'http://site.with.frames/frame_a.html' => 'A frame',
+ 'http://site.with.frames/frame_b.html' => 'B frame',
+ 'http://site.with.frames/frame_c.html' => 'C frame')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.frames/'),
+ 'A frameB frameC frame');
+ $this->assertIdentical($browser->getFrames(), array(
+ 'a' => 'http://site.with.frames/frame_a.html',
+ 'b' => 'http://site.with.frames/frame_b.html',
+ 'c' => 'http://site.with.frames/frame_c.html'));
+ }
+
+ function testFrameFocusByName() {
+ $frameset = '<frameset>' .
+ '<frame name="a" src="frame_a.html">' .
+ '<frame name="b" src="frame_b.html">' .
+ '<frame name="c" src="frame_c.html">' .
+ '</frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.frames/' => $frameset,
+ 'http://site.with.frames/frame_a.html' => 'A frame',
+ 'http://site.with.frames/frame_b.html' => 'B frame',
+ 'http://site.with.frames/frame_c.html' => 'C frame')));
+
+ $browser->get('http://site.with.frames/');
+ $browser->setFrameFocus('a');
+ $this->assertEqual($browser->getContent(), 'A frame');
+ $browser->setFrameFocus('b');
+ $this->assertEqual($browser->getContent(), 'B frame');
+ $browser->setFrameFocus('c');
+ $this->assertEqual($browser->getContent(), 'C frame');
+ }
+
+ function testFramesetWithSomeNamedFrames() {
+ $frameset = '<frameset>' .
+ '<frame name="a" src="frame_a.html">' .
+ '<frame src="frame_b.html">' .
+ '<frame name="c" src="frame_c.html">' .
+ '<frame src="frame_d.html">' .
+ '</frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.frames/' => $frameset,
+ 'http://site.with.frames/frame_a.html' => 'A frame',
+ 'http://site.with.frames/frame_b.html' => 'B frame',
+ 'http://site.with.frames/frame_c.html' => 'C frame',
+ 'http://site.with.frames/frame_d.html' => 'D frame')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.frames/'),
+ 'A frameB frameC frameD frame');
+ $this->assertIdentical($browser->getFrames(), array(
+ 'a' => 'http://site.with.frames/frame_a.html',
+ 2 => 'http://site.with.frames/frame_b.html',
+ 'c' => 'http://site.with.frames/frame_c.html',
+ 4 => 'http://site.with.frames/frame_d.html'));
+ }
+
+ function testFrameFocusWithMixedNamesAndIndexes() {
+ $frameset = '<frameset>' .
+ '<frame name="a" src="frame_a.html">' .
+ '<frame src="frame_b.html">' .
+ '<frame name="c" src="frame_c.html">' .
+ '<frame src="frame_d.html">' .
+ '</frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.frames/' => $frameset,
+ 'http://site.with.frames/frame_a.html' => 'A frame',
+ 'http://site.with.frames/frame_b.html' => 'B frame',
+ 'http://site.with.frames/frame_c.html' => 'C frame',
+ 'http://site.with.frames/frame_d.html' => 'D frame')));
+
+ $browser->get('http://site.with.frames/');
+ $browser->setFrameFocus('a');
+ $this->assertEqual($browser->getContent(), 'A frame');
+ $browser->setFrameFocus(2);
+ $this->assertEqual($browser->getContent(), 'B frame');
+ $browser->setFrameFocus('c');
+ $this->assertEqual($browser->getContent(), 'C frame');
+ $browser->setFrameFocus(4);
+ $this->assertEqual($browser->getContent(), 'D frame');
+ $browser->clearFrameFocus();
+ $this->assertEqual($browser->getContent(), 'A frameB frameC frameD frame');
+ }
+
+ function testNestedFrameset() {
+ $inner = '<frameset>' .
+ '<frame name="page" src="page.html">' .
+ '</frameset>';
+ $outer = '<frameset>' .
+ '<frame name="inner" src="inner.html">' .
+ '</frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.nested.frame/' => $outer,
+ 'http://site.with.nested.frame/inner.html' => $inner,
+ 'http://site.with.nested.frame/page.html' => 'The page')));
+
+ $this->assertEqual(
+ $browser->get('http://site.with.nested.frame/'),
+ 'The page');
+ $this->assertIdentical($browser->getFrames(), array(
+ 'inner' => array(
+ 'page' => 'http://site.with.nested.frame/page.html')));
+ }
+
+ function testCanNavigateToNestedFrame() {
+ $inner = '<frameset>' .
+ '<frame name="one" src="one.html">' .
+ '<frame name="two" src="two.html">' .
+ '</frameset>';
+ $outer = '<frameset>' .
+ '<frame name="inner" src="inner.html">' .
+ '<frame name="three" src="three.html">' .
+ '</frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.nested.frames/' => $outer,
+ 'http://site.with.nested.frames/inner.html' => $inner,
+ 'http://site.with.nested.frames/one.html' => 'Page one',
+ 'http://site.with.nested.frames/two.html' => 'Page two',
+ 'http://site.with.nested.frames/three.html' => 'Page three')));
+
+ $browser->get('http://site.with.nested.frames/');
+ $this->assertEqual($browser->getContent(), 'Page onePage twoPage three');
+
+ $this->assertTrue($browser->setFrameFocus('inner'));
+ $this->assertEqual($browser->getFrameFocus(), array('inner'));
+ $this->assertTrue($browser->setFrameFocus('one'));
+ $this->assertEqual($browser->getFrameFocus(), array('inner', 'one'));
+ $this->assertEqual($browser->getContent(), 'Page one');
+
+ $this->assertTrue($browser->setFrameFocus('two'));
+ $this->assertEqual($browser->getFrameFocus(), array('inner', 'two'));
+ $this->assertEqual($browser->getContent(), 'Page two');
+
+ $browser->clearFrameFocus();
+ $this->assertTrue($browser->setFrameFocus('three'));
+ $this->assertEqual($browser->getFrameFocus(), array('three'));
+ $this->assertEqual($browser->getContent(), 'Page three');
+
+ $this->assertTrue($browser->setFrameFocus('inner'));
+ $this->assertEqual($browser->getContent(), 'Page onePage two');
+ }
+
+ function testCanNavigateToNestedFrameByIndex() {
+ $inner = '<frameset>' .
+ '<frame src="one.html">' .
+ '<frame src="two.html">' .
+ '</frameset>';
+ $outer = '<frameset>' .
+ '<frame src="inner.html">' .
+ '<frame src="three.html">' .
+ '</frameset>';
+ $browser = &$this->createBrowser($this->createUserAgent(array(
+ 'http://site.with.nested.frames/' => $outer,
+ 'http://site.with.nested.frames/inner.html' => $inner,
+ 'http://site.with.nested.frames/one.html' => 'Page one',
+ 'http://site.with.nested.frames/two.html' => 'Page two',
+ 'http://site.with.nested.frames/three.html' => 'Page three')));
+
+ $browser->get('http://site.with.nested.frames/');
+ $this->assertEqual($browser->getContent(), 'Page onePage twoPage three');
+
+ $this->assertTrue($browser->setFrameFocusByIndex(1));
+ $this->assertEqual($browser->getFrameFocus(), array(1));
+ $this->assertTrue($browser->setFrameFocusByIndex(1));
+ $this->assertEqual($browser->getFrameFocus(), array(1, 1));
+ $this->assertEqual($browser->getContent(), 'Page one');
+
+ $this->assertTrue($browser->setFrameFocusByIndex(2));
+ $this->assertEqual($browser->getFrameFocus(), array(1, 2));
+ $this->assertEqual($browser->getContent(), 'Page two');
+
+ $browser->clearFrameFocus();
+ $this->assertTrue($browser->setFrameFocusByIndex(2));
+ $this->assertEqual($browser->getFrameFocus(), array(2));
+ $this->assertEqual($browser->getContent(), 'Page three');
+
+ $this->assertTrue($browser->setFrameFocusByIndex(1));
+ $this->assertEqual($browser->getContent(), 'Page onePage two');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/dumper_test.php b/tests/UnitTests/simpletest/test/dumper_test.php
new file mode 100644
index 00000000..f0169247
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/dumper_test.php
@@ -0,0 +1,87 @@
+<?php
+ // $Id: dumper_test.php,v 1.4 2004/09/24 22:55:10 lastcraft Exp $
+
+ class DumperDummy {
+ }
+
+ class TestOfTextFormatting extends UnitTestCase {
+
+ function testClipping() {
+ $dumper = new SimpleDumper();
+ $this->assertEqual(
+ $dumper->clipString("Hello", 6),
+ "Hello",
+ "Hello, 6->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello", 5),
+ "Hello",
+ "Hello, 5->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 3),
+ "Hel...",
+ "Hello world, 3->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 6, 3),
+ "Hello ...",
+ "Hello world, 6, 3->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 3, 6),
+ "...o w...",
+ "Hello world, 3, 6->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 4, 11),
+ "...orld",
+ "Hello world, 4, 11->%s");
+ $this->assertEqual(
+ $dumper->clipString("Hello world", 4, 12),
+ "...orld",
+ "Hello world, 4, 12->%s");
+ }
+
+ function testDescribeNull() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/null/i', $dumper->describeValue(null));
+ }
+
+ function testDescribeBoolean() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/boolean/i', $dumper->describeValue(true));
+ $this->assertWantedPattern('/true/i', $dumper->describeValue(true));
+ $this->assertWantedPattern('/false/i', $dumper->describeValue(false));
+ }
+
+ function testDescribeString() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/string/i', $dumper->describeValue('Hello'));
+ $this->assertWantedPattern('/Hello/', $dumper->describeValue('Hello'));
+ }
+
+ function testDescribeInteger() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/integer/i', $dumper->describeValue(35));
+ $this->assertWantedPattern('/35/', $dumper->describeValue(35));
+ }
+
+ function testDescribeFloat() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/float/i', $dumper->describeValue(0.99));
+ $this->assertWantedPattern('/0\.99/', $dumper->describeValue(0.99));
+ }
+
+ function testDescribeArray() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern('/array/i', $dumper->describeValue(array(1, 4)));
+ $this->assertWantedPattern('/2/i', $dumper->describeValue(array(1, 4)));
+ }
+
+ function testDescribeObject() {
+ $dumper = new SimpleDumper();
+ $this->assertWantedPattern(
+ '/object/i',
+ $dumper->describeValue(new DumperDummy()));
+ $this->assertWantedPattern(
+ '/DumperDummy/i',
+ $dumper->describeValue(new DumperDummy()));
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/encoding_test.php b/tests/UnitTests/simpletest/test/encoding_test.php
new file mode 100644
index 00000000..409283d5
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/encoding_test.php
@@ -0,0 +1,134 @@
+<?php
+ // $Id: encoding_test.php,v 1.6 2005/01/02 23:43:28 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../url.php');
+
+ class FormEncodingTestCase extends UnitTestCase {
+
+ function testEmpty() {
+ $encoding = &new SimpleFormEncoding();
+ $this->assertIdentical($encoding->getValue('a'), false);
+ $this->assertIdentical($encoding->getKeys(), array());
+ $this->assertIdentical($encoding->asString(), '');
+ }
+
+ function testPrefilled() {
+ $encoding = &new SimpleFormEncoding(array('a' => 'aaa'));
+ $this->assertIdentical($encoding->getValue('a'), 'aaa');
+ $this->assertIdentical($encoding->getKeys(), array('a'));
+ $this->assertIdentical($encoding->asString(), 'a=aaa');
+ }
+
+ function testPrefilledWithObject() {
+ $encoding = &new SimpleFormEncoding(new SimpleFormEncoding(array('a' => 'aaa')));
+ $this->assertIdentical($encoding->getValue('a'), 'aaa');
+ $this->assertIdentical($encoding->getKeys(), array('a'));
+ $this->assertIdentical($encoding->asString(), 'a=aaa');
+ }
+
+ function testMultiplePrefilled() {
+ $encoding = &new SimpleFormEncoding(array('a' => array('a1', 'a2')));
+ $this->assertIdentical($encoding->getValue('a'), array('a1', 'a2'));
+ $this->assertIdentical($encoding->asString(), 'a=a1&a=a2');
+ }
+
+ function testSingleParameter() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', 'Hello');
+ $this->assertEqual($encoding->getValue('a'), 'Hello');
+ $this->assertIdentical($encoding->asString(), 'a=Hello');
+ }
+
+ function testFalseParameter() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', false);
+ $this->assertEqual($encoding->getValue('a'), false);
+ $this->assertIdentical($encoding->asString(), '');
+ }
+
+ function testUrlEncoding() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', 'Hello there!');
+ $this->assertIdentical($encoding->asString(), 'a=Hello+there%21');
+ }
+
+ function testMultipleParameter() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', 'Hello');
+ $encoding->add('b', 'Goodbye');
+ $this->assertIdentical($encoding->asString(), 'a=Hello&b=Goodbye');
+ }
+
+ function testEmptyParameters() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', '');
+ $encoding->add('b', '');
+ $this->assertIdentical($encoding->asString(), 'a=&b=');
+ }
+
+ function testRepeatedParameter() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', 'Hello');
+ $encoding->add('a', 'Goodbye');
+ $this->assertIdentical($encoding->getValue('a'), array('Hello', 'Goodbye'));
+ $this->assertIdentical($encoding->asString(), 'a=Hello&a=Goodbye');
+ }
+
+ function testDefaultCoordinatesAreUnset() {
+ $encoding = &new SimpleFormEncoding();
+ $this->assertIdentical($encoding->getX(), false);
+ $this->assertIdentical($encoding->getY(), false);
+ }
+
+ function testSettingCoordinates() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->setCoordinates('32', '45');
+ $this->assertIdentical($encoding->getX(), 32);
+ $this->assertIdentical($encoding->getY(), 45);
+ $this->assertIdentical($encoding->asString(), '?32,45');
+ }
+
+ function testClearingCordinates() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->setCoordinates('32', '45');
+ $encoding->setCoordinates();
+ $this->assertIdentical($encoding->getX(), false);
+ $this->assertIdentical($encoding->getY(), false);
+ }
+
+ function testAddingLists() {
+ $encoding = &new SimpleFormEncoding();
+ $encoding->add('a', array('Hello', 'Goodbye'));
+ $this->assertIdentical($encoding->getValue('a'), array('Hello', 'Goodbye'));
+ $this->assertIdentical($encoding->asString(), 'a=Hello&a=Goodbye');
+ }
+
+ function testMergeInHash() {
+ $encoding = &new SimpleFormEncoding(array('a' => 'A1', 'b' => 'B'));
+ $encoding->merge(array('a' => 'A2'));
+ $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2'));
+ $this->assertIdentical($encoding->getValue('b'), 'B');
+ }
+
+ function testMergeInObject() {
+ $encoding = &new SimpleFormEncoding(array('a' => 'A1', 'b' => 'B'));
+ $encoding->merge(new SimpleFormEncoding(array('a' => 'A2')));
+ $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2'));
+ $this->assertIdentical($encoding->getValue('b'), 'B');
+ }
+
+ function testMergeInObjectWithCordinates() {
+ $incoming = new SimpleFormEncoding(array('a' => 'A2'));
+ $incoming->setCoordinates(25, 24);
+
+ $encoding = &new SimpleFormEncoding(array('a' => 'A1'));
+ $encoding->setCoordinates(1, 2);
+ $encoding->merge($incoming);
+
+ $this->assertIdentical($encoding->getValue('a'), array('A1', 'A2'));
+ $this->assertIdentical($encoding->getX(), 25);
+ $this->assertIdentical($encoding->getY(), 24);
+ $this->assertIdentical($encoding->asString(), 'a=A1&a=A2?25,24');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/errors_test.php b/tests/UnitTests/simpletest/test/errors_test.php
new file mode 100644
index 00000000..57aff612
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/errors_test.php
@@ -0,0 +1,139 @@
+<?php
+ require_once(dirname(__FILE__) . '/../errors.php');
+
+ class TestOfErrorQueue extends UnitTestCase {
+
+ function setUp() {
+ $queue = &SimpleErrorQueue::instance();
+ $queue->clear();
+ }
+
+ function tearDown() {
+ $queue = &SimpleErrorQueue::instance();
+ $queue->clear();
+ }
+
+ function testSingleton() {
+ $this->assertReference(
+ SimpleErrorQueue::instance(),
+ SimpleErrorQueue::instance());
+ $this->assertIsA(SimpleErrorQueue::instance(), 'SimpleErrorQueue');
+ }
+
+ function testEmpty() {
+ $queue = &SimpleErrorQueue::instance();
+ $this->assertTrue($queue->isEmpty());
+ $this->assertFalse($queue->extract());
+ }
+
+ function testOrder() {
+ $queue = &SimpleErrorQueue::instance();
+ $queue->add(1024, 'Ouch', 'here.php', 100, array());
+ $this->assertFalse($queue->isEmpty());
+ $queue->add(512, 'Yuk', 'there.php', 101, array());
+ $this->assertEqual(
+ $queue->extract(),
+ array(1024, 'Ouch', 'here.php', 100, array()));
+ $this->assertEqual(
+ $queue->extract(),
+ array(512, 'Yuk', 'there.php', 101, array()));
+ $this->assertFalse($queue->extract());
+ }
+ }
+
+ class TestOfErrorTrap extends UnitTestCase {
+ var $_old;
+
+ function setUp() {
+ $this->_old = error_reporting(E_ALL);
+ set_error_handler('simpleTestErrorHandler');
+ }
+
+ function tearDown() {
+ restore_error_handler();
+ error_reporting($this->_old);
+ }
+
+ function testTrappedErrorPlacedInQueue() {
+ $queue = &SimpleErrorQueue::instance();
+ $this->assertFalse($queue->extract());
+ trigger_error('Ouch!');
+ list($severity, $message, $file, $line, $globals) = $queue->extract();
+ $this->assertEqual($message, 'Ouch!');
+ $this->assertEqual($file, __FILE__);
+ $this->assertFalse($queue->extract());
+ }
+ }
+
+ class TestOfErrors extends UnitTestCase {
+ var $_old;
+
+ function setUp() {
+ $this->_old = error_reporting(E_ALL);
+ }
+
+ function tearDown() {
+ error_reporting($this->_old);
+ }
+
+ function testDefaultWhenAllReported() {
+ error_reporting(E_ALL);
+ trigger_error('Ouch!');
+ $this->assertError('Ouch!');
+ }
+
+ function testNoticeWhenReported() {
+ error_reporting(E_ALL);
+ trigger_error('Ouch!', E_USER_NOTICE);
+ $this->assertError('Ouch!');
+ }
+
+ function testWarningWhenReported() {
+ error_reporting(E_ALL);
+ trigger_error('Ouch!', E_USER_WARNING);
+ $this->assertError('Ouch!');
+ }
+
+ function testErrorWhenReported() {
+ error_reporting(E_ALL);
+ trigger_error('Ouch!', E_USER_ERROR);
+ $this->assertError('Ouch!');
+ }
+
+ function testNoNoticeWhenNotReported() {
+ error_reporting(0);
+ trigger_error('Ouch!', E_USER_NOTICE);
+ $this->assertNoErrors();
+ }
+
+ function testNoWarningWhenNotReported() {
+ error_reporting(0);
+ trigger_error('Ouch!', E_USER_WARNING);
+ $this->assertNoErrors();
+ }
+
+ function testNoErrorWhenNotReported() {
+ error_reporting(0);
+ trigger_error('Ouch!', E_USER_ERROR);
+ $this->assertNoErrors();
+ }
+
+ function testNoticeSuppressedWhenReported() {
+ error_reporting(E_ALL);
+ @trigger_error('Ouch!', E_USER_NOTICE);
+ $this->assertNoErrors();
+ }
+
+ function testWarningSuppressedWhenReported() {
+ error_reporting(E_ALL);
+ @trigger_error('Ouch!', E_USER_WARNING);
+ $this->assertNoErrors();
+ }
+
+ function testErrorSuppressedWhenReported() {
+ error_reporting(E_ALL);
+ @trigger_error('Ouch!', E_USER_ERROR);
+ $this->assertNoErrors();
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/expectation_test.php b/tests/UnitTests/simpletest/test/expectation_test.php
new file mode 100644
index 00000000..f16db28a
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/expectation_test.php
@@ -0,0 +1,282 @@
+<?php
+ // $Id: expectation_test.php,v 1.19 2005/01/13 01:31:57 lastcraft Exp $
+ require_once(dirname(__FILE__).DIRECTORY_SEPARATOR . '../expectation.php');
+
+ class TestOfEquality extends UnitTestCase {
+
+ function testBoolean() {
+ $is_true = &new EqualExpectation(true);
+ $this->assertTrue($is_true->test(true));
+ $this->assertFalse($is_true->test(false));
+ $this->assertWantedPattern(
+ '/equal expectation.*?boolean: true/i',
+ $is_true->testMessage(true));
+ $this->assertWantedPattern(
+ '/fails.*?boolean.*?boolean/i',
+ $is_true->testMessage(false));
+ }
+
+ function testStringMatch() {
+ $hello = &new EqualExpectation("Hello");
+ $this->assertTrue($hello->test("Hello"));
+ $this->assertFalse($hello->test("Goodbye"));
+ $this->assertWantedPattern('/Equal expectation.*?Hello/', $hello->testMessage("Hello"));
+ $this->assertWantedPattern('/fails/', $hello->testMessage("Goodbye"));
+ $this->assertWantedPattern('/fails.*?goodbye/i', $hello->testMessage("Goodbye"));
+ }
+
+ function testStringPosition() {
+ $comparisons = array(
+ "ab" => 2,
+ "a" => 1,
+ "abcz" => 3,
+ "abz" => 2,
+ "az" => 1,
+ "z" => 0);
+ $str = &new EqualExpectation("abc");
+ foreach ($comparisons as $compare => $position) {
+ $this->assertWantedPattern(
+ "/at character $position/",
+ $str->testMessage($compare));
+ }
+ $str = &new EqualExpectation("abcd");
+ foreach ($comparisons as $compare => $position) {
+ $this->assertWantedPattern(
+ "/at character $position/",
+ $str->testMessage($compare));
+ }
+ }
+
+ function testInteger() {
+ $fifteen = &new EqualExpectation(15);
+ $this->assertTrue($fifteen->test(15));
+ $this->assertFalse($fifteen->test(14));
+ $this->assertWantedPattern(
+ '/equal expectation.*?15/i',
+ $fifteen->testMessage(15));
+ $this->assertWantedPattern(
+ '/fails.*?15.*?14/i',
+ $fifteen->testMessage(14));
+ }
+
+ function testFloat() {
+ $pi = &new EqualExpectation(3.14);
+ $this->assertTrue($pi->test(3.14));
+ $this->assertFalse($pi->test(3.15));
+ $this->assertWantedPattern(
+ '/float.*?3\.14/i',
+ $pi->testMessage(3.14));
+ $this->assertWantedPattern(
+ '/fails.*?3\.14.*?3\.15/i',
+ $pi->testMessage(3.15));
+ }
+
+ function testArray() {
+ $colours = &new EqualExpectation(array("r", "g", "b"));
+ $this->assertTrue($colours->test(array("r", "g", "b")));
+ $this->assertFalse($colours->test(array("g", "b", "r")));
+ $this->assertEqual(
+ $colours->testMessage(array("r", "g", "b")),
+ "Equal expectation [Array: 3 items]");
+ $this->assertWantedPattern('/fails/', $colours->testMessage(array("r", "g", "z")));
+ $this->assertWantedPattern(
+ '/\[2\] at character 0/',
+ $colours->testMessage(array("r", "g", "z")));
+ $this->assertWantedPattern(
+ '/key.*? does not match/',
+ $colours->testMessage(array("r", "g")));
+ $this->assertWantedPattern(
+ '/key.*? does not match/',
+ $colours->testMessage(array("r", "g", "b", "z")));
+ }
+
+ function testHash() {
+ $is_blue = &new EqualExpectation(array("r" => 0, "g" => 0, "b" => 255));
+ $this->assertTrue($is_blue->test(array("r" => 0, "g" => 0, "b" => 255)));
+ $this->assertFalse($is_blue->test(array("r" => 0, "g" => 255, "b" => 0)));
+ $this->assertWantedPattern(
+ '/array.*?3 items/i',
+ $is_blue->testMessage(array("r" => 0, "g" => 0, "b" => 255)));
+ $this->assertWantedPattern(
+ '/fails.*?\[b\]/',
+ $is_blue->testMessage(array("r" => 0, "g" => 0, "b" => 254)));
+ }
+
+ function testNestedHash() {
+ $tree = &new EqualExpectation(array(
+ "a" => 1,
+ "b" => array(
+ "c" => 2,
+ "d" => "Three")));
+ $this->assertWantedPattern(
+ '/member.*?\[b\].*?\[d\].*?at character 5/',
+ $tree->testMessage(array(
+ "a" => 1,
+ "b" => array(
+ "c" => 2,
+ "d" => "Threeish"))));
+ }
+
+ function testHashWithOutOfOrderKeysShouldStillMatch() {
+ $any_order = &new EqualExpectation(array('a' => 1, 'b' => 2));
+ $this->assertTrue($any_order->test(array('b' => 2, 'a' => 1)));
+ }
+ }
+
+ class TestOfInequality extends UnitTestCase {
+
+ function testStringMismatch() {
+ $not_hello = &new NotEqualExpectation("Hello");
+ $this->assertTrue($not_hello->test("Goodbye"));
+ $this->assertFalse($not_hello->test("Hello"));
+ $this->assertWantedPattern(
+ '/at character 0/',
+ $not_hello->testMessage("Goodbye"));
+ $this->assertWantedPattern(
+ '/matches/',
+ $not_hello->testMessage("Hello"));
+ }
+ }
+
+ class RecursiveNasty {
+ var $_me;
+
+ function RecursiveNasty() {
+ $this->_me = $this;
+ }
+ }
+
+ class TestOfIdentity extends UnitTestCase {
+
+ function testType() {
+ $string = &new IdenticalExpectation("37");
+ $this->assertTrue($string->test("37"));
+ $this->assertFalse($string->test(37));
+ $this->assertFalse($string->test("38"));
+ $this->assertWantedPattern(
+ '/identical.*?string.*?37/i',
+ $string->testMessage("37"));
+ $this->assertWantedPattern(
+ '/fails.*?37/',
+ $string->testMessage(37));
+ $this->assertWantedPattern(
+ '/at character 1/',
+ $string->testMessage("38"));
+ }
+
+ function _testNastyPhp5Bug() {
+ $this->assertFalse(new RecursiveNasty() != new RecursiveNasty());
+ }
+
+ function _testReallyHorribleRecursiveStructure() {
+ $hopeful = &new IdenticalExpectation(new RecursiveNasty());
+ $this->assertTrue($hopeful->test(new RecursiveNasty()));
+ }
+ }
+
+ class TestOfNonIdentity extends UnitTestCase {
+
+ function testType() {
+ $string = &new NotIdenticalExpectation("37");
+ $this->assertTrue($string->test("38"));
+ $this->assertTrue($string->test(37));
+ $this->assertFalse($string->test("37"));
+ $this->assertWantedPattern(
+ '/at character 1/',
+ $string->testMessage("38"));
+ $this->assertWantedPattern(
+ '/passes.*?type/',
+ $string->testMessage(37));
+ }
+ }
+
+ class TestOfPatterns extends UnitTestCase {
+
+ function testWanted() {
+ $pattern = &new WantedPatternExpectation('/hello/i');
+ $this->assertTrue($pattern->test("Hello world"));
+ $this->assertFalse($pattern->test("Goodbye world"));
+ }
+
+ function testUnwanted() {
+ $pattern = &new UnwantedPatternExpectation('/hello/i');
+ $this->assertFalse($pattern->test("Hello world"));
+ $this->assertTrue($pattern->test("Goodbye world"));
+ }
+ }
+
+ class ExpectedMethodTarget {
+ function hasThisMethod() {}
+ }
+
+ class TestOfMethodExistence extends UnitTestCase {
+
+ function testHasMethod() {
+ $instance = &new ExpectedMethodTarget();
+ $expectation = &new MethodExistsExpectation('hasThisMethod');
+ $this->assertTrue($expectation->test($instance));
+ $expectation = &new MethodExistsExpectation('doesNotHaveThisMethod');
+ $this->assertFalse($expectation->test($instance));
+ }
+ }
+
+ class TestOfIsA extends UnitTestCase {
+
+ function testString() {
+ $expectation = &new IsAExpectation('string');
+ $this->assertTrue($expectation->test('Hello'));
+ $this->assertFalse($expectation->test(5));
+ }
+
+ function testBoolean() {
+ $expectation = &new IsAExpectation('boolean');
+ $this->assertTrue($expectation->test(true));
+ $this->assertFalse($expectation->test(1));
+ }
+
+ function testBool() {
+ $expectation = &new IsAExpectation('bool');
+ $this->assertTrue($expectation->test(true));
+ $this->assertFalse($expectation->test(1));
+ }
+
+ function testDouble() {
+ $expectation = &new IsAExpectation('double');
+ $this->assertTrue($expectation->test(5.0));
+ $this->assertFalse($expectation->test(5));
+ }
+
+ function testFloat() {
+ $expectation = &new IsAExpectation('float');
+ $this->assertTrue($expectation->test(5.0));
+ $this->assertFalse($expectation->test(5));
+ }
+
+ function testReal() {
+ $expectation = &new IsAExpectation('real');
+ $this->assertTrue($expectation->test(5.0));
+ $this->assertFalse($expectation->test(5));
+ }
+
+ function testInteger() {
+ $expectation = &new IsAExpectation('integer');
+ $this->assertTrue($expectation->test(5));
+ $this->assertFalse($expectation->test(5.0));
+ }
+
+ function testInt() {
+ $expectation = &new IsAExpectation('int');
+ $this->assertTrue($expectation->test(5));
+ $this->assertFalse($expectation->test(5.0));
+ }
+ }
+
+ class TestOfNotA extends UnitTestCase {
+
+ function testString() {
+ $expectation = &new NotAExpectation('string');
+ $this->assertFalse($expectation->test('Hello'));
+ $this->assertTrue($expectation->test(5));
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/form_test.php b/tests/UnitTests/simpletest/test/form_test.php
new file mode 100644
index 00000000..b24eab2c
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/form_test.php
@@ -0,0 +1,326 @@
+<?php
+ // $Id: form_test.php,v 1.12 2005/02/22 02:17:06 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../form.php');
+ require_once(dirname(__FILE__) . '/../encoding.php');
+
+ class TestOfForm extends UnitTestCase {
+
+ function testFormAttributes() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'action' => 'here.php', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $this->assertEqual($form->getMethod(), 'get');
+ $this->assertEqual(
+ $form->getAction(),
+ new SimpleUrl('http://host/a/here.php'));
+ $this->assertIdentical($form->getId(), '33');
+ $this->assertNull($form->getValue('a'));
+ }
+
+ function testEmptyAction() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'action' => '', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $this->assertEqual(
+ $form->getAction(),
+ new SimpleUrl('http://host/a/index.html'));
+ }
+
+ function testMissingAction() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $this->assertEqual(
+ $form->getAction(),
+ new SimpleUrl('http://host/a/index.html'));
+ }
+
+ function testRootAction() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'action' => '/', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $this->assertEqual(
+ $form->getAction(),
+ new SimpleUrl('http://host/'));
+ }
+
+ function testDefaultFrameTargetOnForm() {
+ $tag = new SimpleFormTag(array('method' => 'GET', 'action' => 'here.php', 'id' => '33'));
+ $form = new SimpleForm($tag, new SimpleUrl('http://host/a/index.html'));
+ $form->setDefaultTarget('frame');
+
+ $expected = new SimpleUrl('http://host/a/here.php');
+ $expected->setTarget('frame');
+ $this->assertEqual($form->getAction(), $expected);
+ }
+
+ function testTextWidget() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleTextTag(
+ array('name' => 'me', 'type' => 'text', 'value' => 'Myself')));
+ $this->assertIdentical($form->getValue('me'), 'Myself');
+ $this->assertTrue($form->setField('me', 'Not me'));
+ $this->assertFalse($form->setField('not_present', 'Not me'));
+ $this->assertIdentical($form->getValue('me'), 'Not me');
+ $this->assertNull($form->getValue('not_present'));
+ }
+
+ function testTextWidgetById() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleTextTag(
+ array('name' => 'me', 'type' => 'text', 'value' => 'Myself', 'id' => 50)));
+ $this->assertIdentical($form->getValueById(50), 'Myself');
+ $this->assertTrue($form->setFieldById(50, 'Not me'));
+ $this->assertIdentical($form->getValueById(50), 'Not me');
+ }
+
+ function testSubmitEmpty() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $this->assertIdentical($form->submit(), new SimpleFormEncoding());
+ }
+
+ function testSubmitButton() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $form->addWidget(new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 'go', 'value' => 'Go!', 'id' => '9')));
+ $this->assertTrue($form->hasSubmitName('go'));
+ $this->assertEqual($form->getValue('go'), 'Go!');
+ $this->assertEqual($form->getValueById(9), 'Go!');
+ $this->assertEqual(
+ $form->submitButtonByName('go'),
+ new SimpleFormEncoding(array('go' => 'Go!')));
+ $this->assertEqual(
+ $form->submitButtonByLabel('Go!'),
+ new SimpleFormEncoding(array('go' => 'Go!')));
+ $this->assertEqual(
+ $form->submitButtonById(9),
+ new SimpleFormEncoding(array('go' => 'Go!')));
+ }
+
+ function testSubmitWithAdditionalParameters() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $form->addWidget(new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 'go', 'value' => 'Go!', 'id' => '9')));
+ $this->assertEqual(
+ $form->submitButtonByName('go', array('a' => 'A')),
+ new SimpleFormEncoding(array('go' => 'Go!', 'a' => 'A')));
+ $this->assertEqual(
+ $form->submitButtonByLabel('Go!', array('a' => 'A')),
+ new SimpleFormEncoding(array('go' => 'Go!', 'a' => 'A')));
+ $this->assertEqual(
+ $form->submitButtonById(9, array('a' => 'A')),
+ new SimpleFormEncoding(array('go' => 'Go!', 'a' => 'A')));
+ }
+
+ function testSubmitButtonWithLabelOfSubmit() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $form->addWidget(new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 'test', 'value' => 'Submit', 'id' => '9')));
+ $this->assertTrue($form->hasSubmitName('test'));
+ $this->assertEqual($form->getValue('test'), 'Submit');
+ $this->assertEqual($form->getValueById(9), 'Submit');
+ $this->assertEqual(
+ $form->submitButtonByName('test'),
+ new SimpleFormEncoding(array('test' => 'Submit')));
+ $this->assertEqual(
+ $form->submitButtonByLabel('Submit'),
+ new SimpleFormEncoding(array('test' => 'Submit')));
+ $this->assertEqual(
+ $form->submitButtonById(9),
+ new SimpleFormEncoding(array('test' => 'Submit')));
+ }
+
+ function testSubmitButtonWithWhitespacePaddedLabelOfSubmit() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $form->addWidget(new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 'test', 'value' => ' Submit ', 'id' => '9')));
+ $this->assertEqual($form->getValue('test'), ' Submit ');
+ $this->assertEqual($form->getValueById(9), ' Submit ');
+ $this->assertEqual(
+ $form->submitButtonByLabel('Submit'),
+ new SimpleFormEncoding(array('test' => ' Submit ')));
+ }
+
+ function testImageSubmitButton() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleImageSubmitTag(array(
+ 'type' => 'image',
+ 'src' => 'source.jpg',
+ 'name' => 'go',
+ 'alt' => 'Go!',
+ 'id' => '9')));
+ $this->assertTrue($form->hasImageLabel('Go!'));
+ $this->assertEqual(
+ $form->submitImageByLabel('Go!', 100, 101),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101)));
+ $this->assertTrue($form->hasImageName('go'));
+ $this->assertEqual(
+ $form->submitImageByName('go', 100, 101),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101)));
+ $this->assertTrue($form->hasImageId(9));
+ $this->assertEqual(
+ $form->submitImageById(9, 100, 101),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101)));
+ }
+
+ function testImageSubmitButtonWithAdditionalData() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleImageSubmitTag(array(
+ 'type' => 'image',
+ 'src' => 'source.jpg',
+ 'name' => 'go',
+ 'alt' => 'Go!',
+ 'id' => '9')));
+ $this->assertEqual(
+ $form->submitImageByLabel('Go!', 100, 101, array('a' => 'A')),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101, 'a' => 'A')));
+ $this->assertTrue($form->hasImageName('go'));
+ $this->assertEqual(
+ $form->submitImageByName('go', 100, 101, array('a' => 'A')),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101, 'a' => 'A')));
+ $this->assertTrue($form->hasImageId(9));
+ $this->assertEqual(
+ $form->submitImageById(9, 100, 101, array('a' => 'A')),
+ new SimpleFormEncoding(array('go.x' => 100, 'go.y' => 101, 'a' => 'A')));
+ }
+
+ function testButtonTag() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('http://host'));
+ $widget = new SimpleButtonTag(
+ array('type' => 'submit', 'name' => 'go', 'value' => 'Go', 'id' => '9'));
+ $widget->addContent('Go!');
+ $form->addWidget($widget);
+ $this->assertTrue($form->hasSubmitName('go'));
+ $this->assertTrue($form->hasSubmitLabel('Go!'));
+ $this->assertEqual(
+ $form->submitButtonByName('go'),
+ new SimpleFormEncoding(array('go' => 'Go')));
+ $this->assertEqual(
+ $form->submitButtonByLabel('Go!'),
+ new SimpleFormEncoding(array('go' => 'Go')));
+ $this->assertEqual(
+ $form->submitButtonById(9),
+ new SimpleFormEncoding(array('go' => 'Go')));
+ }
+
+ function testSingleSelectFieldSubmitted() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $select = new SimpleSelectionTag(array('name' => 'a'));
+ $select->addTag(new SimpleOptionTag(
+ array('value' => 'aaa', 'selected' => '')));
+ $form->addWidget($select);
+ $this->assertIdentical(
+ $form->submit(),
+ new SimpleFormEncoding(array('a' => 'aaa')));
+ }
+
+ function testUnchecked() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleCheckboxTag(
+ array('name' => 'me', 'type' => 'checkbox')));
+ $this->assertIdentical($form->getValue('me'), false);
+ $this->assertTrue($form->setField('me', 'on'));
+ $this->assertEqual($form->getValue('me'), 'on');
+ $this->assertFalse($form->setField('me', 'other'));
+ $this->assertEqual($form->getValue('me'), 'on');
+ }
+
+ function testChecked() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleCheckboxTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'checkbox', 'checked' => '')));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ $this->assertFalse($form->setField('me', 'on'));
+ $this->assertEqual($form->getValue('me'), 'a');
+ $this->assertTrue($form->setField('me', false));
+ $this->assertEqual($form->getValue('me'), false);
+ }
+
+ function testSingleUncheckedRadioButton() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'radio')));
+ $this->assertIdentical($form->getValue('me'), false);
+ $this->assertTrue($form->setField('me', 'a'));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ }
+
+ function testSingleCheckedRadioButton() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'radio', 'checked' => '')));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ $this->assertFalse($form->setField('me', 'other'));
+ }
+
+ function testUncheckedRadioButtons() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'radio')));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'b', 'type' => 'radio')));
+ $this->assertIdentical($form->getValue('me'), false);
+ $this->assertTrue($form->setField('me', 'a'));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ $this->assertTrue($form->setField('me', 'b'));
+ $this->assertIdentical($form->getValue('me'), 'b');
+ $this->assertFalse($form->setField('me', 'c'));
+ $this->assertIdentical($form->getValue('me'), 'b');
+ }
+
+ function testCheckedRadioButtons() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'a', 'type' => 'radio')));
+ $form->addWidget(new SimpleRadioButtonTag(
+ array('name' => 'me', 'value' => 'b', 'type' => 'radio', 'checked' => '')));
+ $this->assertIdentical($form->getValue('me'), 'b');
+ $this->assertTrue($form->setField('me', 'a'));
+ $this->assertIdentical($form->getValue('me'), 'a');
+ }
+
+ function testMultipleFieldsWithSameKey() {
+ $form = new SimpleForm(
+ new SimpleFormTag(array()),
+ new SimpleUrl('htp://host'));
+ $form->addWidget(new SimpleCheckboxTag(
+ array('name' => 'a', 'type' => 'checkbox', 'value' => 'me')));
+ $form->addWidget(new SimpleCheckboxTag(
+ array('name' => 'a', 'type' => 'checkbox', 'value' => 'you')));
+ $this->assertIdentical($form->getValue('a'), false);
+ $this->assertTrue($form->setField('a', 'me'));
+ $this->assertIdentical($form->getValue('a'), 'me');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/frames_test.php b/tests/UnitTests/simpletest/test/frames_test.php
new file mode 100644
index 00000000..5226d070
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/frames_test.php
@@ -0,0 +1,569 @@
+<?php
+ // $Id: frames_test.php,v 1.28 2004/11/30 05:34:00 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../tag.php');
+ require_once(dirname(__FILE__) . '/../page.php');
+ require_once(dirname(__FILE__) . '/../frames.php');
+
+ Mock::generate('SimplePage');
+ Mock::generate('SimpleForm');
+
+ class TestOfFrameset extends UnitTestCase {
+
+ function testTitleReadFromFramesetPage() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getTitle', 'This page');
+ $frameset = &new SimpleFrameset($page);
+ $this->assertEqual($frameset->getTitle(), 'This page');
+ }
+
+ function TestHeadersReadFromFramesetByDefault() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getHeaders', 'Header: content');
+ $page->setReturnValue('getMimeType', 'text/xml');
+ $page->setReturnValue('getResponseCode', 401);
+ $page->setReturnValue('getTransportError', 'Could not parse headers');
+ $page->setReturnValue('getAuthentication', 'Basic');
+ $page->setReturnValue('getRealm', 'Safe place');
+
+ $frameset = &new SimpleFrameset($page);
+
+ $this->assertIdentical($frameset->getHeaders(), 'Header: content');
+ $this->assertIdentical($frameset->getMimeType(), 'text/xml');
+ $this->assertIdentical($frameset->getResponseCode(), 401);
+ $this->assertIdentical($frameset->getTransportError(), 'Could not parse headers');
+ $this->assertIdentical($frameset->getAuthentication(), 'Basic');
+ $this->assertIdentical($frameset->getRealm(), 'Safe place');
+ }
+
+ function testEmptyFramesetHasNoContent() {
+ $page = &new MockSimplePage($this);
+ $page->setReturnValue('getRaw', 'This content');
+ $frameset = &new SimpleFrameset($page);
+ $this->assertEqual($frameset->getRaw(), '');
+ }
+
+ function testRawContentIsFromOnlyFrame() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame = &new MockSimplePage($this);
+ $frame->setReturnValue('getRaw', 'Stuff');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame);
+ $this->assertEqual($frameset->getRaw(), 'Stuff');
+ }
+
+ function testRawContentIsFromAllFrames() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
+ }
+
+ function testTextContentIsFromOnlyFrame() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getText');
+
+ $frame = &new MockSimplePage($this);
+ $frame->setReturnValue('getText', 'Stuff');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame);
+ $this->assertEqual($frameset->getText(), 'Stuff');
+ }
+
+ function testTextContentIsFromAllFrames() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getText');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getText', 'Stuff1');
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getText', 'Stuff2');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $this->assertEqual($frameset->getText(), 'Stuff1 Stuff2');
+ }
+
+ function testFieldIsFirstInFramelist() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getField', null);
+ $frame1->expectOnce('getField', array('a'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getField', 'A');
+ $frame2->expectOnce('getField', array('a'));
+
+ $frame3 = &new MockSimplePage($this);
+ $frame3->expectNever('getField');
+
+ $page = &new MockSimplePage($this);
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $frameset->addFrame($frame3);
+
+ $this->assertIdentical($frameset->getField('a'), 'A');
+ $frame1->tally();
+ $frame2->tally();
+ }
+
+ function testFrameReplacementByIndex() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->setFrame(array(1), $frame2);
+ $this->assertEqual($frameset->getRaw(), 'Stuff2');
+ }
+
+ function testFrameReplacementByName() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1, 'a');
+ $frameset->setFrame(array('a'), $frame2);
+ $this->assertEqual($frameset->getRaw(), 'Stuff2');
+ }
+ }
+
+ class TestOfFrameNavigation extends UnitTestCase {
+
+ function testStartsWithoutFrameFocus() {
+ $page = &new MockSimplePage($this);
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame);
+ $this->assertFalse($frameset->getFrameFocus());
+ }
+
+ function testCanFocusOnSingleFrame() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRaw');
+
+ $frame = &new MockSimplePage($this);
+ $frame->setReturnValue('getFrameFocus', array());
+ $frame->setReturnValue('getRaw', 'Stuff');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame);
+
+ $this->assertFalse($frameset->setFrameFocusByIndex(0));
+ $this->assertTrue($frameset->setFrameFocusByIndex(1));
+ $this->assertEqual($frameset->getRaw(), 'Stuff');
+ $this->assertFalse($frameset->setFrameFocusByIndex(2));
+ $this->assertIdentical($frameset->getFrameFocus(), array(1));
+ }
+
+ function testContentComesFromFrameInFocus() {
+ $page = &new MockSimplePage($this);
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+ $frame1->setReturnValue('getFrameFocus', array());
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+ $frame2->setReturnValue('getFrameFocus', array());
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+
+ $this->assertTrue($frameset->setFrameFocusByIndex(1));
+ $this->assertEqual($frameset->getFrameFocus(), array(1));
+ $this->assertEqual($frameset->getRaw(), 'Stuff1');
+
+ $this->assertTrue($frameset->setFrameFocusByIndex(2));
+ $this->assertEqual($frameset->getFrameFocus(), array(2));
+ $this->assertEqual($frameset->getRaw(), 'Stuff2');
+
+ $this->assertFalse($frameset->setFrameFocusByIndex(3));
+ $this->assertEqual($frameset->getFrameFocus(), array(2));
+
+ $frameset->clearFrameFocus();
+ $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
+ }
+ function testCanFocusByName() {
+ $page = &new MockSimplePage($this);
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getRaw', 'Stuff1');
+ $frame1->setReturnValue('getFrameFocus', array());
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getRaw', 'Stuff2');
+ $frame2->setReturnValue('getFrameFocus', array());
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+
+ $this->assertTrue($frameset->setFrameFocus('A'));
+ $this->assertEqual($frameset->getFrameFocus(), array('A'));
+ $this->assertEqual($frameset->getRaw(), 'Stuff1');
+
+ $this->assertTrue($frameset->setFrameFocusByIndex(2));
+ $this->assertEqual($frameset->getFrameFocus(), array('B'));
+ $this->assertEqual($frameset->getRaw(), 'Stuff2');
+
+ $this->assertFalse($frameset->setFrameFocus('z'));
+
+ $frameset->clearFrameFocus();
+ $this->assertEqual($frameset->getRaw(), 'Stuff1Stuff2');
+ }
+ }
+
+ class TestOfFramesetPageInterface extends UnitTestCase {
+ var $_page_interface;
+ var $_frameset_interface;
+
+ function TestOfFramesetPageInterface() {
+ $this->UnitTestCase();
+ $this->_page_interface = $this->_getPageMethods();
+ $this->_frameset_interface = $this->_getFramesetMethods();
+ }
+
+ function assertListInAnyOrder($list, $expected) {
+ sort($list);
+ sort($expected);
+ $this->assertEqual($list, $expected);
+ }
+
+ function _getPageMethods() {
+ $methods = array();
+ foreach (get_class_methods('SimplePage') as $method) {
+ if (strtolower($method) == strtolower('SimplePage')) {
+ continue;
+ }
+ if (strtolower($method) == strtolower('getFrameset')) {
+ continue;
+ }
+ if (strncmp($method, '_', 1) == 0) {
+ continue;
+ }
+ if (strncmp($method, 'accept', 6) == 0) {
+ continue;
+ }
+ $methods[] = $method;
+ }
+ return $methods;
+ }
+
+ function _getFramesetMethods() {
+ $methods = array();
+ foreach (get_class_methods('SimpleFrameset') as $method) {
+ if (strtolower($method) == strtolower('SimpleFrameset')) {
+ continue;
+ }
+ if (strncmp($method, '_', 1) == 0) {
+ continue;
+ }
+ if (strncmp($method, 'add', 3) == 0) {
+ continue;
+ }
+ $methods[] = $method;
+ }
+ return $methods;
+ }
+
+ function testFramsetHasPageInterface() {
+ $difference = array();
+ foreach ($this->_page_interface as $method) {
+ if (! in_array($method, $this->_frameset_interface)) {
+ $this->fail("No [$method] in Frameset class");
+ return;
+ }
+ }
+ $this->pass('Frameset covers Page interface');
+ }
+
+ function testHeadersReadFromFrameIfInFocus() {
+ $frame = &new MockSimplePage($this);
+ $frame->setReturnValue('getUrl', new SimpleUrl('http://localhost/stuff'));
+
+ $frame->setReturnValue('getRequest', 'POST stuff');
+ $frame->setReturnValue('getMethod', 'POST');
+ $frame->setReturnValue('getRequestData', array('a' => 'A'));
+ $frame->setReturnValue('getHeaders', 'Header: content');
+ $frame->setReturnValue('getMimeType', 'text/xml');
+ $frame->setReturnValue('getResponseCode', 401);
+ $frame->setReturnValue('getTransportError', 'Could not parse headers');
+ $frame->setReturnValue('getAuthentication', 'Basic');
+ $frame->setReturnValue('getRealm', 'Safe place');
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame);
+ $frameset->setFrameFocusByIndex(1);
+
+ $url = new SimpleUrl('http://localhost/stuff');
+ $url->setTarget(1);
+ $this->assertIdentical($frameset->getUrl(), $url);
+
+ $this->assertIdentical($frameset->getRequest(), 'POST stuff');
+ $this->assertIdentical($frameset->getMethod(), 'POST');
+ $this->assertIdentical($frameset->getRequestData(), array('a' => 'A'));
+ $this->assertIdentical($frameset->getHeaders(), 'Header: content');
+ $this->assertIdentical($frameset->getMimeType(), 'text/xml');
+ $this->assertIdentical($frameset->getResponseCode(), 401);
+ $this->assertIdentical($frameset->getTransportError(), 'Could not parse headers');
+ $this->assertIdentical($frameset->getAuthentication(), 'Basic');
+ $this->assertIdentical($frameset->getRealm(), 'Safe place');
+ }
+
+ function testAbsoluteUrlsComeFromBothFrames() {
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getAbsoluteUrls');
+
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue(
+ 'getAbsoluteUrls',
+ array('http://www.lastcraft.com/', 'http://myserver/'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue(
+ 'getAbsoluteUrls',
+ array('http://www.lastcraft.com/', 'http://test/'));
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $this->assertListInAnyOrder(
+ $frameset->getAbsoluteUrls(),
+ array('http://www.lastcraft.com/', 'http://myserver/', 'http://test/'));
+ }
+
+ function testRelativeUrlsComeFromBothFrames() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue(
+ 'getRelativeUrls',
+ array('/', '.', '/test/', 'goodbye.php'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue(
+ 'getRelativeUrls',
+ array('/', '..', '/test/', 'hello.php'));
+
+ $page = &new MockSimplePage($this);
+ $page->expectNever('getRelativeUrls');
+
+ $frameset = &new SimpleFrameset($page);
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+ $this->assertListInAnyOrder(
+ $frameset->getRelativeUrls(),
+ array('/', '.', '/test/', 'goodbye.php', '..', 'hello.php'));
+ }
+
+ function testLabelledUrlsComeFromBothFrames() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue(
+ 'getUrlsByLabel',
+ array(new SimpleUrl('goodbye.php')),
+ array('a'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue(
+ 'getUrlsByLabel',
+ array(new SimpleUrl('hello.php')),
+ array('a'));
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2, 'Two');
+
+ $expected1 = new SimpleUrl('goodbye.php');
+ $expected1->setTarget(1);
+ $expected2 = new SimpleUrl('hello.php');
+ $expected2->setTarget('Two');
+ $this->assertEqual(
+ $frameset->getUrlsByLabel('a'),
+ array($expected1, $expected2));
+ }
+
+ function testUrlByIdComesFromFirstFrameToRespond() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getUrlById', new SimpleUrl('four.php'), array(4));
+ $frame1->setReturnValue('getUrlById', false, array(5));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->setReturnValue('getUrlById', false, array(4));
+ $frame2->setReturnValue('getUrlById', new SimpleUrl('five.php'), array(5));
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1);
+ $frameset->addFrame($frame2);
+
+ $four = new SimpleUrl('four.php');
+ $four->setTarget(1);
+ $this->assertEqual($frameset->getUrlById(4), $four);
+ $five = new SimpleUrl('five.php');
+ $five->setTarget(2);
+ $this->assertEqual($frameset->getUrlById(5), $five);
+ }
+
+ function testReadUrlsFromFrameInFocus() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getAbsoluteUrls', array('a'));
+ $frame1->setReturnValue('getRelativeUrls', array('r'));
+ $frame1->setReturnValue('getUrlsByLabel', array(new SimpleUrl('l')));
+ $frame1->setReturnValue('getUrlById', new SimpleUrl('i'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->expectNever('getAbsoluteUrls');
+ $frame2->expectNever('getRelativeUrls');
+ $frame2->expectNever('getUrlsByLabel');
+ $frame2->expectNever('getUrlById');
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+ $frameset->setFrameFocus('A');
+
+ $this->assertIdentical($frameset->getAbsoluteUrls(), array('a'));
+ $this->assertIdentical($frameset->getRelativeUrls(), array('r'));
+ $expected = new SimpleUrl('l');
+ $expected->setTarget('A');
+ $this->assertIdentical($frameset->getUrlsByLabel('label'), array($expected));
+ $expected = new SimpleUrl('i');
+ $expected->setTarget('A');
+ $this->assertIdentical($frameset->getUrlById(99), $expected);
+ }
+
+ function testReadFrameTaggedUrlsFromFrameInFocus() {
+ $frame = &new MockSimplePage($this);
+
+ $by_label = new SimpleUrl('l');
+ $by_label->setTarget('L');
+ $frame->setReturnValue('getUrlsByLabel', array($by_label));
+
+ $by_id = new SimpleUrl('i');
+ $by_id->setTarget('I');
+ $frame->setReturnValue('getUrlById', $by_id);
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame, 'A');
+ $frameset->setFrameFocus('A');
+
+ $this->assertIdentical($frameset->getUrlsByLabel('label'), array($by_label));
+ $this->assertIdentical($frameset->getUrlById(99), $by_id);
+ }
+
+ function testFindingFormsByAllFinders() {
+ $finders = array(
+ 'getFormBySubmitLabel', 'getFormBySubmitName',
+ 'getFormBySubmitId', 'getFormByImageLabel',
+ 'getFormByImageName', 'getFormByImageId', 'getFormById');
+ $forms = array();
+
+ $frame = &new MockSimplePage($this);
+ for ($i = 0; $i < count($finders); $i++) {
+ $forms[$i] = &new MockSimpleForm($this);
+ $frame->setReturnReference($finders[$i], $forms[$i], array('a'));
+ }
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame(new MockSimplePage($this), 'A');
+ $frameset->addFrame($frame, 'B');
+ for ($i = 0; $i < count($finders); $i++) {
+ $method = $finders[$i];
+ $this->assertReference($frameset->$method('a'), $forms[$i]);
+ }
+
+ $frameset->setFrameFocus('A');
+ for ($i = 0; $i < count($finders); $i++) {
+ $method = $finders[$i];
+ $this->assertNull($frameset->$method('a'));
+ }
+
+ $frameset->setFrameFocus('B');
+ for ($i = 0; $i < count($finders); $i++) {
+ $method = $finders[$i];
+ $this->assertReference($frameset->$method('a'), $forms[$i]);
+ }
+ }
+
+ function testSettingAllFrameFieldsWhenNoFrameFocus() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->expectOnce('setField', array('a', 'A'));
+ $frame1->expectOnce('setFieldById', array(22, 'A'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->expectOnce('setField', array('a', 'A'));
+ $frame2->expectOnce('setFieldById', array(22, 'A'));
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+
+ $frameset->setField('a', 'A');
+ $frameset->setFieldById(22, 'A');
+ $frame1->tally();
+ $frame2->tally();
+ }
+
+ function testOnlySettingFieldFromFocusedFrame() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->expectOnce('setField', array('a', 'A'));
+ $frame1->expectOnce('setFieldById', array(22, 'A'));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->expectNever('setField');
+ $frame2->expectNever('setFieldById');
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+ $frameset->setFrameFocus('A');
+
+ $frameset->setField('a', 'A');
+ $frameset->setFieldById(22, 'A');
+ $frame1->tally();
+ }
+
+ function testOnlyGettingFieldFromFocusedFrame() {
+ $frame1 = &new MockSimplePage($this);
+ $frame1->setReturnValue('getField', 'f', array('a'));
+ $frame1->setReturnValue('getFieldById', 'i', array(7));
+
+ $frame2 = &new MockSimplePage($this);
+ $frame2->expectNever('getField');
+ $frame2->expectNever('getFieldById');
+
+ $frameset = &new SimpleFrameset(new MockSimplePage($this));
+ $frameset->addFrame($frame1, 'A');
+ $frameset->addFrame($frame2, 'B');
+ $frameset->setFrameFocus('A');
+
+ $this->assertIdentical($frameset->getField('a'), 'f');
+ $this->assertIdentical($frameset->getFieldById(7), 'i');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/http_test.php b/tests/UnitTests/simpletest/test/http_test.php
new file mode 100644
index 00000000..ca201be0
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/http_test.php
@@ -0,0 +1,567 @@
+<?php
+ // $Id: http_test.php,v 1.79 2005/01/02 22:46:10 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../encoding.php');
+ require_once(dirname(__FILE__) . '/../http.php');
+ require_once(dirname(__FILE__) . '/../socket.php');
+ Mock::generate('SimpleSocket');
+ Mock::generate('SimpleRoute');
+ Mock::generatePartial('SimpleRoute', 'PartialSimpleRoute', array('_createSocket'));
+ Mock::generatePartial(
+ 'SimpleProxyRoute',
+ 'PartialSimpleProxyRoute',
+ array('_createSocket'));
+
+ class TestOfCookie extends UnitTestCase {
+
+ function testCookieDefaults() {
+ $cookie = new SimpleCookie("name");
+ $this->assertFalse($cookie->getValue());
+ $this->assertEqual($cookie->getPath(), "/");
+ $this->assertIdentical($cookie->getHost(), false);
+ $this->assertFalse($cookie->getExpiry());
+ $this->assertFalse($cookie->isSecure());
+ }
+
+ function testCookieAccessors() {
+ $cookie = new SimpleCookie(
+ "name",
+ "value",
+ "/path",
+ "Mon, 18 Nov 2002 15:50:29 GMT",
+ true);
+ $this->assertEqual($cookie->getName(), "name");
+ $this->assertEqual($cookie->getValue(), "value");
+ $this->assertEqual($cookie->getPath(), "/path/");
+ $this->assertEqual($cookie->getExpiry(), "Mon, 18 Nov 2002 15:50:29 GMT");
+ $this->assertTrue($cookie->isSecure());
+ }
+
+ function testFullHostname() {
+ $cookie = new SimpleCookie("name");
+ $this->assertTrue($cookie->setHost("host.name.here"));
+ $this->assertEqual($cookie->getHost(), "host.name.here");
+ $this->assertTrue($cookie->setHost("host.com"));
+ $this->assertEqual($cookie->getHost(), "host.com");
+ }
+
+ function testHostTruncation() {
+ $cookie = new SimpleCookie("name");
+ $cookie->setHost("this.host.name.here");
+ $this->assertEqual($cookie->getHost(), "host.name.here");
+ $cookie->setHost("this.host.com");
+ $this->assertEqual($cookie->getHost(), "host.com");
+ $this->assertTrue($cookie->setHost("dashes.in-host.com"));
+ $this->assertEqual($cookie->getHost(), "in-host.com");
+ }
+
+ function testBadHosts() {
+ $cookie = new SimpleCookie("name");
+ $this->assertFalse($cookie->setHost("gibberish"));
+ $this->assertFalse($cookie->setHost("host.here"));
+ $this->assertFalse($cookie->setHost("host..com"));
+ $this->assertFalse($cookie->setHost("..."));
+ $this->assertFalse($cookie->setHost("host.com."));
+ }
+
+ function testHostValidity() {
+ $cookie = new SimpleCookie("name");
+ $cookie->setHost("this.host.name.here");
+ $this->assertTrue($cookie->isValidHost("host.name.here"));
+ $this->assertTrue($cookie->isValidHost("that.host.name.here"));
+ $this->assertFalse($cookie->isValidHost("bad.host"));
+ $this->assertFalse($cookie->isValidHost("nearly.name.here"));
+ }
+
+ function testPathValidity() {
+ $cookie = new SimpleCookie("name", "value", "/path");
+ $this->assertFalse($cookie->isValidPath("/"));
+ $this->assertTrue($cookie->isValidPath("/path/"));
+ $this->assertTrue($cookie->isValidPath("/path/more"));
+ }
+
+ function testSessionExpiring() {
+ $cookie = new SimpleCookie("name", "value", "/path");
+ $this->assertTrue($cookie->isExpired(0));
+ }
+
+ function testTimestampExpiry() {
+ $cookie = new SimpleCookie("name", "value", "/path", 456);
+ $this->assertFalse($cookie->isExpired(0));
+ $this->assertTrue($cookie->isExpired(457));
+ $this->assertFalse($cookie->isExpired(455));
+ }
+
+ function testDateExpiry() {
+ $cookie = new SimpleCookie(
+ "name",
+ "value",
+ "/path",
+ "Mon, 18 Nov 2002 15:50:29 GMT");
+ $this->assertTrue($cookie->isExpired("Mon, 18 Nov 2002 15:50:30 GMT"));
+ $this->assertFalse($cookie->isExpired("Mon, 18 Nov 2002 15:50:28 GMT"));
+ }
+
+ function testAging() {
+ $cookie = new SimpleCookie("name", "value", "/path", 200);
+ $cookie->agePrematurely(199);
+ $this->assertFalse($cookie->isExpired(0));
+ $cookie->agePrematurely(2);
+ $this->assertTrue($cookie->isExpired(0));
+ }
+ }
+
+ class TestOfDirectRoute extends UnitTestCase {
+
+ function testDefaultGetRequest() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET /here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html'));
+
+ $this->assertReference($route->createConnection('GET', 15), $socket);
+ $socket->tally();
+ }
+
+ function testDefaultPostRequest() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("POST /here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html'));
+
+ $route->createConnection('POST', 15);
+ $socket->tally();
+ }
+
+ function testGetWithPort() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET /here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host:81\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleRoute(new SimpleUrl('http://a.valid.host:81/here.html'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+
+ function testGetWithParameters() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET /here.html?a=1&b=2 HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html?a=1&b=2'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+ }
+
+ class TestOfProxyRoute extends UnitTestCase {
+
+ function testDefaultGet() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host/here.html'),
+ new SimpleUrl('http://my-proxy'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+
+ function testDefaultPost() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("POST http://a.valid.host/here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host/here.html'),
+ new SimpleUrl('http://my-proxy'));
+
+ $route->createConnection('POST', 15);
+ $socket->tally();
+ }
+
+ function testGetWithPort() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host:81/here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8081\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host:81/here.html'),
+ new SimpleUrl('http://my-proxy:8081'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+
+ function testGetWithParameters() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html?a=1&b=2 HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 3);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host/here.html?a=1&b=2'),
+ new SimpleUrl('http://my-proxy'));
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+
+ function testGetWithAuthentication() {
+ $encoded = base64_encode('Me:Secret');
+
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html HTTP/1.0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("Proxy-Authorization: Basic $encoded\r\n"));
+ $socket->expectArgumentsAt(3, 'write', array("Connection: close\r\n"));
+ $socket->expectCallCount('write', 4);
+
+ $route = &new PartialSimpleProxyRoute($this);
+ $route->setReturnReference('_createSocket', $socket);
+ $route->SimpleProxyRoute(
+ new SimpleUrl('http://a.valid.host/here.html'),
+ new SimpleUrl('http://my-proxy'),
+ 'Me',
+ 'Secret');
+
+ $route->createConnection('GET', 15);
+ $socket->tally();
+ }
+ }
+
+ class TestOfHttpRequest extends UnitTestCase {
+
+ function testReadingBadConnection() {
+ $socket = &new MockSimpleSocket($this);
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+
+ $reponse = &$request->fetch(15);
+ $this->assertTrue($reponse->isError());
+ }
+
+ function testReadingGoodConnection() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectOnce('write', array("\r\n"));
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+ $route->expectArguments('createConnection', array('GET', 15));
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+
+ $this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
+ $socket->tally();
+ $route->tally();
+ }
+
+ function testWritingAdditionalHeaders() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("My: stuff\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("\r\n"));
+ $socket->expectCallCount('write', 2);
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+ $request->addHeaderLine('My: stuff');
+ $request->fetch(15);
+
+ $socket->tally();
+ }
+
+ function testCookieWriting() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("Cookie: a=A\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("\r\n"));
+ $socket->expectCallCount('write', 2);
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+ $request->setCookie(new SimpleCookie('a', 'A'));
+
+ $this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
+ $socket->tally();
+ }
+
+ function testMultipleCookieWriting() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("Cookie: a=A;b=B\r\n"));
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'GET');
+ $request->setCookie(new SimpleCookie('a', 'A'));
+ $request->setCookie(new SimpleCookie('b', 'B'));
+
+ $request->fetch(15);
+ $socket->tally();
+ }
+ }
+
+ class TestOfHttpPostRequest extends UnitTestCase {
+
+ function testReadingBadConnection() {
+ $socket = &new MockSimpleSocket($this);
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+
+ $request = &new SimpleHttpRequest($route, 'POST', '');
+
+ $reponse = &$request->fetch(15);
+ $this->assertTrue($reponse->isError());
+ }
+
+ function testReadingGoodConnection() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("Content-Length: 0\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Content-Type: application/x-www-form-urlencoded\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("\r\n"));
+ $socket->expectArgumentsAt(3, 'write', array(""));
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+ $route->expectArguments('createConnection', array('POST', 15));
+
+ $request = &new SimpleHttpRequest($route, 'POST', new SimpleFormEncoding());
+
+ $this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
+ $socket->tally();
+ $route->tally();
+ }
+
+ function testContentHeadersCalculated() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->expectArgumentsAt(0, 'write', array("Content-Length: 3\r\n"));
+ $socket->expectArgumentsAt(1, 'write', array("Content-Type: application/x-www-form-urlencoded\r\n"));
+ $socket->expectArgumentsAt(2, 'write', array("\r\n"));
+ $socket->expectArgumentsAt(3, 'write', array("a=A"));
+
+ $route = &new MockSimpleRoute($this);
+ $route->setReturnReference('createConnection', $socket);
+ $route->expectArguments('createConnection', array('POST', 15));
+
+ $request = &new SimpleHttpRequest(
+ $route,
+ 'POST',
+ new SimpleFormEncoding(array('a' => 'A')));
+
+ $this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
+ $socket->tally();
+ $route->tally();
+ }
+ }
+
+ class TestOfHttpHeaders extends UnitTestCase {
+
+ function testParseBasicHeaders() {
+ $headers = new SimpleHttpHeaders("HTTP/1.1 200 OK\r\n" .
+ "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n" .
+ "Content-Type: text/plain\r\n" .
+ "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\n" .
+ "Connection: close");
+ $this->assertIdentical($headers->getHttpVersion(), "1.1");
+ $this->assertIdentical($headers->getResponseCode(), 200);
+ $this->assertEqual($headers->getMimeType(), "text/plain");
+ }
+
+ function testParseOfCookies() {
+ $headers = new SimpleHttpHeaders("HTTP/1.1 200 OK\r\n" .
+ "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n" .
+ "Content-Type: text/plain\r\n" .
+ "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\n" .
+ "Set-Cookie: a=aaa; expires=Wed, 25-Dec-02 04:24:20 GMT; path=/here/\r\n" .
+ "Set-Cookie: b=bbb\r\n" .
+ "Connection: close");
+ $cookies = $headers->getNewCookies();
+ $this->assertEqual(count($cookies), 2);
+ $this->assertEqual($cookies[0]->getName(), "a");
+ $this->assertEqual($cookies[0]->getValue(), "aaa");
+ $this->assertEqual($cookies[0]->getPath(), "/here/");
+ $this->assertEqual($cookies[0]->getExpiry(), "Wed, 25 Dec 2002 04:24:20 GMT");
+ $this->assertEqual($cookies[1]->getName(), "b");
+ $this->assertEqual($cookies[1]->getValue(), "bbb");
+ $this->assertEqual($cookies[1]->getPath(), "/");
+ $this->assertEqual($cookies[1]->getExpiry(), "");
+ }
+
+ function testRedirect() {
+ $headers = new SimpleHttpHeaders("HTTP/1.1 301 OK\r\n" .
+ "Content-Type: text/plain\r\n" .
+ "Content-Length: 0\r\n" .
+ "Location: http://www.somewhere-else.com/\r\n" .
+ "Connection: close");
+ $this->assertIdentical($headers->getResponseCode(), 301);
+ $this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com/");
+ $this->assertTrue($headers->isRedirect());
+ }
+
+ function testParseChallenge() {
+ $headers = new SimpleHttpHeaders("HTTP/1.1 401 Authorization required\r\n" .
+ "Content-Type: text/plain\r\n" .
+ "Connection: close\r\n" .
+ "WWW-Authenticate: Basic realm=\"Somewhere\"");
+ $this->assertEqual($headers->getAuthentication(), 'Basic');
+ $this->assertEqual($headers->getRealm(), 'Somewhere');
+ $this->assertTrue($headers->isChallenge());
+ }
+ }
+
+ class TestOfHttpResponse extends UnitTestCase {
+
+ function testBadRequest() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValue('getSent', '');
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertTrue($response->isError());
+ $this->assertWantedPattern('/Nothing fetched/', $response->getError());
+ $this->assertIdentical($response->getContent(), false);
+ $this->assertIdentical($response->getSent(), '');
+ }
+
+ function testBadSocketDuringResponse() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
+ $socket->setReturnValue("read", "");
+ $socket->setReturnValue('getSent', 'HTTP/1.1 ...');
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertTrue($response->isError());
+ $this->assertEqual($response->getContent(), '');
+ $this->assertEqual($response->getSent(), 'HTTP/1.1 ...');
+ }
+
+ function testIncompleteHeader() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
+ $socket->setReturnValueAt(2, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertTrue($response->isError());
+ $this->assertEqual($response->getContent(), "");
+ }
+
+ function testParseOfResponseHeaders() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\nDate: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
+ $socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValueAt(2, "read", "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\nConne");
+ $socket->setReturnValueAt(3, "read", "ction: close\r\n\r\nthis is a test file\n");
+ $socket->setReturnValueAt(4, "read", "with two lines in it\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertFalse($response->isError());
+ $this->assertEqual(
+ $response->getContent(),
+ "this is a test file\nwith two lines in it\n");
+ $headers = $response->getHeaders();
+ $this->assertIdentical($headers->getHttpVersion(), "1.1");
+ $this->assertIdentical($headers->getResponseCode(), 200);
+ $this->assertEqual($headers->getMimeType(), "text/plain");
+ $this->assertFalse($headers->isRedirect());
+ $this->assertFalse($headers->getLocation());
+ }
+
+ function testParseOfCookies() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
+ $socket->setReturnValueAt(2, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValueAt(3, "read", "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\n");
+ $socket->setReturnValueAt(4, "read", "Set-Cookie: a=aaa; expires=Wed, 25-Dec-02 04:24:20 GMT; path=/here/\r\n");
+ $socket->setReturnValueAt(5, "read", "Connection: close\r\n");
+ $socket->setReturnValueAt(6, "read", "\r\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $this->assertFalse($response->isError());
+ $headers = $response->getHeaders();
+ $cookies = $headers->getNewCookies();
+ $this->assertEqual($cookies[0]->getName(), "a");
+ $this->assertEqual($cookies[0]->getValue(), "aaa");
+ $this->assertEqual($cookies[0]->getPath(), "/here/");
+ $this->assertEqual($cookies[0]->getExpiry(), "Wed, 25 Dec 2002 04:24:20 GMT");
+ }
+
+ function testRedirect() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 301 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValueAt(2, "read", "Location: http://www.somewhere-else.com/\r\n");
+ $socket->setReturnValueAt(3, "read", "Connection: close\r\n");
+ $socket->setReturnValueAt(4, "read", "\r\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $headers = $response->getHeaders();
+ $this->assertTrue($headers->isRedirect());
+ $this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com/");
+ }
+
+ function testRedirectWithPort() {
+ $socket = &new MockSimpleSocket($this);
+ $socket->setReturnValueAt(0, "read", "HTTP/1.1 301 OK\r\n");
+ $socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
+ $socket->setReturnValueAt(2, "read", "Location: http://www.somewhere-else.com:80/\r\n");
+ $socket->setReturnValueAt(3, "read", "Connection: close\r\n");
+ $socket->setReturnValueAt(4, "read", "\r\n");
+ $socket->setReturnValue("read", "");
+
+ $response = &new SimpleHttpResponse($socket, 'GET', new SimpleUrl('here'));
+ $headers = $response->getHeaders();
+ $this->assertTrue($headers->isRedirect());
+ $this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com:80/");
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/live_test.php b/tests/UnitTests/simpletest/test/live_test.php
new file mode 100644
index 00000000..193284e7
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/live_test.php
@@ -0,0 +1,47 @@
+<?php
+ // $Id: live_test.php,v 1.95 2004/09/24 22:55:13 lastcraft Exp $
+ require_once(dirname(__FILE__) . '/../unit_tester.php');
+ require_once(dirname(__FILE__) . '/../socket.php');
+ require_once(dirname(__FILE__) . '/../http.php');
+ require_once(dirname(__FILE__) . '/../options.php');
+
+ if (SimpleTestOptions::getDefaultProxy()) {
+ SimpleTestOptions::ignore('LiveHttpTestCase');
+ }
+
+ class LiveHttpTestCase extends UnitTestCase {
+
+ function testBadSocket() {
+ $socket = &new SimpleSocket('bad_url', 111, 5);
+ $this->assertTrue($socket->isError());
+ $this->assertWantedPattern(
+ '/Cannot open \\[bad_url:111\\] with \\[.*?\\] within \\[5\\] seconds/',
+ $socket->getError());
+ $this->assertFalse($socket->isOpen());
+ $this->assertFalse($socket->write('A message'));
+ }
+
+ function testSocketClosure() {
+ $socket = &new SimpleSocket('www.lastcraft.com', 80, 15);
+ $this->assertTrue($socket->isOpen());
+ $this->assertTrue($socket->write("GET /test/network_confirm.php HTTP/1.0\r\n"));
+ $socket->write("Host: www.lastcraft.com\r\n");
+ $socket->write("Connection: close\r\n\r\n");
+ $this->assertEqual($socket->read(8), "HTTP/1.1");
+ $socket->close();
+ $this->assertIdentical($socket->read(8), false);
+ }
+
+ function testRecordOfSentCharacters() {
+ $socket = &new SimpleSocket('www.lastcraft.com', 80, 15);
+ $this->assertTrue($socket->write("GET /test/network_confirm.php HTTP/1.0\r\n"));
+ $socket->write("Host: www.lastcraft.com\r\n");
+ $socket->write("Connection: close\r\n\r\n");
+ $socket->close();
+ $this->assertEqual($socket->getSent(),
+ "GET /test/network_confirm.php HTTP/1.0\r\n" .
+ "Host: www.lastcraft.com\r\n" .
+ "Connection: close\r\n\r\n");
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/options_test.php b/tests/UnitTests/simpletest/test/options_test.php
new file mode 100644
index 00000000..8535f739
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/options_test.php
@@ -0,0 +1,95 @@
+<?php
+ // $Id: options_test.php,v 1.9 2005/01/13 01:31:57 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../options.php');
+
+ class TestOfOptions extends UnitTestCase {
+
+ function testMockBase() {
+ $old_class = SimpleTestOptions::getMockBaseClass();
+ SimpleTestOptions::setMockBaseClass('Fred');
+ $this->assertEqual(SimpleTestOptions::getMockBaseClass(), 'Fred');
+ SimpleTestOptions::setMockBaseClass($old_class);
+ }
+
+ function testStubBase() {
+ $old_class = SimpleTestOptions::getStubBaseClass();
+ SimpleTestOptions::setStubBaseClass('Fred');
+ $this->assertEqual(SimpleTestOptions::getStubBaseClass(), 'Fred');
+ SimpleTestOptions::setStubBaseClass($old_class);
+ }
+
+ function testIgnoreList() {
+ $this->assertFalse(SimpleTestOptions::isIgnored('ImaginaryTestCase'));
+ SimpleTestOptions::ignore('ImaginaryTestCase');
+ $this->assertTrue(SimpleTestOptions::isIgnored('ImaginaryTestCase'));
+ }
+ }
+
+ class ComparisonClass {
+ }
+
+ class ComparisonSubclass extends ComparisonClass {
+ }
+
+ class TestOfCompatibility extends UnitTestCase {
+
+ function testIsA() {
+ $this->assertTrue(SimpleTestCompatibility::isA(
+ new ComparisonClass(),
+ 'ComparisonClass'));
+ $this->assertFalse(SimpleTestCompatibility::isA(
+ new ComparisonClass(),
+ 'ComparisonSubclass'));
+ $this->assertTrue(SimpleTestCompatibility::isA(
+ new ComparisonSubclass(),
+ 'ComparisonClass'));
+ }
+
+ function testIdentityOfObjects() {
+ $object1 = new ComparisonClass();
+ $object2 = new ComparisonClass();
+ $this->assertIdentical($object1, $object2);
+ }
+
+ function testReferences () {
+ $thing = "Hello";
+ $thing_reference = &$thing;
+ $thing_copy = $thing;
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $thing,
+ $thing));
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $thing,
+ $thing_reference));
+ $this->assertFalse(SimpleTestCompatibility::isReference(
+ $thing,
+ $thing_copy));
+ }
+
+ function testObjectReferences () {
+ $object = &new ComparisonClass();
+ $object_reference = &$object;
+ $object_copy = new ComparisonClass();
+ $object_assignment = $object;
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $object,
+ $object));
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $object,
+ $object_reference));
+ $this->assertFalse(SimpleTestCompatibility::isReference(
+ $object,
+ $object_copy));
+ if (version_compare(phpversion(), '5', '>=')) {
+ $this->assertTrue(SimpleTestCompatibility::isReference(
+ $object,
+ $object_assignment));
+ } else {
+ $this->assertFalse(SimpleTestCompatibility::isReference(
+ $object,
+ $object_assignment));
+ }
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/page_test.php b/tests/UnitTests/simpletest/test/page_test.php
new file mode 100644
index 00000000..d9e87d12
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/page_test.php
@@ -0,0 +1,792 @@
+<?php
+ // $Id: page_test.php,v 1.74 2005/01/03 03:41:14 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../http.php');
+ require_once(dirname(__FILE__) . '/../page.php');
+ require_once(dirname(__FILE__) . '/../parser.php');
+
+ Mock::generate('SimpleSaxParser');
+ Mock::generate('SimplePage');
+ Mock::generate('SimpleHttpResponse');
+ Mock::generate('SimpleHttpHeaders');
+ Mock::generate('SimplePageBuilder');
+ Mock::generatePartial(
+ 'SimplePageBuilder',
+ 'PartialSimplePageBuilder',
+ array('_createPage', '_createParser'));
+
+ class TestOfPageBuilder extends UnitTestCase {
+
+ function testLink() {
+ $tag = &new SimpleAnchorTag(array('href' => 'http://somewhere'));
+ $tag->addContent('Label');
+
+ $page = &new MockSimplePage($this);
+ $page->expectArguments('acceptTag', array($tag));
+ $page->expectCallCount('acceptTag', 1);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $this->assertTrue($builder->startElement(
+ 'a',
+ array('href' => 'http://somewhere')));
+ $this->assertTrue($builder->addContent('Label'));
+ $this->assertTrue($builder->endElement('a'));
+
+ $page->tally();
+ }
+
+ function testLinkWithId() {
+ $tag = &new SimpleAnchorTag(array("href" => "http://somewhere", "id" => "44"));
+ $tag->addContent("Label");
+
+ $page = &new MockSimplePage($this);
+ $page->expectArguments("acceptTag", array($tag));
+ $page->expectCallCount("acceptTag", 1);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $this->assertTrue($builder->startElement(
+ "a",
+ array("href" => "http://somewhere", "id" => "44")));
+ $this->assertTrue($builder->addContent("Label"));
+ $this->assertTrue($builder->endElement("a"));
+
+ $page->tally();
+ }
+
+ function testLinkExtraction() {
+ $tag = &new SimpleAnchorTag(array("href" => "http://somewhere"));
+ $tag->addContent("Label");
+
+ $page = &new MockSimplePage($this);
+ $page->expectArguments("acceptTag", array($tag));
+ $page->expectCallCount("acceptTag", 1);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $this->assertTrue($builder->addContent("Starting stuff"));
+ $this->assertTrue($builder->startElement(
+ "a",
+ array("href" => "http://somewhere")));
+ $this->assertTrue($builder->addContent("Label"));
+ $this->assertTrue($builder->endElement("a"));
+ $this->assertTrue($builder->addContent("Trailing stuff"));
+
+ $page->tally();
+ }
+
+ function testMultipleLinks() {
+ $a1 = new SimpleAnchorTag(array("href" => "http://somewhere"));
+ $a1->addContent("1");
+
+ $a2 = new SimpleAnchorTag(array("href" => "http://elsewhere"));
+ $a2->addContent("2");
+
+ $page = &new MockSimplePage($this);
+ $page->expectArgumentsAt(0, "acceptTag", array($a1));
+ $page->expectArgumentsAt(1, "acceptTag", array($a2));
+ $page->expectCallCount("acceptTag", 2);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $builder->startElement("a", array("href" => "http://somewhere"));
+ $builder->addContent("1");
+ $builder->endElement("a");
+ $builder->addContent("Padding");
+ $builder->startElement("a", array("href" => "http://elsewhere"));
+ $builder->addContent("2");
+ $builder->endElement("a");
+
+ $page->tally();
+ }
+
+ function testTitle() {
+ $tag = &new SimpleTitleTag(array());
+ $tag->addContent("HereThere");
+
+ $page = &new MockSimplePage($this);
+ $page->expectArguments("acceptTag", array($tag));
+ $page->expectCallCount("acceptTag", 1);
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $builder->startElement("title", array());
+ $builder->addContent("Here");
+ $builder->addContent("There");
+ $builder->endElement("title");
+
+ $page->tally();
+ }
+
+ function testForm() {
+ $page = &new MockSimplePage($this);
+ $page->expectOnce("acceptFormStart", array(new SimpleFormTag(array())));
+ $page->expectOnce("acceptFormEnd", array());
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', new MockSimpleSaxParser($this));
+ $builder->SimplePageBuilder();
+
+ $builder->parse(new MockSimpleHttpResponse($this));
+ $builder->startElement("form", array());
+ $builder->addContent("Stuff");
+ $builder->endElement("form");
+ $page->tally();
+ }
+ }
+
+ class TestOfPageParsing extends UnitTestCase {
+
+ function testParseMechanics() {
+ $parser = &new MockSimpleSaxParser($this);
+ $parser->expectOnce('parse', array('stuff'));
+
+ $page = &new MockSimplePage($this);
+ $page->expectOnce('acceptPageEnd');
+
+ $builder = &new PartialSimplePageBuilder($this);
+ $builder->setReturnReference('_createPage', $page);
+ $builder->setReturnReference('_createParser', $parser);
+ $builder->SimplePageBuilder();
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'stuff');
+
+ $builder->parse($response);
+ $parser->tally();
+ $page->tally();
+ }
+ }
+
+ class TestOfErrorPage extends UnitTestCase {
+
+ function testInterface() {
+ $page = &new SimplePage();
+ $this->assertEqual($page->getTransportError(), 'No page fetched yet');
+ $this->assertIdentical($page->getRaw(), false);
+ $this->assertIdentical($page->getHeaders(), false);
+ $this->assertIdentical($page->getMimeType(), false);
+ $this->assertIdentical($page->getResponseCode(), false);
+ $this->assertIdentical($page->getAuthentication(), false);
+ $this->assertIdentical($page->getRealm(), false);
+ $this->assertFalse($page->hasFrames());
+ $this->assertIdentical($page->getAbsoluteUrls(), array());
+ $this->assertIdentical($page->getRelativeUrls(), array());
+ $this->assertIdentical($page->getTitle(), false);
+ }
+ }
+
+ class TestOfPageHeaders extends UnitTestCase {
+
+ function testUrlAccessor() {
+ $headers = &new MockSimpleHttpHeaders($this);
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+ $response->setReturnValue('getMethod', 'POST');
+ $response->setReturnValue('getUrl', new SimpleUrl('here'));
+ $response->setReturnValue('getRequestData', array('a' => 'A'));
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getMethod(), 'POST');
+ $this->assertEqual($page->getUrl(), new SimpleUrl('here'));
+ $this->assertEqual($page->getRequestData(), array('a' => 'A'));
+ }
+
+ function testTransportError() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getError', 'Ouch');
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getTransportError(), 'Ouch');
+ }
+
+ function testHeadersAccessor() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getRaw', 'My: Headers');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getHeaders(), 'My: Headers');
+ }
+
+ function testMimeAccessor() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getMimeType', 'text/html');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getMimeType(), 'text/html');
+ }
+
+ function testResponseAccessor() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getResponseCode', 301);
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+
+ $page = &new SimplePage($response);
+ $this->assertIdentical($page->getResponseCode(), 301);
+ }
+
+ function testAuthenticationAccessors() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getAuthentication', 'Basic');
+ $headers->setReturnValue('getRealm', 'Secret stuff');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getHeaders', $headers);
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getAuthentication(), 'Basic');
+ $this->assertEqual($page->getRealm(), 'Secret stuff');
+ }
+ }
+
+ class TestOfHtmlPage extends UnitTestCase {
+
+ function testRawAccessor() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'Raw HTML');
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getRaw(), 'Raw HTML');
+ }
+
+ function testTextAccessor() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<b>Some</b> &quot;messy&quot; HTML');
+
+ $page = &new SimplePage($response);
+ $this->assertEqual($page->getText(), 'Some "messy" HTML');
+ }
+
+ function testNoLinks() {
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $this->assertIdentical($page->getAbsoluteUrls(), array(), 'abs->%s');
+ $this->assertIdentical($page->getRelativeUrls(), array(), 'rel->%s');
+ $this->assertIdentical($page->getUrlsByLabel('Label'), array());
+ }
+
+ function testAddAbsoluteLink() {
+ $link = &new SimpleAnchorTag(array('href' => 'http://somewhere.com'));
+ $link->addContent('Label');
+
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $page->AcceptTag($link);
+
+ $this->assertEqual($page->getAbsoluteUrls(), array('http://somewhere.com'), 'abs->%s');
+ $this->assertIdentical($page->getRelativeUrls(), array(), 'rel->%s');
+ $this->assertEqual(
+ $page->getUrlsByLabel('Label'),
+ array(new SimpleUrl('http://somewhere.com')));
+ }
+
+ function testAddStrictRelativeLink() {
+ $link = &new SimpleAnchorTag(array('href' => './somewhere.php'));
+ $link->addContent('Label');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual($page->getAbsoluteUrls(), array(), 'abs->%s');
+ $this->assertIdentical($page->getRelativeUrls(), array('./somewhere.php'), 'rel->%s');
+ $this->assertEqual(
+ $page->getUrlsByLabel('Label'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ }
+
+ function testAddRelativeLink() {
+ $link = &new SimpleAnchorTag(array('href' => 'somewhere.php'));
+ $link->addContent('Label');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual($page->getAbsoluteUrls(), array(), 'abs->%s');
+ $this->assertIdentical($page->getRelativeUrls(), array('somewhere.php'), 'rel->%s');
+ $this->assertEqual(
+ $page->getUrlsByLabel('Label'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ }
+
+ function testLinkIds() {
+ $link = &new SimpleAnchorTag(array('href' => './somewhere.php', 'id' => 33));
+ $link->addContent('Label');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual(
+ $page->getUrlsByLabel('Label'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ $this->assertFalse($page->getUrlById(0));
+ $this->assertEqual(
+ $page->getUrlById(33),
+ new SimpleUrl('http://host/somewhere.php'));
+ }
+
+ function testFindLinkWithNormalisation() {
+ $link = &new SimpleAnchorTag(array('href' => './somewhere.php', 'id' => 33));
+ $link->addContent(' <em>Long &amp; thin</em> ');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual(
+ $page->getUrlsByLabel('Long & thin'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ }
+
+ function testFindLinkWithImage() {
+ $link = &new SimpleAnchorTag(array('href' => './somewhere.php', 'id' => 33));
+ $link->addContent('<img src="pic.jpg" alt="&lt;A picture&gt;">');
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &new SimplePage($response);
+ $page->AcceptTag($link);
+
+ $this->assertEqual(
+ $page->getUrlsByLabel('<A picture>'),
+ array(new SimpleUrl('http://host/somewhere.php')));
+ }
+
+ function testTitleSetting() {
+ $title = &new SimpleTitleTag(array());
+ $title->addContent('Title');
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $page->AcceptTag($title);
+ $this->assertEqual($page->getTitle(), 'Title');
+ }
+
+ function testFramesetAbsence() {
+ $url = new SimpleUrl('here');
+ $response = new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', $url);
+ $page = &new SimplePage($response);
+ $this->assertFalse($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), false);
+ }
+
+ function testHasEmptyFrameset() {
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $page->acceptFramesetStart(new SimpleTag('frameset', array()));
+ $page->acceptFramesetEnd();
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array());
+ }
+
+ function testFramesInPage() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://here'));
+
+ $page = &new SimplePage($response);
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '1.html')));
+ $page->acceptFramesetStart(new SimpleTag('frameset', array()));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '2.html')));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '3.html')));
+ $page->acceptFramesetEnd();
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '4.html')));
+
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array(
+ 1 => new SimpleUrl('http://here/2.html'),
+ 2 => new SimpleUrl('http://here/3.html')));
+ }
+
+ function testNamedFramesInPage() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://here'));
+
+ $page = &new SimplePage($response);
+ $page->acceptFramesetStart(new SimpleTag('frameset', array()));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '1.html')));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '2.html', 'name' => 'A')));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '3.html', 'name' => 'B')));
+ $page->acceptFrame(new SimpleFrameTag(array('src' => '4.html')));
+ $page->acceptFramesetEnd();
+
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array(
+ 1 => new SimpleUrl('http://here/1.html'),
+ 'A' => new SimpleUrl('http://here/2.html'),
+ 'B' => new SimpleUrl('http://here/3.html'),
+ 4 => new SimpleUrl('http://here/4.html')));
+ }
+ }
+
+ class TestOfForms extends UnitTestCase {
+
+ function testButtons() {
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $page->acceptFormStart(
+ new SimpleFormTag(array("method" => "GET", "action" => "here.php")));
+ $page->AcceptTag(
+ new SimpleSubmitTag(array("type" => "submit", "name" => "s")));
+ $page->acceptFormEnd();
+ $form = &$page->getFormBySubmitLabel("Submit");
+ $this->assertEqual(
+ $form->submitButtonByLabel("Submit"),
+ new SimpleFormEncoding(array("s" => "Submit")));
+ }
+ }
+
+ class TestOfPageScraping extends UnitTestCase {
+
+ function &parse($response) {
+ $builder = &new SimplePageBuilder();
+ return $builder->parse($response);
+ }
+
+ function testEmptyPage() {
+ $page = &new SimplePage(new MockSimpleHttpResponse($this));
+ $this->assertIdentical($page->getAbsoluteUrls(), array());
+ $this->assertIdentical($page->getRelativeUrls(), array());
+ $this->assertIdentical($page->getTitle(), false);
+ }
+
+ function testUninterestingPage() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><body><p>Stuff</p></body></html>');
+
+ $page = &$this->parse($response);
+ $this->assertIdentical($page->getAbsoluteUrls(), array());
+ $this->assertIdentical($page->getRelativeUrls(), array());
+ }
+
+ function testLinksPage() {
+ $raw = '<html>';
+ $raw .= '<a href="there.html">There</a>';
+ $raw .= '<a href="http://there.com/that.html" id="0">That page</a>';
+ $raw .= '</html>';
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', $raw);
+ $response->setReturnValue('getUrl', new SimpleUrl('http://www.here.com/a/index.html'));
+
+ $page = &$this->parse($response);
+ $this->assertIdentical(
+ $page->getAbsoluteUrls(),
+ array('http://there.com/that.html'));
+ $this->assertIdentical(
+ $page->getRelativeUrls(),
+ array('there.html'));
+ $this->assertIdentical(
+ $page->getUrlsByLabel('There'),
+ array(new SimpleUrl('http://www.here.com/a/there.html')));
+ $this->assertEqual(
+ $page->getUrlById('0'),
+ new SimpleUrl('http://there.com/that.html'));
+ }
+
+ function testTitle() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><title>Me</title></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getTitle(), 'Me');
+ }
+
+ function testNastyTitle() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '<html><head><Title> <b>Me&amp;Me </TITLE></b></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getTitle(), "Me&Me");
+ }
+
+ function testCompleteForm() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent',
+ '<html><head><form>' .
+ '<input type="text" name="here" value="Hello">' .
+ '</form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getField('here'), "Hello");
+ }
+
+ function testUnclosedForm() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent',
+ '<html><head><form>' .
+ '<input type="text" name="here" value="Hello">' .
+ '</head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getField('here'), "Hello");
+ }
+
+ function testEmptyFrameset() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '<html><frameset></frameset></html>');
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array());
+ }
+
+ function testSingleFrame() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '<html><frameset><frame src="a.html"></frameset></html>');
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical(
+ $page->getFrameset(),
+ array(1 => new SimpleUrl('http://host/a.html')));
+ }
+
+ function testSingleFrameInNestedFrameset() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent',
+ '<html><frameset><frameset>' .
+ '<frame src="a.html">' .
+ '</frameset></frameset></html>');
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical(
+ $page->getFrameset(),
+ array(1 => new SimpleUrl('http://host/a.html')));
+ }
+
+ function testFrameWithNoSource() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '<html><frameset><frame></frameset></html>');
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array());
+ }
+
+ function testFramesCollectedWithNestedFramesetTags() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent',
+ '<html><frameset>' .
+ '<frame src="a.html">' .
+ '<frameset><frame src="b.html"></frameset>' .
+ '<frame src="c.html">' .
+ '</frameset></html>');
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array(
+ 1 => new SimpleUrl('http://host/a.html'),
+ 2 => new SimpleUrl('http://host/b.html'),
+ 3 => new SimpleUrl('http://host/c.html')));
+ }
+
+ function testNamedFrames() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><frameset>' .
+ '<frame src="a.html">' .
+ '<frame name="_one" src="b.html">' .
+ '<frame src="c.html">' .
+ '<frame src="d.html" name="_two">' .
+ '</frameset></html>');
+ $response->setReturnValue('getUrl', new SimpleUrl('http://host/'));
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->hasFrames());
+ $this->assertIdentical($page->getFrameset(), array(
+ 1 => new SimpleUrl('http://host/a.html'),
+ '_one' => new SimpleUrl('http://host/b.html'),
+ 3 => new SimpleUrl('http://host/c.html'),
+ '_two' => new SimpleUrl('http://host/d.html')));
+ }
+
+ function testFindFormByLabel() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '<html><head><form><input type="submit"></form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getFormBySubmitLabel('submit'));
+ $this->assertIsA($page->getFormBySubmitName('submit'), 'SimpleForm');
+ $this->assertIsA($page->getFormBySubmitLabel('Submit'), 'SimpleForm');
+ }
+
+ function testConfirmSubmitAttributesAreCaseInsensitive() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '<html><head><FORM><INPUT TYPE="SUBMIT"></FORM></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertIsA($page->getFormBySubmitName('submit'), 'SimpleForm');
+ $this->assertIsA($page->getFormBySubmitLabel('Submit'), 'SimpleForm');
+ }
+
+ function testFindFormByImage() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><form>' .
+ '<input type="image" id=100 alt="Label" name="me">' .
+ '</form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertIsA($page->getFormByImageLabel('Label'), 'SimpleForm');
+ $this->assertIsA($page->getFormByImageName('me'), 'SimpleForm');
+ $this->assertIsA($page->getFormByImageId(100), 'SimpleForm');
+ }
+
+ function testFindFormByButtonTag() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><form>' .
+ '<button type="submit" name="b" value="B">BBB</button>' .
+ '</form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getFormBySubmitLabel('b'));
+ $this->assertNull($page->getFormBySubmitLabel('B'));
+ $this->assertIsA($page->getFormBySubmitName('b'), 'SimpleForm');
+ $this->assertIsA($page->getFormBySubmitLabel('BBB'), 'SimpleForm');
+ }
+
+ function testFindFormById() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue(
+ 'getContent',
+ '<html><head><form id="55"><input type="submit"></form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getFormById(54));
+ $this->assertIsA($page->getFormById(55), 'SimpleForm');
+ }
+
+ function testReadingTextField() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><form>' .
+ '<input type="text" name="a">' .
+ '<input type="text" name="b" value="bbb" id=3>' .
+ '</form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getField('missing'));
+ $this->assertIdentical($page->getField('a'), '');
+ $this->assertIdentical($page->getField('b'), 'bbb');
+ }
+
+ function testReadingTextFieldIsCaseInsensitive() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><FORM>' .
+ '<INPUT TYPE="TEXT" NAME="a">' .
+ '<INPUT TYPE="TEXT" NAME="b" VALUE="bbb" id=3>' .
+ '</FORM></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertNull($page->getField('missing'));
+ $this->assertIdentical($page->getField('a'), '');
+ $this->assertIdentical($page->getField('b'), 'bbb');
+ }
+
+ function testSettingTextField() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><form>' .
+ '<input type="text" name="a">' .
+ '<input type="text" name="b" id=3>' .
+ '<input type="submit">' .
+ '</form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->setField('a', 'aaa'));
+ $this->assertEqual($page->getField('a'), 'aaa');
+ $this->assertTrue($page->setFieldById(3, 'bbb'));
+ $this->assertEqual($page->getFieldById(3), 'bbb');
+ $this->assertFalse($page->setField('z', 'zzz'));
+ $this->assertNull($page->getField('z'));
+ }
+
+ function testReadingTextArea() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><form>' .
+ '<textarea name="a">aaa</textarea>' .
+ '<input type="submit">' .
+ '</form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getField('a'), 'aaa');
+ }
+
+ function testSettingTextArea() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><form>' .
+ '<textarea name="a">aaa</textarea>' .
+ '<input type="submit">' .
+ '</form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertTrue($page->setField('a', 'AAA'));
+ $this->assertEqual($page->getField('a'), 'AAA');
+ }
+
+ function testSettingSelectionField() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', '<html><head><form>' .
+ '<select name="a">' .
+ '<option>aaa</option>' .
+ '<option selected>bbb</option>' .
+ '</select>' .
+ '<input type="submit">' .
+ '</form></head></html>');
+
+ $page = &$this->parse($response);
+ $this->assertEqual($page->getField('a'), 'bbb');
+ $this->assertFalse($page->setField('a', 'ccc'));
+ $this->assertTrue($page->setField('a', 'aaa'));
+ $this->assertEqual($page->getField('a'), 'aaa');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/parse_error_test.php b/tests/UnitTests/simpletest/test/parse_error_test.php
new file mode 100644
index 00000000..dd0f4eae
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/parse_error_test.php
@@ -0,0 +1,10 @@
+<?php
+ // $Id: parse_error_test.php,v 1.1 2005/01/24 00:32:14 lastcraft Exp $
+
+ require_once('../unit_tester.php');
+ require_once('../reporter.php');
+
+ $test = &new GroupTest('This should fail');
+ $test->addTestFile('test_with_parse_error.php');
+ $test->run(new HtmlReporter());
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/parser_test.php b/tests/UnitTests/simpletest/test/parser_test.php
new file mode 100644
index 00000000..6356c343
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/parser_test.php
@@ -0,0 +1,648 @@
+<?php
+ // $Id: parser_test.php,v 1.51 2004/11/30 05:34:00 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../parser.php');
+
+ Mock::generate('SimpleSaxParser');
+
+ class TestOfParallelRegex extends UnitTestCase {
+
+ function testNoPatterns() {
+ $regex = &new ParallelRegex(false);
+ $this->assertFalse($regex->match("Hello", $match));
+ $this->assertEqual($match, "");
+ }
+
+ function testNoSubject() {
+ $regex = &new ParallelRegex(false);
+ $regex->addPattern(".*");
+ $this->assertTrue($regex->match("", $match));
+ $this->assertEqual($match, "");
+ }
+
+ function testMatchAll() {
+ $regex = &new ParallelRegex(false);
+ $regex->addPattern(".*");
+ $this->assertTrue($regex->match("Hello", $match));
+ $this->assertEqual($match, "Hello");
+ }
+
+ function testCaseSensitive() {
+ $regex = &new ParallelRegex(true);
+ $regex->addPattern("abc");
+ $this->assertTrue($regex->match("abcdef", $match));
+ $this->assertEqual($match, "abc");
+ $this->assertTrue($regex->match("AAABCabcdef", $match));
+ $this->assertEqual($match, "abc");
+ }
+
+ function testCaseInsensitive() {
+ $regex = &new ParallelRegex(false);
+ $regex->addPattern("abc");
+ $this->assertTrue($regex->match("abcdef", $match));
+ $this->assertEqual($match, "abc");
+ $this->assertTrue($regex->match("AAABCabcdef", $match));
+ $this->assertEqual($match, "ABC");
+ }
+
+ function testMatchMultiple() {
+ $regex = &new ParallelRegex(true);
+ $regex->addPattern("abc");
+ $regex->addPattern("ABC");
+ $this->assertTrue($regex->match("abcdef", $match));
+ $this->assertEqual($match, "abc");
+ $this->assertTrue($regex->match("AAABCabcdef", $match));
+ $this->assertEqual($match, "ABC");
+ $this->assertFalse($regex->match("Hello", $match));
+ }
+
+ function testPatternLabels() {
+ $regex = &new ParallelRegex(false);
+ $regex->addPattern("abc", "letter");
+ $regex->addPattern("123", "number");
+ $this->assertIdentical($regex->match("abcdef", $match), "letter");
+ $this->assertEqual($match, "abc");
+ $this->assertIdentical($regex->match("0123456789", $match), "number");
+ $this->assertEqual($match, "123");
+ }
+ }
+
+ class TestOfStateStack extends UnitTestCase {
+
+ function testStartState() {
+ $stack = &new SimpleStateStack("one");
+ $this->assertEqual($stack->getCurrent(), "one");
+ }
+
+ function testExhaustion() {
+ $stack = &new SimpleStateStack("one");
+ $this->assertFalse($stack->leave());
+ }
+
+ function testStateMoves() {
+ $stack = &new SimpleStateStack("one");
+ $stack->enter("two");
+ $this->assertEqual($stack->getCurrent(), "two");
+ $stack->enter("three");
+ $this->assertEqual($stack->getCurrent(), "three");
+ $this->assertTrue($stack->leave());
+ $this->assertEqual($stack->getCurrent(), "two");
+ $stack->enter("third");
+ $this->assertEqual($stack->getCurrent(), "third");
+ $this->assertTrue($stack->leave());
+ $this->assertTrue($stack->leave());
+ $this->assertEqual($stack->getCurrent(), "one");
+ }
+ }
+
+ class TestParser {
+
+ function accept() {
+ }
+
+ function a() {
+ }
+
+ function b() {
+ }
+ }
+ Mock::generate('TestParser');
+
+ class TestOfLexer extends UnitTestCase {
+
+ function testEmptyPage() {
+ $handler = &new MockTestParser($this);
+ $handler->expectNever("accept");
+ $handler->setReturnValue("accept", true);
+ $handler->expectNever("accept");
+ $handler->setReturnValue("accept", true);
+ $lexer = &new SimpleLexer($handler);
+ $lexer->addPattern("a+");
+ $this->assertTrue($lexer->parse(""));
+ }
+
+ function testSinglePattern() {
+ $handler = &new MockTestParser($this);
+ $handler->expectArgumentsAt(0, "accept", array("aaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "accept", array("x", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "accept", array("a", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "accept", array("yyy", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "accept", array("a", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "accept", array("x", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(6, "accept", array("aaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(7, "accept", array("z", LEXER_UNMATCHED));
+ $handler->expectCallCount("accept", 8);
+ $handler->setReturnValue("accept", true);
+ $lexer = &new SimpleLexer($handler);
+ $lexer->addPattern("a+");
+ $this->assertTrue($lexer->parse("aaaxayyyaxaaaz"));
+ $handler->tally();
+ }
+
+ function testMultiplePattern() {
+ $handler = &new MockTestParser($this);
+ $target = array("a", "b", "a", "bb", "x", "b", "a", "xxxxxx", "a", "x");
+ for ($i = 0; $i < count($target); $i++) {
+ $handler->expectArgumentsAt($i, "accept", array($target[$i], '*'));
+ }
+ $handler->expectCallCount("accept", count($target));
+ $handler->setReturnValue("accept", true);
+ $lexer = &new SimpleLexer($handler);
+ $lexer->addPattern("a+");
+ $lexer->addPattern("b+");
+ $this->assertTrue($lexer->parse("ababbxbaxxxxxxax"));
+ $handler->tally();
+ }
+ }
+
+ class TestOfLexerModes extends UnitTestCase {
+
+ function testIsolatedPattern() {
+ $handler = &new MockTestParser($this);
+ $handler->expectArgumentsAt(0, "a", array("a", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "a", array("bxb", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "a", array("aaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "a", array("x", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(6, "a", array("aaaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(7, "a", array("x", LEXER_UNMATCHED));
+ $handler->expectCallCount("a", 8);
+ $handler->setReturnValue("a", true);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addPattern("b+", "b");
+ $this->assertTrue($lexer->parse("abaabxbaaaxaaaax"));
+ $handler->tally();
+ }
+
+ function testModeChange() {
+ $handler = &new MockTestParser($this);
+ $handler->expectArgumentsAt(0, "a", array("a", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "a", array("aaa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(0, "b", array(":", LEXER_ENTER));
+ $handler->expectArgumentsAt(1, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "b", array("b", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "b", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(6, "b", array("bbb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(7, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectCallCount("a", 5);
+ $handler->expectCallCount("b", 8);
+ $handler->setReturnValue("a", true);
+ $handler->setReturnValue("b", true);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addEntryPattern(":", "a", "b");
+ $lexer->addPattern("b+", "b");
+ $this->assertTrue($lexer->parse("abaabaaa:ababbabbba"));
+ $handler->tally();
+ }
+
+ function testNesting() {
+ $handler = &new MockTestParser($this);
+ $handler->setReturnValue("a", true);
+ $handler->setReturnValue("b", true);
+ $handler->expectArgumentsAt(0, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(2, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(0, "b", array("(", LEXER_ENTER));
+ $handler->expectArgumentsAt(1, "b", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(2, "b", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(3, "b", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(4, "b", array(")", LEXER_EXIT));
+ $handler->expectArgumentsAt(4, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectCallCount("a", 6);
+ $handler->expectCallCount("b", 5);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addEntryPattern("(", "a", "b");
+ $lexer->addPattern("b+", "b");
+ $lexer->addExitPattern(")", "b");
+ $this->assertTrue($lexer->parse("aabaab(bbabb)aab"));
+ $handler->tally();
+ }
+
+ function testSingular() {
+ $handler = &new MockTestParser($this);
+ $handler->setReturnValue("a", true);
+ $handler->setReturnValue("b", true);
+ $handler->expectArgumentsAt(0, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(2, "a", array("xx", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(3, "a", array("xx", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(0, "b", array("b", LEXER_SPECIAL));
+ $handler->expectArgumentsAt(1, "b", array("bbb", LEXER_SPECIAL));
+ $handler->expectCallCount("a", 4);
+ $handler->expectCallCount("b", 2);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addSpecialPattern("b+", "a", "b");
+ $this->assertTrue($lexer->parse("aabaaxxbbbxx"));
+ $handler->tally();
+ }
+
+ function testUnwindTooFar() {
+ $handler = &new MockTestParser($this);
+ $handler->setReturnValue("a", true);
+ $handler->expectArgumentsAt(0, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array(")", LEXER_EXIT));
+ $handler->expectCallCount("a", 2);
+ $lexer = &new SimpleLexer($handler, "a");
+ $lexer->addPattern("a+", "a");
+ $lexer->addExitPattern(")", "a");
+ $this->assertFalse($lexer->parse("aa)aa"));
+ $handler->tally();
+ }
+ }
+
+ class TestOfLexerHandlers extends UnitTestCase {
+
+ function testModeMapping() {
+ $handler = &new MockTestParser($this);
+ $handler->setReturnValue("a", true);
+ $handler->expectArgumentsAt(0, "a", array("aa", LEXER_MATCHED));
+ $handler->expectArgumentsAt(1, "a", array("(", LEXER_ENTER));
+ $handler->expectArgumentsAt(2, "a", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(3, "a", array("a", LEXER_UNMATCHED));
+ $handler->expectArgumentsAt(4, "a", array("bb", LEXER_MATCHED));
+ $handler->expectArgumentsAt(5, "a", array(")", LEXER_EXIT));
+ $handler->expectArgumentsAt(6, "a", array("b", LEXER_UNMATCHED));
+ $handler->expectCallCount("a", 7);
+ $lexer = &new SimpleLexer($handler, "mode_a");
+ $lexer->addPattern("a+", "mode_a");
+ $lexer->addEntryPattern("(", "mode_a", "mode_b");
+ $lexer->addPattern("b+", "mode_b");
+ $lexer->addExitPattern(")", "mode_b");
+ $lexer->mapHandler("mode_a", "a");
+ $lexer->mapHandler("mode_b", "a");
+ $this->assertTrue($lexer->parse("aa(bbabb)b"));
+ $handler->tally();
+ }
+ }
+
+ Mock::generate("HtmlSaxParser");
+
+ class TestOfHtmlLexer extends UnitTestCase {
+ var $_handler;
+ var $_lexer;
+
+ function setUp() {
+ $this->_handler = &new MockSimpleSaxParser($this);
+ $this->_handler->setReturnValue("acceptStartToken", true);
+ $this->_handler->setReturnValue("acceptEndToken", true);
+ $this->_handler->setReturnValue("acceptAttributeToken", true);
+ $this->_handler->setReturnValue("acceptEntityToken", true);
+ $this->_handler->setReturnValue("acceptTextToken", true);
+ $this->_handler->setReturnValue("ignore", true);
+ $this->_lexer = &SimpleSaxParser::createLexer($this->_handler);
+ }
+
+ function tearDown() {
+ $this->_handler->tally();
+ }
+
+ function testUninteresting() {
+ $this->_handler->expectOnce("acceptTextToken", array("<html></html>", "*"));
+ $this->assertTrue($this->_lexer->parse("<html></html>"));
+ }
+
+ function testSkipCss() {
+ $this->_handler->expectMaximumCallCount("acceptTextToken", 0);
+ $this->_handler->expectAtLeastOnce("ignore");
+ $this->assertTrue($this->_lexer->parse("<style>Lot's of styles</style>"));
+ }
+
+ function testSkipJavaScript() {
+ $this->_handler->expectMaximumCallCount("acceptTextToken", 0);
+ $this->_handler->expectAtLeastOnce("ignore");
+ $this->assertTrue($this->_lexer->parse("<SCRIPT>Javascript code {';:^%^%£$'@\"*(}</SCRIPT>"));
+ }
+
+ function testSkipComments() {
+ $this->_handler->expectMaximumCallCount("acceptTextToken", 0);
+ $this->_handler->expectAtLeastOnce("ignore");
+ $this->assertTrue($this->_lexer->parse("<!-- <style>Lot's of styles</style> -->"));
+ }
+
+ function testTitleTag() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<title", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 2);
+ $this->_handler->expectOnce("acceptTextToken", array("Hello", "*"));
+ $this->_handler->expectOnce("acceptEndToken", array("</title>", "*"));
+ $this->assertTrue($this->_lexer->parse("<title>Hello</title>"));
+ }
+
+ function testFramesetTag() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<frameset", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 2);
+ $this->_handler->expectOnce("acceptTextToken", array("Frames", "*"));
+ $this->_handler->expectOnce("acceptEndToken", array("</frameset>", "*"));
+ $this->assertTrue($this->_lexer->parse("<frameset>Frames</frameset>"));
+ }
+
+ function testInputTag() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<input", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array("name", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array("value", "*"));
+ $this->_handler->expectArgumentsAt(3, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("=a.b.c", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("= d", "*"));
+ $this->assertTrue($this->_lexer->parse("<input name=a.b.c value = d>"));
+ }
+
+ function testEmptyLink() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<a", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 2);
+ $this->_handler->expectOnce("acceptEndToken", array("</a>", "*"));
+ $this->assertTrue($this->_lexer->parse("<html><a></a></html>"));
+ }
+
+ function testLabelledLink() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<a", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 2);
+ $this->_handler->expectOnce("acceptEndToken", array("</a>", "*"));
+ $this->_handler->expectArgumentsAt(0, "acceptTextToken", array("<html>", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptTextToken", array("label", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptTextToken", array("</html>", "*"));
+ $this->_handler->expectCallCount("acceptTextToken", 3);
+ $this->assertTrue($this->_lexer->parse("<html><a>label</a></html>"));
+ }
+
+ function testLinkAddress() {
+ $this->_handler->expectArgumentsAt(0, "acceptTextToken", array("<html>", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptTextToken", array("label", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptTextToken", array("</html>", "*"));
+ $this->_handler->expectCallCount("acceptTextToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<a", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array("href", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("= '", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("here.html", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptAttributeToken", array("'", "*"));
+ $this->_handler->expectCallCount("acceptAttributeToken", 3);
+ $this->assertTrue($this->_lexer->parse("<html><a href = 'here.html'>label</a></html>"));
+ }
+
+ function testEncodedLinkAddress() {
+ $this->_handler->expectArgumentsAt(0, "acceptTextToken", array("<html>", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptTextToken", array("label", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptTextToken", array("</html>", "*"));
+ $this->_handler->expectCallCount("acceptTextToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<a", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array("href", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("= '", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("here&amp;there.html", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptAttributeToken", array("'", "*"));
+ $this->_handler->expectCallCount("acceptAttributeToken", 3);
+ $this->assertTrue($this->_lexer->parse("<html><a href = 'here&amp;there.html'>label</a></html>"));
+ }
+
+ function testEmptyLinkWithId() {
+ $this->_handler->expectArgumentsAt(0, "acceptTextToken", array("<html>", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptTextToken", array("label", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptTextToken", array("</html>", "*"));
+ $this->_handler->expectCallCount("acceptTextToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<a", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array("id", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array(">", "*"));
+ $this->_handler->expectCallCount("acceptStartToken", 3);
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("=\"", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("0", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptAttributeToken", array("\"", "*"));
+ $this->_handler->expectCallCount("acceptAttributeToken", 3);
+ $this->assertTrue($this->_lexer->parse("<html><a id=\"0\">label</a></html>"));
+ }
+
+ function testComplexLink() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<a", LEXER_ENTER));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array("HREF", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array("bool", "*"));
+ $this->_handler->expectArgumentsAt(3, "acceptStartToken", array("Style", "*"));
+ $this->_handler->expectArgumentsAt(4, "acceptStartToken", array(">", LEXER_EXIT));
+ $this->_handler->expectCallCount("acceptStartToken", 5);
+ $this->_handler->expectArgumentsAt(0, "acceptAttributeToken", array("= '", "*"));
+ $this->_handler->expectArgumentsAt(1, "acceptAttributeToken", array("here.html", LEXER_UNMATCHED));
+ $this->_handler->expectArgumentsAt(2, "acceptAttributeToken", array("'", "*"));
+ $this->_handler->expectArgumentsAt(3, "acceptAttributeToken", array("=\"", "*"));
+ $this->_handler->expectArgumentsAt(4, "acceptAttributeToken", array("'coo", "*"));
+ $this->_handler->expectArgumentsAt(5, "acceptAttributeToken", array('\"', "*"));
+ $this->_handler->expectArgumentsAt(6, "acceptAttributeToken", array("l'", "*"));
+ $this->_handler->expectArgumentsAt(7, "acceptAttributeToken", array("\"", "*"));
+ $this->_handler->expectCallCount("acceptAttributeToken", 8);
+ $this->assertTrue($this->_lexer->parse("<HTML><a HREF = 'here.html' bool Style=\"'coo\\\"l'\">label</A></Html>"));
+ }
+
+ function testSubmit() {
+ $this->_handler->expectArgumentsAt(0, "acceptStartToken", array("<input", LEXER_ENTER));
+ $this->_handler->expectArgumentsAt(1, "acceptStartToken", array("type", "*"));
+ $this->_handler->expectArgumentsAt(2, "acceptStartToken", array("name", "*"));
+ $this->_handler->expectArgumentsAt(3, "acceptStartToken", array("value", "*"));
+ $this->_handler->expectArgumentsAt(4, "acceptStartToken", array("/", "*"));
+ $this->_handler->expectArgumentsAt(5, "acceptStartToken", array(">", LEXER_EXIT));
+ $this->_handler->expectCallCount("acceptStartToken", 6);
+ $this->assertTrue($this->_lexer->parse('<input type="submit" name="N" value="V" />'));
+ }
+
+ function testFramesParsedWithoutError() {
+ $this->assertTrue($this->_lexer->parse(
+ '<frameset><frame src="frame.html"></frameset>'));
+ $this->assertTrue($this->_lexer->parse(
+ '<frameset><frame src="frame.html"><noframes>Hello</noframes></frameset>'));
+ }
+ }
+
+ class TestOfTextExtraction extends UnitTestCase {
+
+ function testSpaceNormalisation() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise("\nOne\tTwo \nThree\t"),
+ 'One Two Three');
+ }
+
+ function testTagSuppression() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('<b>Hello</b>'),
+ 'Hello');
+ }
+
+ function testAdjoiningTagSuppression() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('<b>Hello</b><em>Goodbye</em>'),
+ 'HelloGoodbye');
+ }
+
+ function testExtractImageAltTextWithDifferentQuotes() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('<img alt="One"><img alt=\'Two\'><img alt=Three>'),
+ 'One Two Three');
+ }
+
+ function testExtractImageAltTextMultipleTimes() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('<img alt="One"><img alt="Two"><img alt="Three">'),
+ 'One Two Three');
+ }
+
+ function testHtmlEntityTranslation() {
+ $this->assertEqual(
+ SimpleSaxParser::normalise('&lt;&gt;&quot;&amp;'),
+ '<>"&');
+ }
+ }
+
+ class TestSimpleSaxParser extends SimpleSaxParser {
+ var $_lexer;
+
+ function TestSimpleSaxParser(&$listener, &$lexer) {
+ $this->_lexer = &$lexer;
+ $this->SimpleSaxParser($listener);
+ }
+
+ function &createLexer() {
+ return $this->_lexer;
+ }
+ }
+
+ Mock::generate("SimpleSaxListener");
+ Mock::generate("SimpleLexer");
+
+ class TestOfSaxGeneration extends UnitTestCase {
+ var $_listener;
+ var $_lexer;
+
+ function setUp() {
+ $this->_listener = &new MockSimpleSaxListener($this);
+ $this->_lexer = &new MockSimpleLexer($this);
+ $this->_parser = &new TestSimpleSaxParser($this->_listener, $this->_lexer);
+ }
+
+ function tearDown() {
+ $this->_listener->tally();
+ $this->_lexer->tally();
+ }
+
+ function testLexerFailure() {
+ $this->_lexer->setReturnValue("parse", false);
+ $this->assertFalse($this->_parser->parse("<html></html>"));
+ }
+
+ function testLexerSuccess() {
+ $this->_lexer->setReturnValue("parse", true);
+ $this->assertTrue($this->_parser->parse("<html></html>"));
+ }
+
+ function testSimpleLinkStart() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("startElement", array("a", array()));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("<a", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testSimpleTitleStart() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("startElement", array("title", array()));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("<title", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testLinkStart() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("startElement", array("a", array("href" => "here.html")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("<a", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptStartToken("href", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("=\"", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptAttributeToken("here.html", LEXER_UNMATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("\"", LEXER_EXIT));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testLinkStartWithEncodedUrl() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce(
+ "startElement",
+ array("a", array("href" => "here&there.html")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("<a", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptStartToken("href", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("=\"", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptAttributeToken("here&amp;there.html", LEXER_UNMATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("\"", LEXER_EXIT));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testLinkStartWithId() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce(
+ "startElement",
+ array("a", array("id" => "0")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("<a", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptStartToken("id", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("= \"", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptAttributeToken("0", LEXER_UNMATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("\"", LEXER_EXIT));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testLinkEnd() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("endElement", array("a"));
+ $this->_listener->setReturnValue("endElement", true);
+ $this->assertTrue($this->_parser->acceptEndToken("</a>", LEXER_SPECIAL));
+ }
+
+ function testInput() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce(
+ "startElement",
+ array("input", array("name" => "a")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("<input", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptStartToken("name", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("= a", LEXER_SPECIAL));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testButton() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce(
+ "startElement",
+ array("button", array("name" => "a")));
+ $this->_listener->setReturnValue("startElement", true);
+ $this->assertTrue($this->_parser->acceptStartToken("<button", LEXER_ENTER));
+ $this->assertTrue($this->_parser->acceptStartToken("name", LEXER_MATCHED));
+ $this->assertTrue($this->_parser->acceptAttributeToken("= a", LEXER_SPECIAL));
+ $this->assertTrue($this->_parser->acceptStartToken(">", LEXER_EXIT));
+ }
+
+ function testContent() {
+ $this->_parser->parse("");
+ $this->_listener->expectOnce("addContent", array("stuff"));
+ $this->_listener->setReturnValue("addContent", true);
+ $this->assertTrue($this->_parser->acceptTextToken("stuff", LEXER_UNMATCHED));
+ }
+
+ function testIgnore() {
+ $this->_parser->parse("");
+ $this->_listener->expectNever("addContent");
+ $this->assertTrue($this->_parser->ignore("stuff", LEXER_UNMATCHED));
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/real_sites_test.php b/tests/UnitTests/simpletest/test/real_sites_test.php
new file mode 100644
index 00000000..728b6e95
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/real_sites_test.php
@@ -0,0 +1,29 @@
+<?php
+ // $Id: real_sites_test.php,v 1.16 2004/12/05 21:12:33 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../web_tester.php');
+
+ class LiveSitesTestCase extends WebTestCase {
+
+ function testLastCraft() {
+ $this->assertTrue($this->get('http://www.lastcraft.com'));
+ $this->assertResponse(array(200));
+ $this->assertMime(array('text/html'));
+ $this->clickLink('About');
+ $this->assertTitle('About Last Craft');
+ }
+
+ function testSourceforge() {
+ $this->assertTrue($this->get('http://sourceforge.net/'));
+ $this->setField('words', 'simpletest');
+ $this->assertTrue($this->clickImageByName('imageField'));
+ $this->assertTitle('SourceForge.net: Search');
+ $this->assertTrue($this->clickLink('SimpleTest'));
+ $this->clickLink('statistics');
+ $this->assertWantedPattern('/Statistics for the past 7 days/');
+ $this->assertTrue($this->setField('report', 'Monthly'));
+ $this->clickSubmit('Change Stats View');
+ $this->assertWantedPattern('/Statistics for the past \d+ months/');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/remote_test.php b/tests/UnitTests/simpletest/test/remote_test.php
new file mode 100644
index 00000000..16c82b31
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/remote_test.php
@@ -0,0 +1,17 @@
+<?php
+ // $Id: remote_test.php,v 1.4 2004/04/07 19:12:13 lastcraft Exp $
+ require_once('../remote.php');
+ require_once('../reporter.php');
+
+ // The following URL will depend on your own installation.
+ $base_url = 'http://uno/simple/';
+
+ $test = &new GroupTest('Remote tests');
+ $test->addTestCase(new RemoteTestCase(
+ $base_url . 'test/visual_test.php?xml=yes',
+ $base_url . 'test/visual_test.php?xml=yes&dry=yes'));
+ if (SimpleReporter::inCli()) {
+ exit ($test->run(new XmlReporter()) ? 0 : 1);
+ }
+ $test->run(new HtmlReporter());
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/shell_test.php b/tests/UnitTests/simpletest/test/shell_test.php
new file mode 100644
index 00000000..7d5631a3
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/shell_test.php
@@ -0,0 +1,38 @@
+<?php
+ // $Id: shell_test.php,v 1.8 2004/09/24 22:55:18 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../shell_tester.php');
+
+ class TestOfShell extends UnitTestCase {
+
+ function testEcho() {
+ $shell = &new SimpleShell();
+ $this->assertIdentical($shell->execute('echo Hello'), 0);
+ $this->assertWantedPattern('/Hello/', $shell->getOutput());
+ }
+
+ function testBadCommand() {
+ $shell = &new SimpleShell();
+ $this->assertNotEqual($ret = $shell->execute('blurgh! 2>&1'), 0);
+ }
+ }
+
+ class TestOfShellTesterAndShell extends ShellTestCase {
+
+ function testEcho() {
+ $this->assertTrue($this->execute('echo Hello'));
+ $this->assertExitCode(0);
+ $this->assertoutput('Hello');
+ }
+
+ function testFileExistence() {
+ $this->assertFileExists(dirname(__FILE__) . '/all_tests.php');
+ $this->assertFileNotExists('wibble');
+ }
+
+ function testFilePatterns() {
+ $this->assertFilePattern('/all_tests/i', dirname(__FILE__) . '/all_tests.php');
+ $this->assertNoFilePattern('/sputnik/i', dirname(__FILE__) . '/all_tests.php');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/shell_tester_test.php b/tests/UnitTests/simpletest/test/shell_tester_test.php
new file mode 100644
index 00000000..5a8f5b59
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/shell_tester_test.php
@@ -0,0 +1,37 @@
+<?php
+ // $Id: shell_tester_test.php,v 1.5 2004/09/24 22:55:18 lastcraft Exp $
+
+ Mock::generate('SimpleShell');
+
+ class TestOfShellTestCase extends ShellTestCase {
+ var $_mock_shell = false;
+
+ function &_getShell() {
+ return $this->_mock_shell;
+ }
+
+ function testExitCode() {
+ $this->_mock_shell = &new MockSimpleShell($this);
+ $this->_mock_shell->setReturnValue('execute', 0);
+ $this->_mock_shell->expectOnce('execute', array('ls'));
+ $this->assertTrue($this->execute('ls'));
+ $this->assertExitCode(0);
+ $this->_mock_shell->tally();
+ }
+
+ function testOutput() {
+ $this->_mock_shell = &new MockSimpleShell($this);
+ $this->_mock_shell->setReturnValue('execute', 0);
+ $this->_mock_shell->setReturnValue('getOutput', "Line 1\nLine 2\n");
+ $this->assertOutput("Line 1\nLine 2\n");
+ }
+
+ function testOutputPatterns() {
+ $this->_mock_shell = &new MockSimpleShell($this);
+ $this->_mock_shell->setReturnValue('execute', 0);
+ $this->_mock_shell->setReturnValue('getOutput', "Line 1\nLine 2\n");
+ $this->assertOutputPattern('/line/i');
+ $this->assertNoOutputPattern('/line 2/');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/simple_mock_test.php b/tests/UnitTests/simpletest/test/simple_mock_test.php
new file mode 100644
index 00000000..cdae14f9
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/simple_mock_test.php
@@ -0,0 +1,648 @@
+<?php
+ // $Id: simple_mock_test.php,v 1.41 2005/01/23 22:20:52 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../expectation.php');
+
+ class TestOfWildcardExpectation extends UnitTestCase {
+
+ function testSimpleInteger() {
+ $expectation = new WildcardExpectation();
+ $this->assertTrue($expectation->test(33));
+ $this->assertWantedPattern(
+ '/matches.*33/i',
+ $expectation->testMessage(33));
+ }
+ }
+
+ class TestOfParametersExpectation extends UnitTestCase {
+
+ function testEmptyMatch() {
+ $expectation = new ParametersExpectation(array());
+ $this->assertTrue($expectation->test(array()));
+ $this->assertFalse($expectation->test(array(33)));
+ }
+
+ function testSingleMatch() {
+ $expectation = new ParametersExpectation(array(0));
+ $this->assertFalse($expectation->test(array(1)));
+ $this->assertTrue($expectation->test(array(0)));
+ }
+
+ function testAnyMatch() {
+ $expectation = new ParametersExpectation(false);
+ $this->assertTrue($expectation->test(array()));
+ $this->assertTrue($expectation->test(array(1, 2)));
+ }
+
+ function testMissingParameter() {
+ $expectation = new ParametersExpectation(array(0));
+ $this->assertFalse($expectation->test(array()));
+ }
+
+ function testNullParameter() {
+ $expectation = new ParametersExpectation(array(null));
+ $this->assertTrue($expectation->test(array(null)));
+ $this->assertFalse($expectation->test(array()));
+ }
+
+ function testWildcardExpectations() {
+ $expectation = new ParametersExpectation(array(new WildcardExpectation()));
+ $this->assertFalse($expectation->test(array()));
+ $this->assertIdentical($expectation->test(array(null)), true);
+ $this->assertIdentical($expectation->test(array(13)), true);
+ }
+
+ function testOtherExpectations() {
+ $expectation = new ParametersExpectation(
+ array(new WantedPatternExpectation('/hello/i')));
+ $this->assertFalse($expectation->test(array('Goodbye')));
+ $this->assertTrue($expectation->test(array('hello')));
+ $this->assertTrue($expectation->test(array('Hello')));
+ }
+
+ function testIdentityOnly() {
+ $expectation = new ParametersExpectation(array("0"));
+ $this->assertFalse($expectation->test(array(0)));
+ $this->assertTrue($expectation->test(array("0")));
+ }
+
+ function testLongList() {
+ $expectation = new ParametersExpectation(
+ array("0", 0, new WildcardExpectation(), false));
+ $this->assertTrue($expectation->test(array("0", 0, 37, false)));
+ $this->assertFalse($expectation->test(array("0", 0, 37, true)));
+ $this->assertFalse($expectation->test(array("0", 0, 37)));
+ }
+ }
+
+ class TestOfCallMap extends UnitTestCase {
+
+ function testEmpty() {
+ $map = new CallMap();
+ $this->assertFalse($map->isMatch("any", array()));
+ $this->assertNull($map->findFirstMatch("any", array()));
+ }
+
+ function testExactValue() {
+ $map = new CallMap();
+ $map->addValue(array(0), "Fred");
+ $map->addValue(array(1), "Jim");
+ $map->addValue(array("1"), "Tom");
+ $this->assertTrue($map->isMatch(array(0)));
+ $this->assertEqual($map->findFirstMatch(array(0)), "Fred");
+ $this->assertTrue($map->isMatch(array(1)));
+ $this->assertEqual($map->findFirstMatch(array(1)), "Jim");
+ $this->assertEqual($map->findFirstMatch(array("1")), "Tom");
+ }
+
+ function testExactReference() {
+ $map = new CallMap();
+ $ref = "Fred";
+ $map->addReference(array(0), $ref);
+ $this->assertEqual($map->findFirstMatch(array(0)), "Fred");
+ $ref2 = &$map->findFirstMatch(array(0));
+ $this->assertReference($ref2, $ref);
+ }
+
+ function testWildcard() {
+ $map = new CallMap();
+ $map->addValue(array(new WildcardExpectation(), 1, 3), "Fred");
+ $this->assertTrue($map->isMatch(array(2, 1, 3)));
+ $this->assertEqual($map->findFirstMatch(array(2, 1, 3)), "Fred");
+ }
+
+ function testAllWildcard() {
+ $map = new CallMap();
+ $this->assertFalse($map->isMatch(array(2, 1, 3)));
+ $map->addValue("", "Fred");
+ $this->assertTrue($map->isMatch(array(2, 1, 3)));
+ $this->assertEqual($map->findFirstMatch(array(2, 1, 3)), "Fred");
+ }
+
+ function testOrdering() {
+ $map = new CallMap();
+ $map->addValue(array(1, 2), "1, 2");
+ $map->addValue(array(1, 3), "1, 3");
+ $map->addValue(array(1), "1");
+ $map->addValue(array(1, 4), "1, 4");
+ $map->addValue(array(new WildcardExpectation()), "Any");
+ $map->addValue(array(2), "2");
+ $map->addValue("", "Default");
+ $map->addValue(array(), "None");
+ $this->assertEqual($map->findFirstMatch(array(1, 2)), "1, 2");
+ $this->assertEqual($map->findFirstMatch(array(1, 3)), "1, 3");
+ $this->assertEqual($map->findFirstMatch(array(1, 4)), "1, 4");
+ $this->assertEqual($map->findFirstMatch(array(1)), "1");
+ $this->assertEqual($map->findFirstMatch(array(2)), "Any");
+ $this->assertEqual($map->findFirstMatch(array(3)), "Any");
+ $this->assertEqual($map->findFirstMatch(array()), "Default");
+ }
+ }
+
+ class Dummy {
+ function Dummy() {
+ }
+
+ function aMethod($parameter) {
+ return $parameter;
+ }
+
+ function anotherMethod() {
+ return true;
+ }
+ }
+
+ Stub::generate("Dummy");
+ Stub::generate("Dummy", "AnotherStubDummy");
+ Stub::generate("Dummy", "StubDummyWithExtraMethods", array("extraMethod"));
+
+ class SpecialSimpleStub extends SimpleStub {
+ function SpecialSimpleStub($wildcard) {
+ $this->SimpleStub($wildcard);
+ }
+ }
+ SimpleTestOptions::setStubBaseClass("SpecialSimpleStub");
+ Stub::generate("Dummy", "SpecialStubDummy");
+ SimpleTestOptions::setStubBaseClass("SimpleStub");
+
+ class TestOfStubGeneration extends UnitTestCase {
+
+ function testCloning() {
+ $stub = &new StubDummy();
+ $this->assertTrue(method_exists($stub, "aMethod"));
+ $this->assertNull($stub->aMethod());
+ }
+
+ function testCloningWithExtraMethod() {
+ $stub = &new StubDummyWithExtraMethods();
+ $this->assertTrue(method_exists($stub, "extraMethod"));
+ }
+
+ function testCloningWithChosenClassName() {
+ $stub = &new AnotherStubDummy();
+ $this->assertTrue(method_exists($stub, "aMethod"));
+ }
+
+ function testCloningWithDifferentBaseClass() {
+ $stub = &new SpecialStubDummy();
+ $this->assertIsA($stub, "SpecialSimpleStub");
+ $this->assertTrue(method_exists($stub, "aMethod"));
+ }
+ }
+
+ class TestOfServerStubReturns extends UnitTestCase {
+
+ function testDefaultReturn() {
+ $stub = &new StubDummy();
+ $stub->setReturnValue("aMethod", "aaa");
+ $this->assertIdentical($stub->aMethod(), "aaa");
+ $this->assertIdentical($stub->aMethod(), "aaa");
+ }
+
+ function testParameteredReturn() {
+ $stub = &new StubDummy();
+ $stub->setReturnValue("aMethod", "aaa", array(1, 2, 3));
+ $this->assertNull($stub->aMethod());
+ $this->assertIdentical($stub->aMethod(1, 2, 3), "aaa");
+ }
+
+ function testReferenceReturned() {
+ $stub = &new StubDummy();
+ $object = new Dummy();
+ $stub->setReturnReference("aMethod", $object, array(1, 2, 3));
+ $this->assertReference($stub->aMethod(1, 2, 3), $object);
+ }
+
+ function testWildcardReturn() {
+ $stub = &new StubDummy("wild");
+ $stub->setReturnValue("aMethod", "aaa", array(1, "wild", 3));
+ $this->assertIdentical($stub->aMethod(1, "something", 3), "aaa");
+ $this->assertIdentical($stub->aMethod(1, "anything", 3), "aaa");
+ }
+
+ function testAllWildcardReturn() {
+ $stub = &new StubDummy("wild");
+ $stub->setReturnValue("aMethod", "aaa");
+ $this->assertIdentical($stub->aMethod(1, 2, 3), "aaa");
+ $this->assertIdentical($stub->aMethod(), "aaa");
+ }
+
+ function testCallCount() {
+ $stub = &new StubDummy();
+ $this->assertEqual($stub->getCallCount("aMethod"), 0);
+ $stub->aMethod();
+ $this->assertEqual($stub->getCallCount("aMethod"), 1);
+ $stub->aMethod();
+ $this->assertEqual($stub->getCallCount("aMethod"), 2);
+ }
+
+ function testMultipleMethods() {
+ $stub = &new StubDummy();
+ $stub->setReturnValue("aMethod", 100, array(1));
+ $stub->setReturnValue("aMethod", 200, array(2));
+ $stub->setReturnValue("anotherMethod", 10, array(1));
+ $stub->setReturnValue("anotherMethod", 20, array(2));
+ $this->assertIdentical($stub->aMethod(1), 100);
+ $this->assertIdentical($stub->anotherMethod(1), 10);
+ $this->assertIdentical($stub->aMethod(2), 200);
+ $this->assertIdentical($stub->anotherMethod(2), 20);
+ }
+
+ function testReturnSequence() {
+ $stub = &new StubDummy();
+ $stub->setReturnValueAt(0, "aMethod", "aaa");
+ $stub->setReturnValueAt(1, "aMethod", "bbb");
+ $stub->setReturnValueAt(3, "aMethod", "ddd");
+ $this->assertIdentical($stub->aMethod(), "aaa");
+ $this->assertIdentical($stub->aMethod(), "bbb");
+ $this->assertNull($stub->aMethod());
+ $this->assertIdentical($stub->aMethod(), "ddd");
+ }
+
+ function testReturnReferenceSequence() {
+ $stub = &new StubDummy();
+ $object = new Dummy();
+ $stub->setReturnReferenceAt(1, "aMethod", $object);
+ $this->assertNull($stub->aMethod());
+ $this->assertReference($stub->aMethod(), $object);
+ $this->assertNull($stub->aMethod());
+ }
+
+ function testComplicatedReturnSequence() {
+ $stub = &new StubDummy("wild");
+ $object = new Dummy();
+ $stub->setReturnValueAt(1, "aMethod", "aaa", array("a"));
+ $stub->setReturnValueAt(1, "aMethod", "bbb");
+ $stub->setReturnReferenceAt(2, "aMethod", $object, array("wild", 2));
+ $stub->setReturnValueAt(2, "aMethod", "value", array("wild", 3));
+ $stub->setReturnValue("aMethod", 3, array(3));
+ $this->assertNull($stub->aMethod());
+ $this->assertEqual($stub->aMethod("a"), "aaa");
+ $this->assertReference($stub->aMethod(1, 2), $object);
+ $this->assertEqual($stub->aMethod(3), 3);
+ $this->assertNull($stub->aMethod());
+ }
+
+ function testMultipleMethodSequences() {
+ $stub = &new StubDummy();
+ $stub->setReturnValueAt(0, "aMethod", "aaa");
+ $stub->setReturnValueAt(1, "aMethod", "bbb");
+ $stub->setReturnValueAt(0, "anotherMethod", "ccc");
+ $stub->setReturnValueAt(1, "anotherMethod", "ddd");
+ $this->assertIdentical($stub->aMethod(), "aaa");
+ $this->assertIdentical($stub->anotherMethod(), "ccc");
+ $this->assertIdentical($stub->aMethod(), "bbb");
+ $this->assertIdentical($stub->anotherMethod(), "ddd");
+ }
+
+ function testSequenceFallback() {
+ $stub = &new StubDummy();
+ $stub->setReturnValueAt(0, "aMethod", "aaa", array('a'));
+ $stub->setReturnValueAt(1, "aMethod", "bbb", array('a'));
+ $stub->setReturnValue("aMethod", "AAA");
+ $this->assertIdentical($stub->aMethod('a'), "aaa");
+ $this->assertIdentical($stub->aMethod('b'), "AAA");
+ }
+
+ function testMethodInterference() {
+ $stub = &new StubDummy();
+ $stub->setReturnValueAt(0, "anotherMethod", "aaa");
+ $stub->setReturnValue("aMethod", "AAA");
+ $this->assertIdentical($stub->aMethod(), "AAA");
+ $this->assertIdentical($stub->anotherMethod(), "aaa");
+ }
+ }
+
+ Mock::generate("Dummy");
+ Mock::generate("Dummy", "AnotherMockDummy");
+ Mock::generate("Dummy", "MockDummyWithExtraMethods", array("extraMethod"));
+
+ class SpecialSimpleMock extends SimpleMock {
+ function SpecialSimpleMock(&$test, $wildcard) {
+ $this->SimpleMock($test, $wildcard);
+ }
+ }
+ SimpleTestOptions::setMockBaseClass("SpecialSimpleMock");
+ Mock::generate("Dummy", "SpecialMockDummy");
+ SimpleTestOptions::setMockBaseClass("SimpleMock");
+
+ class TestOfMockGeneration extends UnitTestCase {
+
+ function testCloning() {
+ $mock = &new MockDummy($this);
+ $this->assertTrue(method_exists($mock, "aMethod"));
+ $this->assertNull($mock->aMethod());
+ }
+
+ function testCloningWithExtraMethod() {
+ $mock = &new MockDummyWithExtraMethods($this);
+ $this->assertTrue(method_exists($mock, "extraMethod"));
+ }
+
+ function testCloningWithChosenClassName() {
+ $mock = &new AnotherMockDummy($this);
+ $this->assertTrue(method_exists($mock, "aMethod"));
+ }
+
+ function testCloningWithDifferentBaseClass() {
+ $mock = &new SpecialMockDummy($this);
+ $this->assertIsA($mock, "SpecialSimpleMock");
+ $this->assertTrue(method_exists($mock, "aMethod"));
+ }
+ }
+
+ class TestOfMockReturns extends UnitTestCase {
+
+ function testNoUnitTesterSetThrowsError() {
+ $mock = &new MockDummy();
+ $this->assertErrorPattern('/missing argument/i');
+ $this->assertErrorPattern('/no unit tester/i');
+ }
+
+ function testParameteredReturn() {
+ $mock = &new MockDummy($this);
+ $mock->setReturnValue("aMethod", "aaa", array(1, 2, 3));
+ $this->assertNull($mock->aMethod());
+ $this->assertIdentical($mock->aMethod(1, 2, 3), "aaa");
+ }
+
+ function testReferenceReturned() {
+ $mock = &new MockDummy($this);
+ $object = new Dummy();
+ $mock->setReturnReference("aMethod", $object, array(1, 2, 3));
+ $this->assertReference($mock->aMethod(1, 2, 3), $object);
+ }
+
+ function testWildcardReturn() {
+ $mock = &new MockDummy($this, "wild");
+ $mock->setReturnValue("aMethod", "aaa", array(1, "wild", 3));
+ $this->assertIdentical($mock->aMethod(1, "something", 3), "aaa");
+ $this->assertIdentical($mock->aMethod(1, "anything", 3), "aaa");
+ }
+
+ function testPatternMatchReturn() {
+ $mock = &new MockDummy($this);
+ $mock->setReturnValue(
+ "aMethod",
+ "aaa",
+ array(new wantedPatternExpectation('/hello/i')));
+ $this->assertIdentical($mock->aMethod('Hello'), "aaa");
+ $this->assertNull($mock->aMethod('Goodbye'));
+ }
+
+ function testCallCount() {
+ $mock = &new MockDummy($this);
+ $this->assertEqual($mock->getCallCount("aMethod"), 0);
+ $mock->aMethod();
+ $this->assertEqual($mock->getCallCount("aMethod"), 1);
+ $mock->aMethod();
+ $this->assertEqual($mock->getCallCount("aMethod"), 2);
+ }
+
+ function testReturnReferenceSequence() {
+ $mock = &new MockDummy($this);
+ $object = new Dummy();
+ $mock->setReturnReferenceAt(1, "aMethod", $object);
+ $this->assertNull($mock->aMethod());
+ $this->assertReference($mock->aMethod(), $object);
+ $this->assertNull($mock->aMethod());
+ $this->swallowErrors();
+ }
+ }
+
+ Mock::generate("SimpleTestCase");
+
+ class TestOfMockTally extends UnitTestCase {
+
+ function testZeroCallCount() {
+ $mock = &new MockDummy($this);
+ $mock->expectCallCount("aMethod", 0);
+ $mock->tally();
+ }
+
+ function testExpectedCallCount() {
+ $mock = &new MockDummy($this);
+ $mock->expectCallCount("aMethod", 2);
+ $mock->aMethod();
+ $mock->aMethod();
+ $mock->tally();
+ }
+ }
+
+ class TestOfMockExpectations extends UnitTestCase {
+ var $_test;
+
+ function TestOfMockExpectations() {
+ $this->UnitTestCase();
+ }
+
+ function setUp() {
+ $this->_test = &new MockSimpleTestCase($this);
+ }
+
+ function tearDown() {
+ $this->_test->tally();
+ }
+
+ function testSettingExpectationOnNonMethodThrowsError() {
+ $mock = &new MockDummy($this);
+ $mock->expectMaximumCallCount("aMissingMethod", 2);
+ $this->assertError();
+ }
+
+ function testMaxCallsDetectsOverrun() {
+ $this->_test->expectOnce("assertTrue", array(false, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectMaximumCallCount("aMethod", 2);
+ $mock->aMethod();
+ $mock->aMethod();
+ $mock->aMethod();
+ }
+
+ function testTallyOnMaxCallsSendsPassOnUnderrun() {
+ $this->_test->expectOnce("assertTrue", array(true, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectMaximumCallCount("aMethod", 2);
+ $mock->aMethod();
+ $mock->aMethod();
+ $mock->tally();
+ }
+
+ function testExpectNeverDetectsOverrun() {
+ $this->_test->expectOnce("assertTrue", array(false, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectNever("aMethod");
+ $mock->aMethod();
+ }
+
+ function testTallyOnExpectNeverSendsPassOnUnderrun() {
+ $this->_test->expectOnce("assertTrue", array(true, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectNever("aMethod");
+ $mock->tally();
+ }
+
+ function testMinCalls() {
+ $this->_test->expectOnce("assertTrue", array(true, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectMinimumCallCount("aMethod", 2);
+ $mock->aMethod();
+ $mock->aMethod();
+ $mock->tally();
+ }
+
+ function testFailedNever() {
+ $this->_test->expectOnce("assertTrue", array(false, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectNever("aMethod");
+ $mock->aMethod();
+ }
+
+ function testUnderOnce() {
+ $this->_test->expectOnce("assertTrue", array(false, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectOnce("aMethod");
+ $mock->tally();
+ }
+
+ function testOverOnce() {
+ $this->_test->expectOnce("assertTrue", array(false, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectOnce("aMethod");
+ $mock->aMethod();
+ $mock->aMethod();
+ $mock->tally();
+ $this->swallowErrors();
+ }
+
+ function testUnderAtLeastOnce() {
+ $this->_test->expectOnce("assertTrue", array(false, '*'));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectAtLeastOnce("aMethod");
+ $mock->tally();
+ }
+
+ function testZeroArguments() {
+ $mock = &new MockDummy($this);
+ $mock->expectArguments("aMethod", array());
+ $mock->aMethod();
+ }
+
+ function testExpectedArguments() {
+ $mock = &new MockDummy($this);
+ $mock->expectArguments("aMethod", array(1, 2, 3));
+ $mock->aMethod(1, 2, 3);
+ }
+
+ function testFailedArguments() {
+ $this->_test->expectOnce("assertTrue", array(false, "*"));
+ $mock = &new MockDummy($this->_test);
+ $mock->expectArguments("aMethod", array("this"));
+ $mock->aMethod("that");
+ }
+
+ function testWildcardArguments() {
+ $mock = &new MockDummy($this, "wild");
+ $mock->expectArguments("aMethod", array("wild", 123, "wild"));
+ $mock->aMethod(100, 123, 101);
+ }
+
+ function testSpecificSequence() {
+ $mock = &new MockDummy($this);
+ $mock->expectArgumentsAt(1, "aMethod", array(1, 2, 3));
+ $mock->expectArgumentsAt(2, "aMethod", array("Hello"));
+ $mock->aMethod();
+ $mock->aMethod(1, 2, 3);
+ $mock->aMethod("Hello");
+ $mock->aMethod();
+ }
+
+ function testFailedSequence() {
+ $this->_test->expectArguments("assertTrue", array(false, "*"));
+ $this->_test->expectCallCount("assertTrue", 2);
+ $mock = &new MockDummy($this->_test);
+ $mock->expectArgumentsAt(0, "aMethod", array(1, 2, 3));
+ $mock->expectArgumentsAt(1, "aMethod", array("Hello"));
+ $mock->aMethod(1, 2);
+ $mock->aMethod("Goodbye");
+ }
+
+ function testBadArgParameter() {
+ $mock = &new MockDummy($this);
+ $mock->expectArguments("aMethod", "foo");
+ $this->assertErrorPattern('/\$args.*not an array/i');
+ $mock->aMethod();
+ $mock->tally();
+ }
+ }
+
+ class TestOfMockComparisons extends UnitTestCase {
+
+ function testTestCaseRegistry() {
+ $test = &new MockSimpleTestCase($this);
+ $class = SimpleMock::registerTest($test);
+ $this->assertReference($test, SimpleMock::injectTest($class));
+ }
+
+ function testEqualComparisonOfMocksDoesNotCrash() {
+ $expectation = &new EqualExpectation(new MockDummy($this));
+ $this->assertTrue($expectation->test(new MockDummy($this), true));
+ }
+
+ function testIdenticalComparisonOfMocksDoesNotCrash() {
+ $expectation = &new IdenticalExpectation(new MockDummy($this));
+ $this->assertTrue($expectation->test(new MockDummy($this)));
+ }
+ }
+
+ SimpleTestOptions::addPartialMockCode('function sayHello() { return "Hello"; }');
+ Mock::generatePartial("Dummy", "TestDummy", array("anotherMethod"));
+ SimpleTestOptions::addPartialMockCode();
+
+ class TestOfPartialMocks extends UnitTestCase {
+
+ function testMethodReplacement() {
+ $mock = &new TestDummy($this);
+ $this->assertEqual($mock->aMethod(99), 99);
+ $this->assertNull($mock->anotherMethod());
+ }
+
+ function testSettingReturns() {
+ $mock = &new TestDummy($this);
+ $mock->setReturnValue("anotherMethod", 33, array(3));
+ $mock->setReturnValue("anotherMethod", 22);
+ $mock->setReturnValueAt(2, "anotherMethod", 44, array(3));
+ $this->assertEqual($mock->anotherMethod(), 22);
+ $this->assertEqual($mock->anotherMethod(3), 33);
+ $this->assertEqual($mock->anotherMethod(3), 44);
+ }
+
+ function testReferences() {
+ $mock = &new TestDummy($this);
+ $object = new Dummy();
+ $mock->setReturnReferenceAt(0, "anotherMethod", $object, array(3));
+ $this->assertReference($mock->anotherMethod(3), $object);
+ }
+
+ function testExpectations() {
+ $mock = &new TestDummy($this);
+ $mock->expectCallCount("anotherMethod", 2);
+ $mock->expectArguments("anotherMethod", array(77));
+ $mock->expectArgumentsAt(1, "anotherMethod", array(66));
+ $mock->anotherMethod(77);
+ $mock->anotherMethod(66);
+ $mock->tally();
+ }
+
+ function testAdditionalPartialMockCode() {
+ $dummy = &new TestDummy($this);
+ $this->assertEqual($dummy->sayHello(), 'Hello');
+ }
+
+ function testSettingExpectationOnMissingMethodThrowsError() {
+ $mock = &new TestDummy($this);
+ $mock->expectCallCount("aMissingMethod", 2);
+ $this->assertError();
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/socket_test.php b/tests/UnitTests/simpletest/test/socket_test.php
new file mode 100644
index 00000000..a872d8c5
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/socket_test.php
@@ -0,0 +1,26 @@
+<?php
+ // $Id: socket_test.php,v 1.16 2004/09/24 22:55:19 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../socket.php');
+
+ Mock::generate('SimpleSocket');
+
+ class TestOfSimpleStickyError extends UnitTestCase {
+
+ function testSettingError() {
+ $error = new SimpleStickyError();
+ $this->assertFalse($error->isError());
+ $error->_setError('Ouch');
+ $this->assertTrue($error->isError());
+ $this->assertEqual($error->getError(), 'Ouch');
+ }
+
+ function testClearingError() {
+ $error = new SimpleStickyError();
+ $error->_setError('Ouch');
+ $this->assertTrue($error->isError());
+ $error->_clearError();
+ $this->assertFalse($error->isError());
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/tag_test.php b/tests/UnitTests/simpletest/test/tag_test.php
new file mode 100644
index 00000000..c18788af
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/tag_test.php
@@ -0,0 +1,462 @@
+<?php
+ // $Id: tag_test.php,v 1.52 2005/02/10 02:44:24 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../tag.php');
+
+ class TestOfTag extends UnitTestCase {
+
+ function testStartValuesWithoutAdditionalContent() {
+ $tag = new SimpleTitleTag(array('a' => '1', 'b' => ''));
+ $this->assertEqual($tag->getTagName(), 'title');
+ $this->assertIdentical($tag->getAttribute('a'), '1');
+ $this->assertIdentical($tag->getAttribute('b'), true);
+ $this->assertIdentical($tag->getAttribute('c'), false);
+ $this->assertIdentical($tag->getContent(), '');
+ }
+
+ function testTitleContent() {
+ $tag = &new SimpleTitleTag(array());
+ $this->assertTrue($tag->expectEndTag());
+ $tag->addContent('Hello');
+ $tag->addContent('World');
+ $this->assertEqual($tag->getText(), 'HelloWorld');
+ }
+
+ function testMessyTitleContent() {
+ $tag = &new SimpleTitleTag(array());
+ $this->assertTrue($tag->expectEndTag());
+ $tag->addContent('<b>Hello</b>');
+ $tag->addContent('<em>World</em>');
+ $this->assertEqual($tag->getText(), 'HelloWorld');
+ }
+
+ function testTagWithNoEnd() {
+ $tag = &new SimpleTextTag(array());
+ $this->assertFalse($tag->expectEndTag());
+ }
+
+ function testAnchorHref() {
+ $tag = &new SimpleAnchorTag(array('href' => 'http://here/'));
+ $this->assertEqual($tag->getHref(), 'http://here/');
+
+ $tag = &new SimpleAnchorTag(array('href' => ''));
+ $this->assertIdentical($tag->getAttribute('href'), true);
+ $this->assertIdentical($tag->getHref(), '');
+
+ $tag = &new SimpleAnchorTag(array());
+ $this->assertIdentical($tag->getAttribute('href'), false);
+ $this->assertIdentical($tag->getHref(), '');
+ }
+
+ function testIsIdMatchesIdAttribute() {
+ $tag = &new SimpleAnchorTag(array('href' => 'http://here/', 'id' => 7));
+ $this->assertIdentical($tag->getAttribute('id'), '7');
+ $this->assertTrue($tag->isId(7));
+ }
+ }
+
+ class TestOfWidget extends UnitTestCase {
+
+ function testTextEmptyDefault() {
+ $tag = &new SimpleTextTag(array('' => 'text'));
+ $this->assertIdentical($tag->getDefault(), '');
+ $this->assertIdentical($tag->getValue(), '');
+ }
+
+ function testTextDefault() {
+ $tag = &new SimpleTextTag(array('value' => 'aaa'));
+ $this->assertEqual($tag->getDefault(), 'aaa');
+ $this->assertEqual($tag->getValue(), 'aaa');
+ }
+
+ function testSettingTextValue() {
+ $tag = &new SimpleTextTag(array('value' => 'aaa'));
+ $tag->setValue('bbb');
+ $this->assertEqual($tag->getValue(), 'bbb');
+ $tag->resetValue();
+ $this->assertEqual($tag->getValue(), 'aaa');
+ }
+
+ function testFailToSetHiddenValue() {
+ $tag = &new SimpleTextTag(array('value' => 'aaa', 'type' => 'hidden'));
+ $this->assertFalse($tag->setValue('bbb'));
+ $this->assertEqual($tag->getValue(), 'aaa');
+ }
+
+ function testSubmitDefaults() {
+ $tag = &new SimpleSubmitTag(array('type' => 'submit'));
+ $this->assertEqual($tag->getName(), 'submit');
+ $this->assertEqual($tag->getValue(), 'Submit');
+ $this->assertFalse($tag->setValue('Cannot set this'));
+ $this->assertEqual($tag->getValue(), 'Submit');
+ $this->assertEqual($tag->getLabel(), 'Submit');
+ $this->assertEqual($tag->getSubmitValues(), array('submit' => 'Submit'));
+ }
+
+ function testPopulatedSubmit() {
+ $tag = &new SimpleSubmitTag(
+ array('type' => 'submit', 'name' => 's', 'value' => 'Ok!'));
+ $this->assertEqual($tag->getName(), 's');
+ $this->assertEqual($tag->getValue(), 'Ok!');
+ $this->assertEqual($tag->getLabel(), 'Ok!');
+ $this->assertEqual($tag->getSubmitValues(), array('s' => 'Ok!'));
+ }
+
+ function testImageSubmit() {
+ $tag = &new SimpleImageSubmitTag(
+ array('type' => 'image', 'name' => 's', 'alt' => 'Label'));
+ $this->assertEqual($tag->getName(), 's');
+ $this->assertEqual($tag->getLabel(), 'Label');
+ $this->assertEqual(
+ $tag->getSubmitValues(20, 30),
+ array('s.x' => 20, 's.y' => 30));
+ }
+
+ function testImageSubmitTitlePreferredOverAltForLabel() {
+ $tag = &new SimpleImageSubmitTag(
+ array('type' => 'image', 'name' => 's', 'alt' => 'Label', 'title' => 'Title'));
+ $this->assertEqual($tag->getLabel(), 'Title');
+ }
+
+ function testButton() {
+ $tag = &new SimpleButtonTag(
+ array('type' => 'submit', 'name' => 's', 'value' => 'do'));
+ $tag->addContent('I am a button');
+ $this->assertEqual($tag->getName(), 's');
+ $this->assertEqual($tag->getValue(), 'do');
+ $this->assertEqual($tag->getLabel(), 'I am a button');
+ $this->assertEqual($tag->getSubmitValues(), array('s' => 'do'));
+ }
+ }
+
+ class TestOfTextArea extends UnitTestCase {
+
+ function testDefault() {
+ $tag = &new SimpleTextAreaTag(array('name' => 'a'));
+ $tag->addContent('Some text');
+ $this->assertEqual($tag->getName(), 'a');
+ $this->assertEqual($tag->getDefault(), 'Some text');
+ }
+
+ function testWrapping() {
+ $tag = &new SimpleTextAreaTag(array('cols' => '10', 'wrap' => 'physical'));
+ $tag->addContent("Lot's of text that should be wrapped");
+ $this->assertEqual(
+ $tag->getDefault(),
+ "Lot's of\ntext that\nshould be\nwrapped");
+ $tag->setValue("New long text\nwith two lines");
+ $this->assertEqual(
+ $tag->getValue(),
+ "New long\ntext\nwith two\nlines");
+ }
+ }
+
+ class TestOfSelection extends UnitTestCase {
+
+ function testEmpty() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $this->assertIdentical($tag->getValue(), '');
+ }
+
+ function testSingle() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $option = &new SimpleOptionTag(array());
+ $option->addContent('AAA');
+ $tag->addTag($option);
+ $this->assertEqual($tag->getValue(), 'AAA');
+ }
+
+ function testSingleDefault() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $option = &new SimpleOptionTag(array('selected' => ''));
+ $option->addContent('AAA');
+ $tag->addTag($option);
+ $this->assertEqual($tag->getValue(), 'AAA');
+ }
+
+ function testSingleMappedDefault() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $option = &new SimpleOptionTag(array('selected' => '', 'value' => 'aaa'));
+ $option->addContent('AAA');
+ $tag->addTag($option);
+ $this->assertEqual($tag->getValue(), 'aaa');
+ }
+
+ function testStartsWithDefault() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $a = &new SimpleOptionTag(array());
+ $a->addContent('AAA');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array('selected' => ''));
+ $b->addContent('BBB');
+ $tag->addTag($b);
+ $c = &new SimpleOptionTag(array());
+ $c->addContent('CCC');
+ $tag->addTag($c);
+ $this->assertEqual($tag->getValue(), 'BBB');
+ }
+
+ function testSettingOption() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $a = &new SimpleOptionTag(array());
+ $a->addContent('AAA');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array('selected' => ''));
+ $b->addContent('BBB');
+ $tag->addTag($b);
+ $c = &new SimpleOptionTag(array());
+ $c->addContent('CCC');
+ $tag->setValue('AAA');
+ $this->assertEqual($tag->getValue(), 'AAA');
+ }
+
+ function testSettingMappedOption() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $a = &new SimpleOptionTag(array('value' => 'aaa'));
+ $a->addContent('AAA');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array('value' => 'bbb', 'selected' => ''));
+ $b->addContent('BBB');
+ $tag->addTag($b);
+ $c = &new SimpleOptionTag(array('value' => 'ccc'));
+ $c->addContent('CCC');
+ $tag->addTag($c);
+ $tag->setValue('AAA');
+ $this->assertEqual($tag->getValue(), 'aaa');
+ }
+
+ function testSelectionDespiteSpuriousWhitespace() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $a = &new SimpleOptionTag(array());
+ $a->addContent(' AAA ');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array('selected' => ''));
+ $b->addContent(' BBB ');
+ $tag->addTag($b);
+ $c = &new SimpleOptionTag(array());
+ $c->addContent(' CCC ');
+ $tag->addTag($c);
+ $this->assertEqual($tag->getValue(), ' BBB ');
+ $tag->setValue('AAA');
+ $this->assertEqual($tag->getValue(), ' AAA ');
+ }
+
+ function testFailToSetIllegalOption() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $a = &new SimpleOptionTag(array());
+ $a->addContent('AAA');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array('selected' => ''));
+ $b->addContent('BBB');
+ $tag->addTag($b);
+ $c = &new SimpleOptionTag(array());
+ $c->addContent('CCC');
+ $tag->addTag($c);
+ $this->assertFalse($tag->setValue('Not present'));
+ $this->assertEqual($tag->getValue(), 'BBB');
+ }
+
+ function testNastyOptionValuesThatLookLikeFalse() {
+ $tag = &new SimpleSelectionTag(array('name' => 'a'));
+ $a = &new SimpleOptionTag(array('value' => '1'));
+ $a->addContent('One');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array('value' => '0'));
+ $b->addContent('Zero');
+ $tag->addTag($b);
+ $this->assertIdentical($tag->getValue(), '1');
+ $tag->setValue('Zero');
+ $this->assertIdentical($tag->getValue(), '0');
+ }
+
+ function testBlankOption() {
+ $tag = &new SimpleSelectionTag(array('name' => 'A'));
+ $a = &new SimpleOptionTag(array());
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array());
+ $b->addContent('b');
+ $tag->addTag($b);
+ $this->assertIdentical($tag->getValue(), '');
+ $tag->setValue('b');
+ $this->assertIdentical($tag->getValue(), 'b');
+ $tag->setValue('');
+ $this->assertIdentical($tag->getValue(), '');
+ }
+
+ function testMultipleDefaultWithNoSelections() {
+ $tag = &new MultipleSelectionTag(array('name' => 'a', 'multiple' => ''));
+ $a = &new SimpleOptionTag(array());
+ $a->addContent('AAA');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array());
+ $b->addContent('BBB');
+ $tag->addTag($b);
+ $this->assertIdentical($tag->getDefault(), array());
+ $this->assertIdentical($tag->getValue(), array());
+ }
+
+ function testMultipleDefaultWithSelections() {
+ $tag = &new MultipleSelectionTag(array('name' => 'a', 'multiple' => ''));
+ $a = &new SimpleOptionTag(array('selected' => ''));
+ $a->addContent('AAA');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array('selected' => ''));
+ $b->addContent('BBB');
+ $tag->addTag($b);
+ $this->assertIdentical($tag->getDefault(), array('AAA', 'BBB'));
+ $this->assertIdentical($tag->getValue(), array('AAA', 'BBB'));
+ }
+
+ function testSettingMultiple() {
+ $tag = &new MultipleSelectionTag(array('name' => 'a', 'multiple' => ''));
+ $a = &new SimpleOptionTag(array('selected' => ''));
+ $a->addContent('AAA');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array());
+ $b->addContent('BBB');
+ $tag->addTag($b);
+ $c = &new SimpleOptionTag(array('selected' => ''));
+ $c->addContent('CCC');
+ $tag->addTag($c);
+ $this->assertIdentical($tag->getDefault(), array('AAA', 'CCC'));
+ $this->assertTrue($tag->setValue(array('BBB', 'CCC')));
+ $this->assertIdentical($tag->getValue(), array('BBB', 'CCC'));
+ $this->assertTrue($tag->setValue(array()));
+ $this->assertIdentical($tag->getValue(), array());
+ }
+
+ function testFailToSetIllegalOptionsInMultiple() {
+ $tag = &new MultipleSelectionTag(array('name' => 'a', 'multiple' => ''));
+ $a = &new SimpleOptionTag(array('selected' => ''));
+ $a->addContent('AAA');
+ $tag->addTag($a);
+ $b = &new SimpleOptionTag(array());
+ $b->addContent('BBB');
+ $tag->addTag($b);
+ $this->assertFalse($tag->setValue(array('CCC')));
+ $this->assertTrue($tag->setValue(array('AAA', 'BBB')));
+ $this->assertFalse($tag->setValue(array('AAA', 'CCC')));
+ }
+ }
+
+ class TestOfRadioGroup extends UnitTestCase {
+
+ function testEmptyGroup() {
+ $group = &new SimpleRadioGroup();
+ $this->assertIdentical($group->getDefault(), false);
+ $this->assertIdentical($group->getValue(), false);
+ $this->assertFalse($group->setValue('a'));
+ }
+
+ function testReadingSingleButtonGroup() {
+ $group = &new SimpleRadioGroup();
+ $group->addWidget(new SimpleRadioButtonTag(
+ array('value' => 'A', 'checked' => '')));
+ $this->assertIdentical($group->getDefault(), 'A');
+ $this->assertIdentical($group->getValue(), 'A');
+ }
+
+ function testReadingMultipleButtonGroup() {
+ $group = &new SimpleRadioGroup();
+ $group->addWidget(new SimpleRadioButtonTag(
+ array('value' => 'A')));
+ $group->addWidget(new SimpleRadioButtonTag(
+ array('value' => 'B', 'checked' => '')));
+ $this->assertIdentical($group->getDefault(), 'B');
+ $this->assertIdentical($group->getValue(), 'B');
+ }
+
+ function testFailToSetUnlistedValue() {
+ $group = &new SimpleRadioGroup();
+ $group->addWidget(new SimpleRadioButtonTag(array('value' => 'z')));
+ $this->assertFalse($group->setValue('a'));
+ $this->assertIdentical($group->getValue(), false);
+ }
+
+ function testSettingNewValueClearsTheOldOne() {
+ $group = &new SimpleRadioGroup();
+ $group->addWidget(new SimpleRadioButtonTag(
+ array('value' => 'A')));
+ $group->addWidget(new SimpleRadioButtonTag(
+ array('value' => 'B', 'checked' => '')));
+ $this->assertTrue($group->setValue('A'));
+ $this->assertIdentical($group->getValue(), 'A');
+ }
+
+ function testIsIdMatchesAnyWidgetInSet() {
+ $group = &new SimpleRadioGroup();
+ $group->addWidget(new SimpleRadioButtonTag(
+ array('value' => 'A', 'id' => 'i1')));
+ $group->addWidget(new SimpleRadioButtonTag(
+ array('value' => 'B', 'id' => 'i2')));
+ $this->assertFalse($group->isId('i0'));
+ $this->assertTrue($group->isId('i1'));
+ $this->assertTrue($group->isId('i2'));
+ }
+ }
+
+ class TestOfTagGroup extends UnitTestCase {
+
+ function testReadingMultipleCheckboxGroup() {
+ $group = &new SimpleCheckboxGroup();
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
+ $group->addWidget(new SimpleCheckboxTag(
+ array('value' => 'B', 'checked' => '')));
+ $this->assertIdentical($group->getDefault(), 'B');
+ $this->assertIdentical($group->getValue(), 'B');
+ }
+
+ function testReadingMultipleUncheckedItems() {
+ $group = &new SimpleCheckboxGroup();
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'B')));
+ $this->assertIdentical($group->getDefault(), false);
+ $this->assertIdentical($group->getValue(), false);
+ }
+
+ function testReadingMultipleCheckedItems() {
+ $group = &new SimpleCheckboxGroup();
+ $group->addWidget(new SimpleCheckboxTag(
+ array('value' => 'A', 'checked' => '')));
+ $group->addWidget(new SimpleCheckboxTag(
+ array('value' => 'B', 'checked' => '')));
+ $this->assertIdentical($group->getDefault(), array('A', 'B'));
+ $this->assertIdentical($group->getValue(), array('A', 'B'));
+ }
+
+ function testSettingSingleValue() {
+ $group = &new SimpleCheckboxGroup();
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'B')));
+ $this->assertTrue($group->setValue('A'));
+ $this->assertIdentical($group->getValue(), 'A');
+ $this->assertTrue($group->setValue('B'));
+ $this->assertIdentical($group->getValue(), 'B');
+ }
+
+ function testSettingMultipleValues() {
+ $group = &new SimpleCheckboxGroup();
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'B')));
+ $this->assertTrue($group->setValue(array('A', 'B')));
+ $this->assertIdentical($group->getValue(), array('A', 'B'));
+ }
+
+ function testSettingNoValue() {
+ $group = &new SimpleCheckboxGroup();
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'A')));
+ $group->addWidget(new SimpleCheckboxTag(array('value' => 'B')));
+ $this->assertTrue($group->setValue(false));
+ $this->assertIdentical($group->getValue(), false);
+ }
+
+ function testIsIdMatchesAnyIdInSet() {
+ $group = &new SimpleCheckboxGroup();
+ $group->addWidget(new SimpleCheckboxTag(array('id' => 1, 'value' => 'A')));
+ $group->addWidget(new SimpleCheckboxTag(array('id' => 2, 'value' => 'B')));
+ $this->assertFalse($group->isId(0));
+ $this->assertTrue($group->isId(1));
+ $this->assertTrue($group->isId(2));
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/test_with_parse_error.php b/tests/UnitTests/simpletest/test/test_with_parse_error.php
new file mode 100644
index 00000000..affdb751
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/test_with_parse_error.php
@@ -0,0 +1,8 @@
+<?php
+ // $Id: test_with_parse_error.php,v 1.1 2005/01/24 00:32:14 lastcraft Exp $
+
+ class TestCaseWithParseError extends UnitTestCase {
+ wibble
+ }
+
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/unit_tester_test.php b/tests/UnitTests/simpletest/test/unit_tester_test.php
new file mode 100644
index 00000000..a6a61f8d
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/unit_tester_test.php
@@ -0,0 +1,52 @@
+<?php
+ // $Id: unit_tester_test.php,v 1.3 2005/02/18 22:40:58 lastcraft Exp $
+
+ class TestOfUnitTester extends UnitTestCase {
+
+ function testAssertTrueReturnsAssertionAsBoolean() {
+ $this->assertTrue($this->assertTrue(true));
+ }
+
+ function testAssertFalseReturnsAssertionAsBoolean() {
+ $this->assertTrue($this->assertFalse(false));
+ }
+
+ function testAssertEqualReturnsAssertionAsBoolean() {
+ $this->assertTrue($this->assertEqual(5, 5));
+ }
+
+ function testAssertIdenticalReturnsAssertionAsBoolean() {
+ $this->assertTrue($this->assertIdentical(5, 5));
+ }
+
+ function testCoreAssertionsDoNotThrowErrors() {
+ $this->assertIsA($this, 'UnitTestCase');
+ $this->assertNotA($this, 'WebTestCase');
+ }
+ }
+
+ class JBehaveStyleRunner extends SimpleRunner {
+ function JBehaveStyleRunner(&$test_case, &$scorer) {
+ $this->SimpleRunner($test_case, $scorer);
+ }
+
+ function _isTest($method) {
+ return strtolower(substr($method, 0, 6)) == 'should';
+ }
+ }
+
+ class TestOfJBehaveStyleRunner extends UnitTestCase {
+
+ function &_createRunner(&$reporter) {
+ return new JBehaveStyleRunner($this, $reporter);
+ }
+
+ function testFail() {
+ $this->fail('This should not be run');
+ }
+
+ function shouldBeRun() {
+ $this->pass('This should be run');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/unit_tests.php b/tests/UnitTests/simpletest/test/unit_tests.php
new file mode 100644
index 00000000..ebc38ffe
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/unit_tests.php
@@ -0,0 +1,49 @@
+<?php
+ // $Id: unit_tests.php,v 1.47 2005/02/05 04:51:31 lastcraft Exp $
+ if (! defined('TEST')) {
+ define('TEST', __FILE__);
+ }
+ require_once('../unit_tester.php');
+ require_once('../web_tester.php');
+ require_once('../shell_tester.php');
+ require_once('../reporter.php');
+ require_once('../mock_objects.php');
+ require_once('../extensions/pear_test_case.php');
+ require_once('../extensions/phpunit_test_case.php');
+
+ class UnitTests extends GroupTest {
+ function UnitTests() {
+ $this->GroupTest('Unit tests');
+ $this->addTestFile('errors_test.php');
+ $this->addTestFile('options_test.php');
+ $this->addTestFile('dumper_test.php');
+ $this->addTestFile('expectation_test.php');
+ $this->addTestFile('unit_tester_test.php');
+ $this->addTestFile('simple_mock_test.php');
+ $this->addTestFile('adapter_test.php');
+ $this->addTestFile('socket_test.php');
+ $this->addTestFile('encoding_test.php');
+ $this->addTestFile('url_test.php');
+ $this->addTestFile('http_test.php');
+ $this->addTestFile('authentication_test.php');
+ $this->addTestFile('user_agent_test.php');
+ $this->addTestFile('parser_test.php');
+ $this->addTestFile('tag_test.php');
+ $this->addTestFile('form_test.php');
+ $this->addTestFile('page_test.php');
+ $this->addTestFile('frames_test.php');
+ $this->addTestFile('browser_test.php');
+ $this->addTestFile('web_tester_test.php');
+ $this->addTestFile('shell_tester_test.php');
+ $this->addTestFile('xml_test.php');
+ }
+ }
+
+ if (TEST == __FILE__) {
+ $test = &new UnitTests();
+ if (SimpleReporter::inCli()) {
+ exit ($test->run(new TextReporter()) ? 0 : 1);
+ }
+ $test->run(new HtmlReporter());
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/url_test.php b/tests/UnitTests/simpletest/test/url_test.php
new file mode 100644
index 00000000..5ca01260
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/url_test.php
@@ -0,0 +1,369 @@
+<?php
+ // $Id: url_test.php,v 1.19 2005/01/02 23:43:28 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../url.php');
+
+ class TestOfUrl extends UnitTestCase {
+
+ function testDefaultUrl() {
+ $url = new SimpleUrl('');
+ $this->assertEqual($url->getScheme(), '');
+ $this->assertEqual($url->getHost(), '');
+ $this->assertEqual($url->getScheme('http'), 'http');
+ $this->assertEqual($url->getHost('localhost'), 'localhost');
+ $this->assertEqual($url->getPath(), '');
+ }
+
+ function testBasicParsing() {
+ $url = new SimpleUrl('https://www.lastcraft.com/test/');
+ $this->assertEqual($url->getScheme(), 'https');
+ $this->assertEqual($url->getHost(), 'www.lastcraft.com');
+ $this->assertEqual($url->getPath(), '/test/');
+ }
+
+ function testRelativeUrls() {
+ $url = new SimpleUrl('../somewhere.php');
+ $this->assertEqual($url->getScheme(), false);
+ $this->assertEqual($url->getHost(), false);
+ $this->assertEqual($url->getPath(), '../somewhere.php');
+ }
+
+ function testParseBareParameter() {
+ $url = new SimpleUrl('?a');
+ $this->assertEqual($url->getPath(), '');
+ $this->assertEqual($url->getEncodedRequest(), '?a=');
+ }
+
+ function testParseEmptyParameter() {
+ $url = new SimpleUrl('?a=');
+ $this->assertEqual($url->getPath(), '');
+ $this->assertEqual($url->getEncodedRequest(), '?a=');
+ }
+
+ function testParseParameterPair() {
+ $url = new SimpleUrl('?a=A');
+ $this->assertEqual($url->getPath(), '');
+ $this->assertEqual($url->getEncodedRequest(), '?a=A');
+ }
+
+ function testParseMultipleParameters() {
+ $url = new SimpleUrl('?a=A&b=B');
+ $this->assertEqual($url->getEncodedRequest(), '?a=A&b=B');
+ }
+
+ function testParsingParameterMixture() {
+ $url = new SimpleUrl('?a=A&b=&c');
+ $this->assertEqual($url->getEncodedRequest(), '?a=A&b=&c=');
+ }
+
+ function testAddParameters() {
+ $url = new SimpleUrl('');
+ $url->addRequestParameter('a', 'A');
+ $this->assertEqual($url->getEncodedRequest(), '?a=A');
+ $url->addRequestParameter('b', 'B');
+ $this->assertEqual($url->getEncodedRequest(), '?a=A&b=B');
+ $url->addRequestParameter('a', 'aaa');
+ $this->assertEqual($url->getEncodedRequest(), '?a=A&a=aaa&b=B');
+ }
+
+ function testClearingParameters() {
+ $url = new SimpleUrl('');
+ $url->addRequestParameter('a', 'A');
+ $url->clearRequest();
+ $this->assertIdentical($url->getEncodedRequest(), '');
+ }
+
+ function testEncodingParameters() {
+ $url = new SimpleUrl('');
+ $url->addRequestParameter('a', '?!"\'#~@[]{}:;<>,./|£$%^&*()_+-=');
+ $this->assertIdentical(
+ $request = $url->getEncodedRequest(),
+ '?a=%3F%21%22%27%23%7E%40%5B%5D%7B%7D%3A%3B%3C%3E%2C.%2F%7C%A3%24%25%5E%26%2A%28%29_%2B-%3D');
+ }
+
+ function testDecodingParameters() {
+ $url = new SimpleUrl('?a=%3F%21%22%27%23%7E%40%5B%5D%7B%7D%3A%3B%3C%3E%2C.%2F%7C%A3%24%25%5E%26%2A%28%29_%2B-%3D');
+ $this->assertEqual(
+ $url->getEncodedRequest(),
+ '?a=' . urlencode('?!"\'#~@[]{}:;<>,./|£$%^&*()_+-='));
+ }
+
+ function testSettingCordinates() {
+ $url = new SimpleUrl('');
+ $url->setCoordinates('32', '45');
+ $this->assertIdentical($url->getX(), 32);
+ $this->assertIdentical($url->getY(), 45);
+ $this->assertEqual($url->getEncodedRequest(), '?32,45');
+ }
+
+ function testParseCordinates() {
+ $url = new SimpleUrl('?32,45');
+ $this->assertIdentical($url->getX(), 32);
+ $this->assertIdentical($url->getY(), 45);
+ $this->assertEqual($url->getEncodedRequest(), '?32,45');
+ }
+
+ function testClearingCordinates() {
+ $url = new SimpleUrl('?32,45');
+ $url->setCoordinates();
+ $this->assertIdentical($url->getX(), false);
+ $this->assertIdentical($url->getY(), false);
+ }
+
+ function testParsingParameterCordinateMixture() {
+ $url = new SimpleUrl('?a=A&b=&c?32,45');
+ $this->assertIdentical($url->getX(), 32);
+ $this->assertIdentical($url->getY(), 45);
+ $this->assertEqual($url->getEncodedRequest(), '?a=A&b=&c=?32,45');
+ }
+
+ function testParsingParameterWithBadCordinates() {
+ $url = new SimpleUrl('?a=A&b=&c?32');
+ $this->assertIdentical($url->getX(), false);
+ $this->assertIdentical($url->getY(), false);
+ $this->assertEqual($url->getEncodedRequest(), '?a=A&b=&c?32=');
+ }
+
+ function testPageSplitting() {
+ $url = new SimpleUrl('./here/../there/somewhere.php');
+ $this->assertEqual($url->getPath(), './here/../there/somewhere.php');
+ $this->assertEqual($url->getPage(), 'somewhere.php');
+ $this->assertEqual($url->getBasePath(), './here/../there/');
+ }
+
+ function testAbsolutePathPageSplitting() {
+ $url = new SimpleUrl("http://host.com/here/there/somewhere.php");
+ $this->assertEqual($url->getPath(), "/here/there/somewhere.php");
+ $this->assertEqual($url->getPage(), "somewhere.php");
+ $this->assertEqual($url->getBasePath(), "/here/there/");
+ }
+
+ function testSplittingUrlWithNoPageGivesEmptyPage() {
+ $url = new SimpleUrl('/here/there/');
+ $this->assertEqual($url->getPath(), '/here/there/');
+ $this->assertEqual($url->getPage(), '');
+ $this->assertEqual($url->getBasePath(), '/here/there/');
+ }
+
+ function testPathNormalisation() {
+ $this->assertEqual(
+ SimpleUrl::normalisePath('https://host.com/I/am/here/../there/somewhere.php'),
+ 'https://host.com/I/am/there/somewhere.php');
+ }
+
+ function testUsernameAndPasswordAreUrlDecoded() {
+ $url = new SimpleUrl('http://' . urlencode('test@test') .
+ ':' . urlencode('$!£@*&%') . '@www.lastcraft.com');
+ $this->assertEqual($url->getUsername(), 'test@test');
+ $this->assertEqual($url->getPassword(), '$!£@*&%');
+ }
+
+ function testBlitz() {
+ $this->assertUrl(
+ "https://username:password@www.somewhere.com:243/this/that/here.php?a=1&b=2#anchor",
+ array("https", "username", "password", "www.somewhere.com", 243, "/this/that/here.php", "com", "?a=1&b=2", "anchor"),
+ array("a" => "1", "b" => "2"));
+ $this->assertUrl(
+ "username:password@www.somewhere.com/this/that/here.php?a=1",
+ array(false, "username", "password", "www.somewhere.com", false, "/this/that/here.php", "com", "?a=1", false),
+ array("a" => "1"));
+ $this->assertUrl(
+ "username:password@somewhere.com:243?1,2",
+ array(false, "username", "password", "somewhere.com", 243, "/", "com", "?1,2", false),
+ array(),
+ array(1, 2));
+ $this->assertUrl(
+ "https://www.somewhere.com",
+ array("https", false, false, "www.somewhere.com", false, "/", "com", "", false));
+ $this->assertUrl(
+ "username@www.somewhere.com:243#anchor",
+ array(false, "username", false, "www.somewhere.com", 243, "/", "com", "", "anchor"));
+ $this->assertUrl(
+ "/this/that/here.php?a=1&b=2?3,4",
+ array(false, false, false, false, false, "/this/that/here.php", false, "?a=1&b=2?3,4", false),
+ array("a" => "1", "b" => "2"),
+ array(3, 4));
+ $this->assertUrl(
+ "username@/here.php?a=1&b=2",
+ array(false, "username", false, false, false, "/here.php", false, "?a=1&b=2", false),
+ array("a" => "1", "b" => "2"));
+ }
+
+ function testAmbiguousHosts() {
+ $this->assertUrl(
+ "tigger",
+ array(false, false, false, false, false, "tigger", false, "", false));
+ $this->assertUrl(
+ "/tigger",
+ array(false, false, false, false, false, "/tigger", false, "", false));
+ $this->assertUrl(
+ "//tigger",
+ array(false, false, false, "tigger", false, "/", false, "", false));
+ $this->assertUrl(
+ "//tigger/",
+ array(false, false, false, "tigger", false, "/", false, "", false));
+ $this->assertUrl(
+ "tigger.com",
+ array(false, false, false, "tigger.com", false, "/", "com", "", false));
+ $this->assertUrl(
+ "me.net/tigger",
+ array(false, false, false, "me.net", false, "/tigger", "net", "", false));
+ }
+
+ function testAsString() {
+ $this->assertPreserved('https://www.here.com');
+ $this->assertPreserved('http://me:secret@www.here.com');
+ $this->assertPreserved('http://here/there');
+ $this->assertPreserved('http://here/there?a=A&b=B');
+ $this->assertPreserved('http://here/there?a=1&a=2');
+ $this->assertPreserved('http://here/there?a=1&a=2?9,8');
+ $this->assertPreserved('http://host?a=1&a=2');
+ $this->assertPreserved('http://host#stuff');
+ $this->assertPreserved('http://me:secret@www.here.com/a/b/c/here.html?a=A?7,6');
+ }
+
+ function assertUrl($raw, $parts, $params = false, $coords = false) {
+ if (! is_array($params)) {
+ $params = array();
+ }
+ $url = new SimpleUrl($raw);
+ $this->assertIdentical($url->getScheme(), $parts[0], "[$raw] scheme -> %s");
+ $this->assertIdentical($url->getUsername(), $parts[1], "[$raw] username -> %s");
+ $this->assertIdentical($url->getPassword(), $parts[2], "[$raw] password -> %s");
+ $this->assertIdentical($url->getHost(), $parts[3], "[$raw] host -> %s");
+ $this->assertIdentical($url->getPort(), $parts[4], "[$raw] port -> %s");
+ $this->assertIdentical($url->getPath(), $parts[5], "[$raw] path -> %s");
+ $this->assertIdentical($url->getTld(), $parts[6], "[$raw] tld -> %s");
+ $this->assertIdentical($url->getEncodedRequest(), $parts[7], "[$raw] encoded -> %s");
+ $this->assertIdentical($url->getFragment(), $parts[8], "[$raw] fragment -> %s");
+ if ($coords) {
+ $this->assertIdentical($url->getX(), $coords[0], "[$raw] x -> %s");
+ $this->assertIdentical($url->getY(), $coords[1], "[$raw] y -> %s");
+ }
+ }
+
+ function assertPreserved($string) {
+ $url = new SimpleUrl($string);
+ $this->assertEqual($url->asString(), $string);
+ }
+ }
+
+ class TestOfAbsoluteUrls extends UnitTestCase {
+
+ function testMakingAbsolute() {
+ $url = new SimpleUrl('../there/somewhere.php');
+ $this->assertEqual($url->getPath(), '../there/somewhere.php');
+ $absolute = $url->makeAbsolute('https://host.com:1234/I/am/here/');
+ $this->assertEqual($absolute->getScheme(), 'https');
+ $this->assertEqual($absolute->getHost(), 'host.com');
+ $this->assertEqual($absolute->getPort(), 1234);
+ $this->assertEqual($absolute->getPath(), '/I/am/there/somewhere.php');
+ }
+
+ function testMakingAnEmptyUrlAbsolute() {
+ $url = new SimpleUrl('');
+ $this->assertEqual($url->getPath(), '');
+ $absolute = $url->makeAbsolute('http://host.com/I/am/here/page.html');
+ $this->assertEqual($absolute->getScheme(), 'http');
+ $this->assertEqual($absolute->getHost(), 'host.com');
+ $this->assertEqual($absolute->getPath(), '/I/am/here/page.html');
+ }
+
+ function testMakingAnEmptyUrlAbsoluteWithMissingPageName() {
+ $url = new SimpleUrl('');
+ $this->assertEqual($url->getPath(), '');
+ $absolute = $url->makeAbsolute('http://host.com/I/am/here/');
+ $this->assertEqual($absolute->getScheme(), 'http');
+ $this->assertEqual($absolute->getHost(), 'host.com');
+ $this->assertEqual($absolute->getPath(), '/I/am/here/');
+ }
+
+ function testMakingAShortQueryUrlAbsolute() {
+ $url = new SimpleUrl('?a#b');
+ $this->assertEqual($url->getPath(), '');
+ $absolute = $url->makeAbsolute('http://host.com/I/am/here/');
+ $this->assertEqual($absolute->getScheme(), 'http');
+ $this->assertEqual($absolute->getHost(), 'host.com');
+ $this->assertEqual($absolute->getPath(), '/I/am/here/');
+ $this->assertEqual($absolute->getEncodedRequest(), '?a=');
+ $this->assertEqual($absolute->getFragment(), 'b');
+ }
+
+ function testMakingADirectoryUrlAbsolute() {
+ $url = new SimpleUrl('hello/');
+ $this->assertEqual($url->getPath(), 'hello/');
+ $this->assertEqual($url->getBasePath(), 'hello/');
+ $this->assertEqual($url->getPage(), '');
+ $absolute = $url->makeAbsolute('http://host.com/I/am/here/page.html');
+ $this->assertEqual($absolute->getPath(), '/I/am/here/hello/');
+ }
+
+ function testMakingARootUrlAbsolute() {
+ $url = new SimpleUrl('/');
+ $this->assertEqual($url->getPath(), '/');
+ $absolute = $url->makeAbsolute('http://host.com/I/am/here/page.html');
+ $this->assertEqual($absolute->getPath(), '/');
+ }
+
+ function testMakingARootPageUrlAbsolute() {
+ $url = new SimpleUrl('/here.html');
+ $absolute = $url->makeAbsolute('http://host.com/I/am/here/page.html');
+ $this->assertEqual($absolute->getPath(), '/here.html');
+ }
+
+ function testMakingCoordinateUrlAbsolute() {
+ $url = new SimpleUrl('?1,2');
+ $this->assertEqual($url->getPath(), '');
+ $absolute = $url->makeAbsolute('http://host.com/I/am/here/');
+ $this->assertEqual($absolute->getScheme(), 'http');
+ $this->assertEqual($absolute->getHost(), 'host.com');
+ $this->assertEqual($absolute->getPath(), '/I/am/here/');
+ $this->assertEqual($absolute->getX(), 1);
+ $this->assertEqual($absolute->getY(), 2);
+ }
+
+ function testMakingAbsoluteAppendedPath() {
+ $url = new SimpleUrl('./there/somewhere.php');
+ $absolute = $url->makeAbsolute('https://host.com/here/');
+ $this->assertEqual($absolute->getPath(), '/here/there/somewhere.php');
+ }
+
+ function testMakingAbsoluteBadlyFormedAppendedPath() {
+ $url = new SimpleUrl('there/somewhere.php');
+ $absolute = $url->makeAbsolute('https://host.com/here/');
+ $this->assertEqual($absolute->getPath(), '/here/there/somewhere.php');
+ }
+
+ function testMakingAbsoluteHasNoEffectWhenAlreadyAbsolute() {
+ $url = new SimpleUrl('https://test:secret@www.lastcraft.com:321/stuff/?a=1#f');
+ $absolute = $url->makeAbsolute('http://host.com/here/');
+ $this->assertEqual($absolute->getScheme(), 'https');
+ $this->assertEqual($absolute->getUsername(), 'test');
+ $this->assertEqual($absolute->getPassword(), 'secret');
+ $this->assertEqual($absolute->getHost(), 'www.lastcraft.com');
+ $this->assertEqual($absolute->getPort(), 321);
+ $this->assertEqual($absolute->getPath(), '/stuff/');
+ $this->assertEqual($absolute->getEncodedRequest(), '?a=1');
+ $this->assertEqual($absolute->getFragment(), 'f');
+ }
+
+ function testMakingHostOnlyAbsoluteDoesNotCarryAnyOtherInformation() {
+ $url = new SimpleUrl('http://www.lastcraft.com');
+ $absolute = $url->makeAbsolute('https://host.com:81/here/');
+ $this->assertEqual($absolute->getScheme(), 'http');
+ $this->assertEqual($absolute->getHost(), 'www.lastcraft.com');
+ $this->assertIdentical($absolute->getPort(), false);
+ $this->assertEqual($absolute->getPath(), '/');
+ }
+ }
+
+ class TestOfFrameUrl extends UnitTestCase {
+
+ function testTargetAttachment() {
+ $url = new SimpleUrl('http://www.site.com/home.html');
+ $this->assertIdentical($url->getTarget(), false);
+ $url->setTarget('A frame');
+ $this->assertIdentical($url->getTarget(), 'A frame');
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/user_agent_test.php b/tests/UnitTests/simpletest/test/user_agent_test.php
new file mode 100644
index 00000000..578327c6
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/user_agent_test.php
@@ -0,0 +1,546 @@
+<?php
+ // $Id: user_agent_test.php,v 1.22 2005/01/02 22:46:10 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../user_agent.php');
+ require_once(dirname(__FILE__) . '/../authentication.php');
+ require_once(dirname(__FILE__) . '/../http.php');
+ require_once(dirname(__FILE__) . '/../encoding.php');
+ Mock::generate('SimpleHttpRequest');
+ Mock::generate('SimpleHttpResponse');
+ Mock::generate('SimpleHttpHeaders');
+ Mock::generatePartial('SimpleUserAgent', 'MockRequestUserAgent', array('_createHttpRequest'));
+
+ class TestOfSimpleCookieJar extends UnitTestCase {
+
+ function testAddCookie() {
+ $jar = new SimpleCookieJar();
+ $jar->setCookie(new SimpleCookie("a", "A"));
+ $cookies = $jar->getValidCookies();
+ $this->assertEqual(count($cookies), 1);
+ $this->assertEqual($cookies[0]->getValue(), "A");
+ }
+
+ function testHostFilter() {
+ $jar = new SimpleCookieJar();
+ $cookie = new SimpleCookie('a', 'A');
+ $cookie->setHost('my-host.com');
+ $jar->setCookie($cookie);
+ $cookie = new SimpleCookie('b', 'B');
+ $cookie->setHost('another-host.com');
+ $jar->setCookie($cookie);
+ $cookie = new SimpleCookie('c', 'C');
+ $jar->setCookie($cookie);
+ $cookies = $jar->getValidCookies('my-host.com');
+ $this->assertEqual(count($cookies), 2);
+ $this->assertEqual($cookies[0]->getValue(), 'A');
+ $this->assertEqual($cookies[1]->getValue(), 'C');
+ $this->assertEqual(count($jar->getValidCookies('another-host.com')), 2);
+ $this->assertEqual(count($jar->getValidCookies('www.another-host.com')), 2);
+ $this->assertEqual(count($jar->getValidCookies('new-host.org')), 1);
+ $this->assertEqual(count($jar->getValidCookies()), 3);
+ }
+
+ function testPathFilter() {
+ $jar = new SimpleCookieJar();
+ $jar->setCookie(new SimpleCookie("a", "A", "/path/"));
+ $this->assertEqual(count($jar->getValidCookies(false, "/")), 0);
+ $this->assertEqual(count($jar->getValidCookies(false, "/elsewhere")), 0);
+ $this->assertEqual(count($jar->getValidCookies(false, "/path/")), 1);
+ $this->assertEqual(count($jar->getValidCookies(false, "/path")), 1);
+ $this->assertEqual(count($jar->getValidCookies(false, "/pa")), 0);
+ $this->assertEqual(count($jar->getValidCookies(false, "/path/here/")), 1);
+ }
+
+ function testPathFilterDeeply() {
+ $jar = new SimpleCookieJar();
+ $jar->setCookie(new SimpleCookie("a", "A", "/path/more_path/"));
+ $this->assertEqual(count($jar->getValidCookies(false, "/path/")), 0);
+ $this->assertEqual(count($jar->getValidCookies(false, "/path")), 0);
+ $this->assertEqual(count($jar->getValidCookies(false, "/pa")), 0);
+ $this->assertEqual(count($jar->getValidCookies(false, "/path/more_path/")), 1);
+ $this->assertEqual(count($jar->getValidCookies(false, "/path/more_path/and_more")), 1);
+ $this->assertEqual(count($jar->getValidCookies(false, "/path/not_here/")), 0);
+ }
+
+ function testMultipleCookieWithDifferentPaths() {
+ $jar = new SimpleCookieJar();
+ $jar->setCookie(new SimpleCookie("a", "abc", "/"));
+ $jar->setCookie(new SimpleCookie("a", "123", "/path/here/"));
+ $cookies = $jar->getValidCookies("my-host.com", "/");
+ $this->assertEqual($cookies[0]->getPath(), "/");
+ $cookies = $jar->getValidCookies("my-host.com", "/path/");
+ $this->assertEqual($cookies[0]->getPath(), "/");
+ $cookies = $jar->getValidCookies("my-host.com", "/path/here");
+ $this->assertEqual($cookies[0]->getPath(), "/");
+ $this->assertEqual($cookies[1]->getPath(), "/path/here/");
+ $cookies = $jar->getValidCookies("my-host.com", "/path/here/there");
+ $this->assertEqual($cookies[0]->getPath(), "/");
+ $this->assertEqual($cookies[1]->getPath(), "/path/here/");
+ }
+
+ function testOverwrite() {
+ $jar = new SimpleCookieJar();
+ $jar->setCookie(new SimpleCookie("a", "abc", "/"));
+ $jar->setCookie(new SimpleCookie("a", "cde", "/"));
+ $cookies = $jar->getValidCookies();
+ $this->assertIdentical($cookies[0]->getValue(), "cde");
+ }
+
+ function testClearSessionCookies() {
+ $jar = new SimpleCookieJar();
+ $jar->setCookie(new SimpleCookie("a", "A", "/"));
+ $jar->restartSession();
+ $this->assertEqual(count($jar->getValidCookies(false, "/")), 0);
+ }
+
+ function testExpiryFilterByDate() {
+ $cookie = new SimpleCookie("a", "A", "/", "Wed, 25-Dec-02 04:24:20 GMT");
+ $jar = new SimpleCookieJar();
+ $jar->setCookie($cookie);
+ $jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
+ $this->assertIdentical($list = $jar->getValidCookies(false, "/"), array($cookie));
+ $jar->restartSession("Wed, 25-Dec-02 04:24:21 GMT");
+ $this->assertIdentical($list = $jar->getValidCookies(false, "/"), array());
+ }
+
+ function testExpiryFilterByAgeing() {
+ $cookie = new SimpleCookie("a", "A", "/", "Wed, 25-Dec-02 04:24:20 GMT");
+ $jar = new SimpleCookieJar();
+ $jar->setCookie($cookie);
+ $jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
+ $this->assertIdentical($list = $jar->getValidCookies(false, "/"), array($cookie));
+ $jar->agePrematurely(2);
+ $jar->restartSession("Wed, 25-Dec-02 04:24:19 GMT");
+ $this->assertIdentical($list = $jar->getValidCookies(false, "/"), array());
+ }
+
+ function testCookieClearing() {
+ $jar = new SimpleCookieJar();
+ $jar->setCookie(new SimpleCookie("a", "abc", "/"));
+ $jar->setCookie(new SimpleCookie("a", "", "/"));
+ $this->assertEqual(count($cookies = $jar->getValidCookies(false, "/")), 1);
+ $this->assertIdentical($cookies[0]->getValue(), "");
+ }
+
+ function testCookieClearByDate() {
+ $jar = new SimpleCookieJar();
+ $jar->setCookie(new SimpleCookie("a", "abc", "/", "Wed, 25-Dec-02 04:24:21 GMT"));
+ $jar->setCookie(new SimpleCookie("a", "def", "/", "Wed, 25-Dec-02 04:24:19 GMT"));
+ $cookies = $jar->getValidCookies(false, "/");
+ $this->assertIdentical($cookies[0]->getValue(), "def");
+ $jar->restartSession("Wed, 25-Dec-02 04:24:20 GMT");
+ $this->assertEqual(count($jar->getValidCookies(false, "/")), 0);
+ }
+ }
+
+ class TestOfFetchingUrlParameters extends UnitTestCase {
+
+ function testGet() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getMimeType', 'text/html');
+ $headers->setReturnValue('getResponseCode', 200);
+ $headers->setReturnValue('getNewCookies', array());
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'stuff');
+ $response->setReturnReference('getHeaders', $headers);
+
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference('fetch', $response);
+
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReference('_createHttpRequest', $request);
+ $agent->expectOnce('_createHttpRequest', array(
+ 'GET',
+ new SimpleUrl('http://test:secret@this.com/page.html?a=A&b=B'),
+ new SimpleFormEncoding()));
+ $agent->SimpleUserAgent();
+
+ $agent->fetchResponse(
+ 'GET',
+ new SimpleUrl('http://test:secret@this.com/page.html'),
+ new SimpleFormEncoding(array('a' => 'A', 'b' => 'B')));
+ $agent->tally();
+ }
+
+ function testHead() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getMimeType', 'text/html');
+ $headers->setReturnValue('getResponseCode', 200);
+ $headers->setReturnValue('getNewCookies', array());
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'stuff');
+ $response->setReturnReference('getHeaders', $headers);
+
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference('fetch', $response);
+
+ $url = new SimpleUrl('http://this.com/page.html');
+ $url->addRequestParameters(array('a' => 'A', 'b' => 'B'));
+
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReference('_createHttpRequest', $request);
+ $agent->expectOnce('_createHttpRequest', array(
+ 'HEAD',
+ new SimpleUrl('http://test:secret@this.com/page.html?a=A&b=B'),
+ new SimpleFormEncoding()));
+ $agent->SimpleUserAgent();
+
+ $agent->fetchResponse(
+ 'HEAD',
+ new SimpleUrl('http://test:secret@this.com/page.html'),
+ new SimpleFormEncoding(array('a' => 'A', 'b' => 'B')));
+ $agent->tally();
+ }
+
+ function testPost() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getMimeType', 'text/html');
+ $headers->setReturnValue('getResponseCode', 200);
+ $headers->setReturnValue('getNewCookies', array());
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', 'stuff');
+ $response->setReturnReference('getHeaders', $headers);
+
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference('fetch', $response);
+
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReference('_createHttpRequest', $request);
+ $agent->expectOnce('_createHttpRequest', array(
+ 'POST',
+ new SimpleUrl('http://test:secret@this.com/page.html'),
+ new SimpleFormEncoding(array('a' => 'A', 'b' => 'B'))));
+ $agent->SimpleUserAgent();
+
+ $agent->fetchResponse(
+ 'POST',
+ new SimpleUrl('http://test:secret@this.com/page.html'),
+ new SimpleFormEncoding(array('a' => 'A', 'b' => 'B')));
+ $agent->tally();
+ }
+ }
+
+ class TestOfAdditionalHeaders extends UnitTestCase {
+
+ function testAdditionalHeaderAddedToRequest() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getNewCookies', array());
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnReference('getHeaders', $headers);
+
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference('fetch', $response);
+ $request->expectOnce(
+ 'addHeaderLine',
+ array('User-Agent: SimpleTest'));
+
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReference('_createHttpRequest', $request);
+ $agent->SimpleUserAgent();
+
+ $agent->addHeader('User-Agent: SimpleTest');
+ $response = &$agent->fetchResponse('GET', new SimpleUrl('http://this.host/'));
+ $request->tally();
+ }
+ }
+
+ class TestOfBrowserCookies extends UnitTestCase {
+
+ function &_createStandardResponse() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue("getNewCookies", array());
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue("isError", false);
+ $response->setReturnValue("getContent", "stuff");
+ $response->setReturnReference("getHeaders", $headers);
+ return $response;
+ }
+
+ function &_createCookieSite($cookies) {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue("getNewCookies", $cookies);
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue("isError", false);
+ $response->setReturnReference("getHeaders", $headers);
+ $response->setReturnValue("getContent", "stuff");
+
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference("fetch", $response);
+ return $request;
+ }
+
+ function &_createPartialFetcher(&$request) {
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReference('_createHttpRequest', $request);
+ $agent->SimpleUserAgent();
+ return $agent;
+ }
+
+ function testSendingExistingCookie() {
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference('fetch', $this->_createStandardResponse());
+ $request->expectOnce('setCookie', array(new SimpleCookie('a', 'A')));
+
+ $agent = &$this->_createPartialFetcher($request);
+ $agent->setCookie('a', 'A');
+ $response = $agent->fetchResponse(
+ 'GET',
+ new SimpleUrl('http://this.com/this/path/page.html'),
+ array());
+ $this->assertEqual($response->getContent(), "stuff");
+ $request->tally();
+ }
+
+ function testOverwriteCookieThatAlreadyExists() {
+ $request = &$this->_createCookieSite(array(new SimpleCookie("a", "AAAA", "this/path/")));
+ $agent = &$this->_createPartialFetcher($request);
+
+ $agent->setCookie("a", "A");
+ $agent->fetchResponse(
+ "GET",
+ new SimpleUrl('http://this.com/this/path/page.html'),
+ array());
+ $this->assertEqual($agent->getCookieValue("this.com", "this/path/", "a"), "AAAA");
+ }
+
+ function testClearCookieBySettingExpiry() {
+ $request = &$this->_createCookieSite(array(
+ new SimpleCookie("a", "b", "this/path/", "Wed, 25-Dec-02 04:24:19 GMT")));
+ $agent = &$this->_createPartialFetcher($request);
+
+ $agent->setCookie("a", "A", "this/path/", "Wed, 25-Dec-02 04:24:21 GMT");
+ $agent->fetchResponse(
+ 'GET',
+ new SimpleUrl('http://this.com/this/path/page.html'),
+ array());
+ $this->assertIdentical(
+ $agent->getCookieValue("this.com", "this/path/", "a"),
+ "b");
+ $agent->restart("Wed, 25-Dec-02 04:24:20 GMT");
+ $this->assertIdentical(
+ $agent->getCookieValue("this.com", "this/path/", "a"),
+ false);
+ }
+
+ function testAgeingAndClearing() {
+ $request = &$this->_createCookieSite(array(
+ new SimpleCookie("a", "A", "this/path/", "Wed, 25-Dec-02 04:24:21 GMT")));
+ $agent = &$this->_createPartialFetcher($request);
+
+ $agent->fetchResponse(
+ 'GET',
+ new SimpleUrl('http://this.com/this/path/page.html'),
+ array());
+ $agent->restart("Wed, 25-Dec-02 04:24:20 GMT");
+ $this->assertIdentical(
+ $agent->getCookieValue("this.com", "this/path/", "a"),
+ "A");
+ $agent->ageCookies(2);
+ $agent->restart("Wed, 25-Dec-02 04:24:20 GMT");
+ $this->assertIdentical(
+ $agent->getCookieValue("this.com", "this/path/", "a"),
+ false);
+ }
+
+ function testReadingIncomingAndSetCookies() {
+ $request = &$this->_createCookieSite(array(
+ new SimpleCookie("a", "AAA", "this/path/")));
+ $agent = &$this->_createPartialFetcher($request);
+
+ $this->assertNull($agent->getBaseCookieValue("a", false));
+ $agent->fetchResponse(
+ 'GET',
+ new SimpleUrl('http://this.com/this/path/page.html'),
+ array());
+ $agent->setCookie("b", "BBB", "this.com", "this/path/");
+ $this->assertEqual(
+ $agent->getBaseCookieValue("a", new SimpleUrl('http://this.com/this/path/page.html')),
+ "AAA");
+ $this->assertEqual(
+ $agent->getBaseCookieValue("b", new SimpleUrl('http://this.com/this/path/page.html')),
+ "BBB");
+ }
+ }
+
+ class TestOfHttpRedirects extends UnitTestCase {
+
+ function &createRedirect($content, $redirect) {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getNewCookies', array());
+ $headers->setReturnValue('isRedirect', (boolean)$redirect);
+ $headers->setReturnValue('getLocation', $redirect);
+ $headers->setReturnValue('getNewCookies', array());
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('getContent', $content);
+ $response->setReturnReference('getHeaders', $headers);
+
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference('fetch', $response);
+ return $request;
+ }
+
+ function testDisabledRedirects() {
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReference(
+ '_createHttpRequest',
+ $this->createRedirect('stuff', 'there.html'));
+ $agent->expectOnce('_createHttpRequest');
+ $agent->SimpleUserAgent();
+
+ $agent->setMaximumRedirects(0);
+ $response = &$agent->fetchResponse('GET', new SimpleUrl('here.html'));
+
+ $this->assertEqual($response->getContent(), 'stuff');
+ $agent->tally();
+ }
+
+ function testSingleRedirect() {
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReferenceAt(
+ 0,
+ '_createHttpRequest',
+ $this->createRedirect('first', 'two.html'));
+ $agent->setReturnReferenceAt(
+ 1,
+ '_createHttpRequest',
+ $this->createRedirect('second', 'three.html'));
+ $agent->expectCallCount('_createHttpRequest', 2);
+ $agent->SimpleUserAgent();
+
+ $agent->setMaximumRedirects(1);
+ $response = &$agent->fetchResponse('GET', new SimpleUrl('one.html'));
+
+ $this->assertEqual($response->getContent(), 'second');
+ $agent->tally();
+ }
+
+ function testDoubleRedirect() {
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReferenceAt(
+ 0,
+ '_createHttpRequest',
+ $this->createRedirect('first', 'two.html'));
+ $agent->setReturnReferenceAt(
+ 1,
+ '_createHttpRequest',
+ $this->createRedirect('second', 'three.html'));
+ $agent->setReturnReferenceAt(
+ 2,
+ '_createHttpRequest',
+ $this->createRedirect('third', 'four.html'));
+ $agent->expectCallCount('_createHttpRequest', 3);
+ $agent->SimpleUserAgent();
+
+ $agent->setMaximumRedirects(2);
+ $response = &$agent->fetchResponse('GET', new SimpleUrl('one.html'));
+
+ $this->assertEqual($response->getContent(), 'third');
+ $agent->tally();
+ }
+
+ function testSuccessAfterRedirect() {
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReferenceAt(
+ 0,
+ '_createHttpRequest',
+ $this->createRedirect('first', 'two.html'));
+ $agent->setReturnReferenceAt(
+ 1,
+ '_createHttpRequest',
+ $this->createRedirect('second', false));
+ $agent->setReturnReferenceAt(
+ 2,
+ '_createHttpRequest',
+ $this->createRedirect('third', 'four.html'));
+ $agent->expectCallCount('_createHttpRequest', 2);
+ $agent->SimpleUserAgent();
+
+ $agent->setMaximumRedirects(2);
+ $response = &$agent->fetchResponse('GET', new SimpleUrl('one.html'));
+
+ $this->assertEqual($response->getContent(), 'second');
+ $agent->tally();
+ }
+
+ function testRedirectChangesPostToGet() {
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReferenceAt(
+ 0,
+ '_createHttpRequest',
+ $this->createRedirect('first', 'two.html'));
+ $agent->expectArgumentsAt(0, '_createHttpRequest', array('POST', '*', '*'));
+ $agent->setReturnReferenceAt(
+ 1,
+ '_createHttpRequest',
+ $this->createRedirect('second', 'three.html'));
+ $agent->expectArgumentsAt(1, '_createHttpRequest', array('GET', '*', '*'));
+ $agent->expectCallCount('_createHttpRequest', 2);
+ $agent->SimpleUserAgent();
+
+ $agent->setMaximumRedirects(1);
+ $response = &$agent->fetchResponse('POST', new SimpleUrl('one.html'));
+
+ $agent->tally();
+ }
+ }
+
+ class TestOfBadHosts extends UnitTestCase {
+
+ function &_createSimulatedBadHost() {
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnValue('isError', true);
+ $response->setReturnValue('getError', 'Bad socket');
+ $response->setReturnValue('getContent', false);
+
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference('fetch', $response);
+ return $request;
+ }
+
+ function testUntestedHost() {
+ $request = &$this->_createSimulatedBadHost();
+
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReference('_createHttpRequest', $request);
+ $agent->SimpleUserAgent();
+
+ $response = &$agent->fetchResponse(
+ 'GET',
+ new SimpleUrl('http://this.host/this/path/page.html'));
+ $this->assertTrue($response->isError());
+ }
+ }
+
+ class TestOfAuthorisation extends UnitTestCase {
+
+ function testAuthenticateHeaderAdded() {
+ $headers = &new MockSimpleHttpHeaders($this);
+ $headers->setReturnValue('getNewCookies', array());
+
+ $response = &new MockSimpleHttpResponse($this);
+ $response->setReturnReference('getHeaders', $headers);
+
+ $request = &new MockSimpleHttpRequest($this);
+ $request->setReturnReference('fetch', $response);
+ $request->expectOnce(
+ 'addHeaderLine',
+ array('Authorization: Basic ' . base64_encode('test:secret')));
+
+ $agent = &new MockRequestUserAgent($this);
+ $agent->setReturnReference('_createHttpRequest', $request);
+ $agent->SimpleUserAgent();
+
+ $response = &$agent->fetchResponse(
+ 'GET',
+ new SimpleUrl('http://test:secret@this.host'));
+ $request->tally();
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/visual_test.php b/tests/UnitTests/simpletest/test/visual_test.php
new file mode 100644
index 00000000..975ef050
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/visual_test.php
@@ -0,0 +1,386 @@
+<?php
+ // $Id: visual_test.php,v 1.29 2004/11/22 19:20:00 lastcraft Exp $
+
+ // NOTE:
+ // Some of these tests are designed to fail! Do not be alarmed.
+ // ----------------
+
+ // The following tests are a bit hacky. Whilst Kent Beck tried to
+ // build a unit tester with a unit tester I am not that brave.
+ // Instead I have just hacked together odd test scripts until
+ // I have enough of a tester to procede more formally.
+ //
+ // The proper tests start in all_tests.php
+ require_once('../unit_tester.php');
+ require_once('../shell_tester.php');
+ require_once('../mock_objects.php');
+ require_once('../reporter.php');
+ require_once('../xml.php');
+
+ class TestDisplayClass {
+ var $_a;
+
+ function TestDisplayClass($a) {
+ $this->_a = $a;
+ }
+ }
+
+ class TestOfUnitTestCaseOutput extends UnitTestCase {
+
+ function testOfResults() {
+ $this->pass('Pass');
+ $this->fail('Fail'); // Fail.
+ }
+
+ function testTrue() {
+ $this->assertTrue(true);
+ $this->assertTrue(false); // Fail.
+ }
+
+ function testFalse() {
+ $this->assertFalse(true); // Fail.
+ $this->assertFalse(false);
+ }
+
+ function testExpectation() {
+ $expectation = &new EqualExpectation(25, 'My expectation message: %s');
+ $this->assertExpectation($expectation, 25, 'My assert message : %s');
+ $this->assertExpectation($expectation, 24, 'My assert message : %s'); // Fail.
+ }
+
+ function testNull() {
+ $this->assertNull(null, "%s -> Pass");
+ $this->assertNull(false, "%s -> Fail"); // Fail.
+ $this->assertNotNull(null, "%s -> Fail"); // Fail.
+ $this->assertNotNull(false, "%s -> Pass");
+ }
+
+ function testType() {
+ $this->assertIsA("hello", "string", "%s -> Pass");
+ $this->assertIsA(14, "string", "%s -> Fail"); // Fail.
+ $this->assertIsA($this, "TestOfUnitTestCaseOutput", "%s -> Pass");
+ $this->assertIsA($this, "UnitTestCase", "%s -> Pass");
+ $this->assertIsA(14, "TestOfUnitTestCaseOutput", "%s -> Fail"); // Fail.
+ $this->assertIsA($this, "TestReporter", "%s -> Fail"); // Fail.
+ }
+
+ function testTypeEquality() {
+ $this->assertEqual("0", 0, "%s -> Pass");
+ $this->assertNotEqual("0", 0, "%s -> Fail"); // Fail.
+ }
+
+ function testNullEquality() {
+ $this->assertEqual(null, 1, "%s -> Fail"); // Fail.
+ $this->assertNotEqual(null, 1, "%s -> Pass");
+ $this->assertEqual(1, null, "%s -> Fail"); // Fail.
+ $this->assertNotEqual(1, null, "%s -> Pass");
+ }
+
+ function testIntegerEquality() {
+ $this->assertEqual(1, 2, "%s -> Fail"); // Fail.
+ $this->assertNotEqual(1, 2, "%s -> Pass");
+ }
+
+ function testStringEquality() {
+ $this->assertEqual("a", "a", "%s -> Pass");
+ $this->assertNotEqual("a", "a", "%s -> Fail"); // Fail.
+ $this->assertEqual("aa", "ab", "%s -> Fail"); // Fail.
+ $this->assertNotEqual("aa", "ab", "%s -> Pass");
+ }
+
+ function testHashEquality() {
+ $this->assertEqual(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "A"), "%s -> Pass");
+ $this->assertEqual(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "Z"), "%s -> Pass");
+ }
+
+ function testStringIdentity() {
+ $a = "fred";
+ $b = $a;
+ $this->assertIdentical($a, $b, "%s -> Pass");
+ $this->assertNotIdentical($a, $b, "%s -> Fail"); // Fail.
+ }
+
+ function testTypeIdentity() {
+ $a = "0";
+ $b = 0;
+ $this->assertIdentical($a, $b, "%s -> Fail"); // Fail.
+ $this->assertNotIdentical($a, $b, "%s -> Pass");
+ }
+
+ function testNullIdentity() {
+ $this->assertIdentical(null, 1, "%s -> Fail"); // Fail.
+ $this->assertNotIdentical(null, 1, "%s -> Pass");
+ $this->assertIdentical(1, null, "%s -> Fail"); // Fail.
+ $this->assertNotIdentical(1, null, "%s -> Pass");
+ }
+
+ function testHashIdentity() {
+ $this->assertIdentical(array("a" => "A", "b" => "B"), array("b" => "B", "a" => "A"), "%s -> fail"); // Fail.
+ }
+
+ function testObjectEquality() {
+ $this->assertEqual(new TestDisplayClass(4), new TestDisplayClass(4), "%s -> Pass");
+ $this->assertNotEqual(new TestDisplayClass(4), new TestDisplayClass(4), "%s -> Fail"); // Fail.
+ $this->assertEqual(new TestDisplayClass(4), new TestDisplayClass(5), "%s -> Fail"); // Fail.
+ $this->assertNotEqual(new TestDisplayClass(4), new TestDisplayClass(5), "%s -> Pass");
+ }
+
+ function testObjectIndentity() {
+ $this->assertIdentical(new TestDisplayClass(false), new TestDisplayClass(false), "%s -> Pass");
+ $this->assertNotIdentical(new TestDisplayClass(false), new TestDisplayClass(false), "%s -> Fail"); // Fail.
+ $this->assertIdentical(new TestDisplayClass(false), new TestDisplayClass(0), "%s -> Fail"); // Fail.
+ $this->assertNotIdentical(new TestDisplayClass(false), new TestDisplayClass(0), "%s -> Pass");
+ }
+
+ function testReference() {
+ $a = "fred";
+ $b = &$a;
+ $this->assertReference($a, $b, "%s -> Pass");
+ $this->assertCopy($a, $b, "%s -> Fail"); // Fail.
+ $c = "Hello";
+ $this->assertReference($a, $c, "%s -> Fail"); // Fail.
+ $this->assertCopy($a, $c, "%s -> Pass");
+ }
+
+ function testPatterns() {
+ $this->assertWantedPattern('/hello/i', "Hello there", "%s -> Pass");
+ $this->assertNoUnwantedPattern('/hello/', "Hello there", "%s -> Pass");
+ $this->assertWantedPattern('/hello/', "Hello there", "%s -> Fail"); // Fail.
+ $this->assertNoUnwantedPattern('/hello/i', "Hello there", "%s -> Fail"); // Fail.
+ }
+
+ function testLongStrings() {
+ $text = "";
+ for ($i = 0; $i < 10; $i++) {
+ $text .= "0123456789";
+ }
+ $this->assertEqual($text, $text);
+ $this->assertEqual($text . $text, $text . "a" . $text); // Fail.
+ }
+
+ function testErrorDisplay() {
+ trigger_error('Default'); // Exception.
+ trigger_error('Error', E_USER_ERROR); // Exception.
+ trigger_error('Warning', E_USER_WARNING); // Exception.
+ trigger_error('Notice', E_USER_NOTICE); // Exception.
+ }
+
+ function testErrorTrap() {
+ $this->assertNoErrors("%s -> Pass");
+ $this->assertError(); // Fail.
+ trigger_error('Error 1');
+ $this->assertNoErrors("%s -> Fail"); // Fail.
+ $this->assertError();
+ $this->assertNoErrors("%s -> Pass at end");
+ }
+
+ function testErrorText() {
+ trigger_error('Error 2');
+ $this->assertError('Error 2', "%s -> Pass");
+ trigger_error('Error 3');
+ $this->assertError('Error 2', "%s -> Fail"); // Fail.
+ }
+
+ function testErrorPatterns() {
+ trigger_error('Error 2');
+ $this->assertErrorPattern('/Error 2/', "%s -> Pass");
+ trigger_error('Error 3');
+ $this->assertErrorPattern('/Error 2/', "%s -> Fail"); // Fail.
+ }
+
+ function testDumping() {
+ $this->dump(array("Hello"), "Displaying a variable");
+ }
+
+ function testSignal() {
+ $fred = "signal as a string";
+ $this->signal("Signal", $fred); // Signal.
+ }
+ }
+
+ class Dummy {
+ function Dummy() {
+ }
+
+ function a() {
+ }
+ }
+ Mock::generate('Dummy');
+
+ class TestOfMockObjectsOutput extends UnitTestCase {
+
+ function testCallCounts() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectCallCount('a', 1, 'My message: %s');
+ $dummy->a();
+ $dummy->tally();
+ $dummy->a();
+ $dummy->tally();
+ }
+
+ function testMinimumCallCounts() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectMinimumCallCount('a', 2, 'My message: %s');
+ $dummy->a();
+ $dummy->tally();
+ $dummy->a();
+ $dummy->tally();
+ }
+
+ function testEmptyMatching() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array());
+ $dummy->a();
+ $dummy->a(null); // Fail.
+ }
+
+ function testEmptyMatchingWithCustomMessage() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array(), 'My expectation message: %s');
+ $dummy->a();
+ $dummy->a(null); // Fail.
+ }
+
+ function testNullMatching() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array(null));
+ $dummy->a(null);
+ $dummy->a(); // Fail.
+ }
+
+ function testBooleanMatching() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array(true, false));
+ $dummy->a(true, false);
+ $dummy->a(true, true); // Fail.
+ }
+
+ function testIntegerMatching() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array(32, 33));
+ $dummy->a(32, 33);
+ $dummy->a(32, 34); // Fail.
+ }
+
+ function testFloatMatching() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array(3.2, 3.3));
+ $dummy->a(3.2, 3.3);
+ $dummy->a(3.2, 3.4); // Fail.
+ }
+
+ function testStringMatching() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array('32', '33'));
+ $dummy->a('32', '33');
+ $dummy->a('32', '34'); // Fail.
+ }
+
+ function testEmptyMatchingWithCustomExpectationMessage() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments(
+ 'a',
+ array(new EqualExpectation('A', 'My part expectation message: %s')),
+ 'My expectation message: %s');
+ $dummy->a('A');
+ $dummy->a('B'); // Fail.
+ }
+
+ function testArrayMatching() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array(array(32), array(33)));
+ $dummy->a(array(32), array(33));
+ $dummy->a(array(32), array('33')); // Fail.
+ }
+
+ function testObjectMatching() {
+ $a = new Dummy();
+ $a->a = 'a';
+ $b = new Dummy();
+ $b->b = 'b';
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array($a, $b));
+ $dummy->a($a, $b);
+ $dummy->a($a, $a); // Fail.
+ }
+
+ function testBigList() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array(false, 0, 1, 1.0));
+ $dummy->a(false, 0, 1, 1.0);
+ $dummy->a(true, false, 2, 2.0); // Fail.
+ }
+ }
+
+ class TestOfPastBugs extends UnitTestCase {
+
+ function testMixedTypes() {
+ $this->assertEqual(array(), null, "%s -> Pass");
+ $this->assertIdentical(array(), null, "%s -> Fail"); // Fail.
+ }
+
+ function testMockWildcards() {
+ $dummy = &new MockDummy($this);
+ $dummy->expectArguments('a', array('*', array(33)));
+ $dummy->a(array(32), array(33));
+ $dummy->a(array(32), array('33')); // Fail.
+ }
+ }
+
+ class TestOfVisualShell extends ShellTestCase {
+
+ function testDump() {
+ $this->execute('ls');
+ $this->dumpOutput();
+ $this->execute('dir');
+ $this->dumpOutput();
+ }
+
+ function testDumpOfList() {
+ $this->execute('ls');
+ $this->dump($this->getOutputAsList());
+ }
+ }
+
+ class AllOutputReporter extends HtmlReporter {
+
+ function _getCss() {
+ return parent::_getCss() . ' .pass { color: darkgreen; }';
+ }
+
+ function paintPass($message) {
+ parent::paintPass($message);
+ print "<span class=\"pass\">Pass</span>: ";
+ $breadcrumb = $this->getTestList();
+ array_shift($breadcrumb);
+ print implode(" -&gt; ", $breadcrumb);
+ print " -&gt; " . htmlentities($message) . "<br />\n";
+ }
+
+ function paintSignal($type, &$payload) {
+ print "<span class=\"fail\">$type</span>: ";
+ $breadcrumb = $this->getTestList();
+ array_shift($breadcrumb);
+ print implode(" -&gt; ", $breadcrumb);
+ print " -&gt; " . htmlentities(serialize($payload)) . "<br />\n";
+ }
+ }
+
+ $test = &new GroupTest("Visual test with 49 passes, 49 fails and 4 exceptions");
+ $test->addTestCase(new TestOfUnitTestCaseOutput());
+ $test->addTestCase(new TestOfMockObjectsOutput());
+ $test->addTestCase(new TestOfPastBugs());
+ $test->addTestCase(new TestOfVisualShell());
+
+ if (isset($_GET['xml']) || in_array('xml', (isset($argv) ? $argv : array()))) {
+ $reporter = &new XmlReporter();
+ } elseif(SimpleReporter::inCli()) {
+ $reporter = &new TextReporter();
+ } else {
+ $reporter = &new AllOutputReporter();
+ }
+ if (isset($_GET['dry']) || in_array('dry', (isset($argv) ? $argv : array()))) {
+ $reporter->makeDry();
+ }
+ exit ($test->run($reporter) ? 0 : 1);
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/web_tester_test.php b/tests/UnitTests/simpletest/test/web_tester_test.php
new file mode 100644
index 00000000..6d3219c5
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/web_tester_test.php
@@ -0,0 +1,133 @@
+<?php
+ // $Id: web_tester_test.php,v 1.10 2004/11/30 05:34:00 lastcraft Exp $
+
+ class TestOfFieldExpectation extends UnitTestCase {
+
+ function testStringMatchingIsCaseSensitive() {
+ $expectation = new FieldExpectation('a');
+ $this->assertTrue($expectation->test('a'));
+ $this->assertTrue($expectation->test(array('a')));
+ $this->assertFalse($expectation->test('A'));
+ }
+
+ function testMatchesInteger() {
+ $expectation = new FieldExpectation('1');
+ $this->assertTrue($expectation->test('1'));
+ $this->assertTrue($expectation->test(1));
+ $this->assertTrue($expectation->test(array('1')));
+ $this->assertTrue($expectation->test(array(1)));
+ }
+
+ function testNonStringFailsExpectation() {
+ $expectation = new FieldExpectation('a');
+ $this->assertFalse($expectation->test(null));
+ }
+
+ function testUnsetFieldCanBeTestedFor() {
+ $expectation = new FieldExpectation(false);
+ $this->assertTrue($expectation->test(false));
+ }
+
+ function testMultipleValuesCanBeInAnyOrder() {
+ $expectation = new FieldExpectation(array('a', 'b'));
+ $this->assertTrue($expectation->test(array('a', 'b')));
+ $this->assertTrue($expectation->test(array('b', 'a')));
+ $this->assertFalse($expectation->test(array('a', 'a')));
+ $this->assertFalse($expectation->test('a'));
+ }
+
+ function testSingleItemCanBeArrayOrString() {
+ $expectation = new FieldExpectation(array('a'));
+ $this->assertTrue($expectation->test(array('a')));
+ $this->assertTrue($expectation->test('a'));
+ }
+ }
+
+ class TestOfHeaderExpectations extends UnitTestCase {
+
+ function testExpectingOnlyTheHeaderName() {
+ $expectation = new HttpHeaderExpectation('a');
+ $this->assertIdentical($expectation->test(false), false);
+ $this->assertIdentical($expectation->test('a: A'), true);
+ $this->assertIdentical($expectation->test('A: A'), true);
+ $this->assertIdentical($expectation->test('a: B'), true);
+ $this->assertIdentical($expectation->test(' a : A '), true);
+ }
+
+ function testHeaderValueAsWell() {
+ $expectation = new HttpHeaderExpectation('a', 'A');
+ $this->assertIdentical($expectation->test(false), false);
+ $this->assertIdentical($expectation->test('a: A'), true);
+ $this->assertIdentical($expectation->test('A: A'), true);
+ $this->assertIdentical($expectation->test('A: a'), false);
+ $this->assertIdentical($expectation->test('a: B'), false);
+ $this->assertIdentical($expectation->test(' a : A '), true);
+ $this->assertIdentical($expectation->test(' a : AB '), false);
+ }
+
+ function testMultilineSearch() {
+ $expectation = new HttpHeaderExpectation('a', 'A');
+ $this->assertIdentical($expectation->test("aa: A\r\nb: B\r\nc: C"), false);
+ $this->assertIdentical($expectation->test("aa: A\r\na: A\r\nb: B"), true);
+ }
+
+ function testMultilineSearchWithPadding() {
+ $expectation = new HttpHeaderExpectation('a', ' A ');
+ $this->assertIdentical($expectation->test("aa:A\r\nb:B\r\nc:C"), false);
+ $this->assertIdentical($expectation->test("aa:A\r\na:A\r\nb:B"), true);
+ }
+
+ function testPatternMatching() {
+ $expectation = new HttpHeaderPatternExpectation('a', '/A/');
+ $this->assertIdentical($expectation->test('a: A'), true);
+ $this->assertIdentical($expectation->test('A: A'), true);
+ $this->assertIdentical($expectation->test('A: a'), false);
+ $this->assertIdentical($expectation->test('a: B'), false);
+ $this->assertIdentical($expectation->test(' a : A '), true);
+ $this->assertIdentical($expectation->test(' a : AB '), true);
+ }
+
+ function testCaseInsensitivePatternMatching() {
+ $expectation = new HttpHeaderPatternExpectation('a', '/A/i');
+ $this->assertIdentical($expectation->test('a: a'), true);
+ $this->assertIdentical($expectation->test('a: B'), false);
+ $this->assertIdentical($expectation->test(' a : A '), true);
+ $this->assertIdentical($expectation->test(' a : BAB '), true);
+ $this->assertIdentical($expectation->test(' a : bab '), true);
+ }
+
+ function testUnwantedHeader() {
+ $expectation = new HttpUnwantedHeaderExpectation('a');
+ $this->assertIdentical($expectation->test(''), true);
+ $this->assertIdentical($expectation->test('stuff'), true);
+ $this->assertIdentical($expectation->test('b: B'), true);
+ $this->assertIdentical($expectation->test('a: A'), false);
+ $this->assertIdentical($expectation->test('A: A'), false);
+ }
+
+ function testMultilineUnwantedSearch() {
+ $expectation = new HttpUnwantedHeaderExpectation('a');
+ $this->assertIdentical($expectation->test("aa:A\r\nb:B\r\nc:C"), true);
+ $this->assertIdentical($expectation->test("aa:A\r\na:A\r\nb:B"), false);
+ }
+ }
+
+ class TestOfTextExpectations extends UnitTestCase {
+
+ function testMatchingSubString() {
+ $expectation = new WantedTextExpectation('wanted');
+ $this->assertIdentical($expectation->test(''), false);
+ $this->assertIdentical($expectation->test('Wanted'), false);
+ $this->assertIdentical($expectation->test('wanted'), true);
+ $this->assertIdentical($expectation->test('the wanted text is here'), true);
+ }
+
+ function testNotMatchingSubString() {
+ $expectation = new UnwantedTextExpectation('wanted');
+ $this->assertIdentical($expectation->test(''), true);
+ $this->assertIdentical($expectation->test('Wanted'), true);
+ $this->assertIdentical($expectation->test('wanted'), false);
+ $this->assertIdentical($expectation->test('the wanted text is here'), false);
+ }
+ }
+?> \ No newline at end of file
diff --git a/tests/UnitTests/simpletest/test/xml_test.php b/tests/UnitTests/simpletest/test/xml_test.php
new file mode 100644
index 00000000..afb3faf9
--- /dev/null
+++ b/tests/UnitTests/simpletest/test/xml_test.php
@@ -0,0 +1,189 @@
+<?php
+ // $Id: xml_test.php,v 1.16 2004/09/24 22:55:38 lastcraft Exp $
+
+ require_once(dirname(__FILE__) . '/../xml.php');
+
+ Mock::generate('SimpleScorer');
+
+ if (! function_exists('xml_parser_create')) {
+ SimpleTestOptions::ignore('TestOfXmlStructureParsing');
+ SimpleTestOptions::ignore('TestOfXmlResultsParsing');
+ }
+
+ class TestOfNestingTags extends UnitTestCase {
+
+ function testGroupSize() {
+ $nesting = new NestingGroupTag(array('SIZE' => 2));
+ $this->assertEqual($nesting->getSize(), 2);
+ }
+ }
+
+ class TestOfXmlStructureParsing extends UnitTestCase {
+
+ function testValidXml() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectNever('paintGroupStart');
+ $listener->expectNever('paintGroupEnd');
+ $listener->expectNever('paintCaseStart');
+ $listener->expectNever('paintCaseEnd');
+ $parser = &new SimpleTestXmlParser($listener);
+ $this->assertTrue($parser->parse("<?xml version=\"1.0\"?>\n"));
+ $this->assertTrue($parser->parse("<run>\n"));
+ $this->assertTrue($parser->parse("</run>\n"));
+ }
+
+ function testEmptyGroup() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintGroupStart', array('a_group', 7));
+ $listener->expectOnce('paintGroupEnd', array('a_group'));
+ $parser = &new SimpleTestXmlParser($listener);
+ $parser->parse("<?xml version=\"1.0\"?>\n");
+ $parser->parse("<run>\n");
+ $this->assertTrue($parser->parse("<group size=\"7\">\n"));
+ $this->assertTrue($parser->parse("<name>a_group</name>\n"));
+ $this->assertTrue($parser->parse("</group>\n"));
+ $parser->parse("</run>\n");
+ $listener->tally();
+ }
+
+ function testEmptyCase() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintCaseStart', array('a_case'));
+ $listener->expectOnce('paintCaseEnd', array('a_case'));
+ $parser = &new SimpleTestXmlParser($listener);
+ $parser->parse("<?xml version=\"1.0\"?>\n");
+ $parser->parse("<run>\n");
+ $this->assertTrue($parser->parse("<case>\n"));
+ $this->assertTrue($parser->parse("<name>a_case</name>\n"));
+ $this->assertTrue($parser->parse("</case>\n"));
+ $parser->parse("</run>\n");
+ $listener->tally();
+ }
+
+ function testEmptyMethod() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintCaseStart', array('a_case'));
+ $listener->expectOnce('paintCaseEnd', array('a_case'));
+ $listener->expectOnce('paintMethodStart', array('a_method'));
+ $listener->expectOnce('paintMethodEnd', array('a_method'));
+ $parser = &new SimpleTestXmlParser($listener);
+ $parser->parse("<?xml version=\"1.0\"?>\n");
+ $parser->parse("<run>\n");
+ $parser->parse("<case>\n");
+ $parser->parse("<name>a_case</name>\n");
+ $this->assertTrue($parser->parse("<test>\n"));
+ $this->assertTrue($parser->parse("<name>a_method</name>\n"));
+ $this->assertTrue($parser->parse("</test>\n"));
+ $parser->parse("</case>\n");
+ $parser->parse("</run>\n");
+ $listener->tally();
+ }
+
+ function testNestedGroup() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectArgumentsAt(0, 'paintGroupStart', array('a_group', 7));
+ $listener->expectArgumentsAt(1, 'paintGroupStart', array('b_group', 3));
+ $listener->expectCallCount('paintGroupStart', 2);
+ $listener->expectArgumentsAt(0, 'paintGroupEnd', array('b_group'));
+ $listener->expectArgumentsAt(1, 'paintGroupEnd', array('a_group'));
+ $listener->expectCallCount('paintGroupEnd', 2);
+ $parser = &new SimpleTestXmlParser($listener);
+ $parser->parse("<?xml version=\"1.0\"?>\n");
+ $parser->parse("<run>\n");
+ $this->assertTrue($parser->parse("<group size=\"7\">\n"));
+ $this->assertTrue($parser->parse("<name>a_group</name>\n"));
+ $this->assertTrue($parser->parse("<group size=\"3\">\n"));
+ $this->assertTrue($parser->parse("<name>b_group</name>\n"));
+ $this->assertTrue($parser->parse("</group>\n"));
+ $this->assertTrue($parser->parse("</group>\n"));
+ $parser->parse("</run>\n");
+ $listener->tally();
+ }
+ }
+
+ class AnyOldSignal {
+ var $stuff = true;
+ }
+
+ class TestOfXmlResultsParsing extends UnitTestCase {
+
+ function sendValidStart(&$parser) {
+ $parser->parse("<?xml version=\"1.0\"?>\n");
+ $parser->parse("<run>\n");
+ $parser->parse("<case>\n");
+ $parser->parse("<name>a_case</name>\n");
+ $parser->parse("<test>\n");
+ $parser->parse("<name>a_method</name>\n");
+ }
+
+ function sendValidEnd(&$parser) {
+ $parser->parse("</test>\n");
+ $parser->parse("</case>\n");
+ $parser->parse("</run>\n");
+ }
+
+ function testPass() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintPass', array('a_message'));
+ $parser = &new SimpleTestXmlParser($listener);
+ $this->sendValidStart($parser);
+ $this->assertTrue($parser->parse("<pass>a_message</pass>\n"));
+ $this->sendValidEnd($parser);
+ $listener->tally();
+ }
+
+ function testFail() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintFail', array('a_message'));
+ $parser = &new SimpleTestXmlParser($listener);
+ $this->sendValidStart($parser);
+ $this->assertTrue($parser->parse("<fail>a_message</fail>\n"));
+ $this->sendValidEnd($parser);
+ $listener->tally();
+ }
+
+ function testException() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintException', array('a_message'));
+ $parser = &new SimpleTestXmlParser($listener);
+ $this->sendValidStart($parser);
+ $this->assertTrue($parser->parse("<exception>a_message</exception>\n"));
+ $this->sendValidEnd($parser);
+ $listener->tally();
+ }
+
+ function testSignal() {
+ $signal = new AnyOldSignal();
+ $signal->stuff = "Hello";
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintSignal', array('a_signal', $signal));
+ $parser = &new SimpleTestXmlParser($listener);
+ $this->sendValidStart($parser);
+ $this->assertTrue($parser->parse(
+ "<signal type=\"a_signal\"><![CDATA[" .
+ serialize($signal) . "]]></signal>\n"));
+ $this->sendValidEnd($parser);
+ $listener->tally();
+ }
+
+ function testMessage() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintMessage', array('a_message'));
+ $parser = &new SimpleTestXmlParser($listener);
+ $this->sendValidStart($parser);
+ $this->assertTrue($parser->parse("<message>a_message</message>\n"));
+ $this->sendValidEnd($parser);
+ $listener->tally();
+ }
+
+ function testFormattedMessage() {
+ $listener = &new MockSimpleScorer($this);
+ $listener->expectOnce('paintFormattedMessage', array("\na\tmessage\n"));
+ $parser = &new SimpleTestXmlParser($listener);
+ $this->sendValidStart($parser);
+ $this->assertTrue($parser->parse("<formatted><![CDATA[\na\tmessage\n]]></formatted>\n"));
+ $this->sendValidEnd($parser);
+ $listener->tally();
+ }
+ }
+?> \ No newline at end of file