PHP Classes

File: Tests/GDM/Framework/Types/UrlTest.php

Recommend this page to a friend!
  Classes of Corey Sewell   GDM Types   Tests/GDM/Framework/Types/UrlTest.php   Download  
File: Tests/GDM/Framework/Types/UrlTest.php
Role: Unit test script
Content type: text/plain
Description: Auxiliary script
Class: GDM Types
Manipulate scalar data types as objects
Author: By
Last change: Update of Tests/GDM/Framework/Types/UrlTest.php
Date: 4 months ago
Size: 11,343 bytes
 

Contents

Class file image Download
<?php namespace GDM\Framework\Types; /** * Generated by PHPUnit_SkeletonGenerator on 2014-07-05 at 21:15:10. */ class UrlTest extends \PHPUnit_Framework_TestCase { /** * @var Url */ protected $object; protected $urlString = "http://www.example.com/foo/bar"; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->object = new Url($this->urlString); } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * @covers GDM\Framework\Types\Url::__get * @covers GDM\Framework\Types\Url::__set */ public function test__get() { $this->object->someparameter = "foobar"; $this->object->boolTrue = true; $this->object->boolFalse = false; $this->object->arrayTest = ['foo', 'bar']; $this->object->objectTest = (object) ['foo', 'bar']; $this->assertEquals("foobar", $this->object->someparameter); $this->assertEquals("true", $this->object->boolTrue); $this->assertEquals("false", $this->object->boolFalse); $this->assertEquals('a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}', $this->object->arrayTest); $this->assertEquals('O:8:"stdClass":2:{i:0;s:3:"foo";i:1;s:3:"bar";}', $this->object->objectTest); } /** * @covers GDM\Framework\Types\Url::__isset */ public function test__isset() { $this->object->someparameter = "foobar"; $this->assertTrue(isset($this->object->someparameter)); $this->assertFalse(isset($this->object->somenonexistantparameter)); } /** * @covers GDM\Framework\Types\Url::__unset */ public function test__unset() { $this->object->someparameter = "foobar"; $this->assertEquals("foobar", $this->object->someparameter); unset($this->object->someparameter); $this->assertNull($this->object->someparameter); } /** * @covers GDM\Framework\Types\Url::compareTo * @covers GDM\Framework\Types\Url::tidy */ public function testCompareTo() { $query = "?foo=bar"; $this->assertEquals(0, $this->object->compareTo($this->urlString)); $this->assertEquals(0, $this->object->compareTo(strtoupper($this->urlString), false)); $this->assertEquals(-1, $this->object->compareTo(strtoupper($this->urlString))); $this->assertEquals(0, $this->object->compareTo($this->urlString.$query, true, false)); $this->assertEquals(strlen($query), $this->object->compareTo($this->urlString.$query, true, true)); $this->assertEquals(0, $this->object->compareTo($this->urlString.$query, true, false)); $urlSpecial = "http://www.example.com/fo@o$ / b%a@r"; $this->assertEquals(0, $this->object->compareTo($urlSpecial, false, false, true)); $this->assertNotEquals(0, $this->object->compareTo($urlSpecial, false, false, false)); } /** * @covers GDM\Framework\Types\Url::decodePath * @covers GDM\Framework\Types\Url::parse * @covers GDM\Framework\Types\Url::clear */ public function testDecodePath() { $urlString = "http://www.example.com/foo/bar"; $segmentOne = 'foo bar&foo?bar%foo'; $segmentTwo = 'foo!bar@foo#bar$foo'; $segmentThree = 'foo>bar.foo"bar]foo'; $this->object->set($urlString."/".rawurlencode($segmentOne)."/".rawurlencode($segmentTwo)."/".rawurlencode($segmentThree)); $this->object->decodePath(); $this->assertEquals($urlString."/".$segmentOne."/".$segmentTwo."/".$segmentThree, (string) $this->object); } /** * @covers GDM\Framework\Types\Url::encodePath */ public function testEncodePath() { $urlString = "http://www.example.com/foo/bar"; $segmentOne = 'foo bar&foo=bar%foo'; $segmentTwo = 'foo!bar@foo*bar$foo'; $segmentThree = 'foo>bar.foo"bar]foo'; $this->object->set($urlString."/".$segmentOne."/".$segmentTwo."/".$segmentThree); $this->object->encodePath(); $this->assertEquals($urlString."/".rawurlencode($segmentOne)."/".rawurlencode($segmentTwo)."/".rawurlencode($segmentThree), (string) $this->object); } /** * @covers GDM\Framework\Types\Url::fromObject */ public function testFromObject() { $stringableClass = new stringableClass(); $this->assertEquals((string) $stringableClass, (string) $this->object->fromObject(new stringableClass())); $this->assertEmpty((string) $this->object->fromObject(new nonStringableClass())); } /** * @covers GDM\Framework\Types\Url::fromString */ public function testFromString() { $this->assertEquals($this->urlString, (string) $this->object->fromString($this->urlString)); } /** * @covers GDM\Framework\Types\Url::fromResource */ public function testFromResource() { $fh = tmpfile(); fwrite($fh, $this->urlString); fseek($fh, 0); $this->assertEquals($this->urlString, (string) $this->object->fromResource($fh)); fclose($fh); } /** * @covers GDM\Framework\Types\Url::get */ public function testGet() { $this->assertEquals($this->urlString, $this->object->get()); } /** * @covers GDM\Framework\Types\Url::offsetExists */ public function testOffsetExists() { $this->assertTrue($this->object->offsetExists(0)); $this->assertTrue($this->object->offsetExists(1)); $this->assertFalse($this->object->offsetExists(2)); $this->assertTrue(isset($this->object[0])); $this->assertTrue(isset($this->object[1])); $this->assertFalse(isset($this->object[2])); } /** * @covers GDM\Framework\Types\Url::offsetGet */ public function testOffsetGet() { $this->assertEquals('foo', $this->object->offsetGet(0)); $this->assertEquals('bar', $this->object->offsetGet(1)); $this->assertEquals('foo', $this->object[0]); $this->assertEquals('bar', $this->object[1]); } /** * @covers GDM\Framework\Types\Url::offsetSet */ public function testOffsetSet() { $this->object->offsetSet(0, "foobar"); $this->object[1] = "barfoo"; $this->assertEquals('foobar', $this->object->offsetGet(0)); $this->assertEquals('barfoo', $this->object[1]); $this->object[] = "last"; $this->assertEquals('last', $this->object->getSegment('last')); } /** * @covers GDM\Framework\Types\Url::offsetUnset */ public function testOffsetUnset() { $this->object->offsetSet(0, "foobar"); $this->object[1] = "barfoo"; $this->assertEquals('foobar', $this->object->offsetGet(0)); $this->assertEquals('barfoo', $this->object[1]); $this->object->offsetUnset(0); $this->assertFalse(isset($this->object[0])); unset($this->object[1]); $this->assertFalse(isset($this->object[1])); } /** * @covers GDM\Framework\Types\Url::getSegment */ public function testGetSegment() { $this->assertEquals('foo', $this->object->getSegment(0)); $this->assertEquals('bar', $this->object->getSegment(1)); $this->assertEquals('foo', $this->object->getSegment('first')); $this->assertEquals('bar', $this->object->getSegment('last')); $this->assertNull($this->object->getSegment(3)); } /** * @covers GDM\Framework\Types\Url::current * @covers GDM\Framework\Types\Url::serverVar * @covers GDM\Framework\Types\Url::currentQuery * @covers GDM\Framework\Types\Url::currentScheme * @covers GDM\Framework\Types\Url::currentHost * @covers GDM\Framework\Types\Url::currentPort * @covers GDM\Framework\Types\Url::currentPath */ public function testCurrent() { $this->assertEquals("/cli", (string) $this->object->current(true)); $reflectionClass = new \ReflectionClass($this->object); $reflectionProperty = $reflectionClass->getProperty('serverVars'); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($this->object, [ 'HTTPS' => false, 'HTTP_HOST' => 'www.example.com', 'SERVER_PORT' => 8080, 'SCRIPT_NAME' => '/foo/bar', 'QUERY_STRING' => 'foo=bar' ]); $this->assertEquals("http://www.example.com:8080/foo/bar?foo=bar", (string) $this->object->current(true)); $reflectionProperty->setValue($this->object, [ 'HTTPS' => false, 'HTTP_HOST' => 'www.example.com', 'SERVER_PORT' => 8080, 'REQUEST_URI' => '/foo/bar', 'QUERY_STRING' => 'foo=bar' ]); $this->assertEquals("http://www.example.com:8080/foo/bar?foo=bar", (string) $this->object->current(true)); } /** * @covers GDM\Framework\Types\Url::fetch */ public function testFetch() { $this->assertNotEmpty($this->object->set("http://www.gooole.com")->fetch(true)); } /** * @covers GDM\Framework\Types\Url::tidy */ public function testTidy() { $this->assertEquals("http://www.example.com/foo_Bar_and_barfoo?foo=bar", (string) $this->object->set("http://www.example.com/foo % Bar & barfoo?foo=bar")->tidy()); } /** * @covers GDM\Framework\Types\Url::toString */ public function testToString() { $this->assertEquals($this->urlString, $this->object->toString()); $this->object->set('http://foo:bar@www.example.com/foo_Bar_and_barfoo?foo=bar'); $this->assertEquals('http://foo:bar@www.example.com/foo_Bar_and_barfoo?foo=bar', $this->object->toString()); } /** * @covers GDM\Framework\Types\Url::asString */ public function testAsString() { $stribObj = $this->object->asString(); $this->assertInstanceOf('GDM\Framework\Types\String', $stribObj); $this->assertEquals($this->urlString, $stribObj->get()); } /** * @covers GDM\Framework\Types\Url::isHttps */ public function testIsHttps() { $this->assertFalse($this->object->isHttps()); $this->object->set(str_replace("http://", "https://", $this->urlString)); $this->assertTrue($this->object->isHttps()); } /** * @covers GDM\Framework\Types\Url::isFullUrl */ public function testIsFullUrl() { $this->assertTrue($this->object->isFullUrl()); $this->assertFalse($this->object->fromString("/foo/bar")->isFullUrl()); } } if (!class_exists('\GDM\Framework\Types\stringableClass')) { class stringableClass { public function __toString() { return "http://www.example.com/foo/bar"; } } } if (!class_exists('\GDM\Framework\Types\nonStringableClass')) { class nonStringableClass { } }