Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kiltec/1252989 to your computer and use it in GitHub Desktop.
Save kiltec/1252989 to your computer and use it in GitHub Desktop.
Coding Kata of After Work Hacking: URL Splitting, http://sites.google.com/site/tddproblems/all-problems-1/URL-splitting
<?php
class Url {
public $protocol = "";
public $domain = "";
public $path = "";
public function __construct($url_string){
$this->protocol = $this->_extractProtocol($url_string);
$this->domain = $this->_extractDomain($url_string);
$this->path = $this->_extractPath($url_string);
}
private function _extractProtocol($url_string) {
preg_match("~^(\w*)://.*~", $url_string, $matches);
$protocol = '';
if(isset($matches[1])) {
$protocol = $matches[1];
}
return $protocol;
}
private function _extractDomain($url_string){
preg_match("~^(\w*://)?([^:/]+)~", $url_string, $matches);
$domain = $matches[2];
return $domain;
}
private function _extractPath($url_string) {
preg_match("~^(\w*://)?([^/]+)[/]?(.*)~", $url_string, $matches);
$path = $matches[3];
return $path;
}
}
<?php
include "SplitUrl.php";
class UrlSplittingTest extends PHPUnit_Framework_TestCase {
public function testUrlWithNoProtocol() {
$url = "www.noprotocol.com";
$expected = "";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->protocol);
}
public function testGetProtocolFromUrlWithProtocol(){
$url = "http://google.de";
$expected = "http";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->protocol);
}
public function testGetProtocolFromUrlWithoutProtocolAndWithPort() {
$url = "google.de:8080";
$expected = "";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->protocol);
}
public function testGetDomainNameFromUrlWithoutProtocolAndWithoutPath() {
$url = "www.google.de";
$expected = "www.google.de";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->domain);
}
public function testDomainNameFromUrlWithProtocolAndWithoutPath() {
$url = "http://www.google.de";
$expected = "www.google.de";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->domain);
}
public function testDomainNameFromUrlWithProtocolAndWithPath() {
$url = "http://www.google.de/test";
$expected = "www.google.de";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->domain);
}
public function testDomainNameFromUrlWithProtocolAndWithPathAndWithPort() {
$url = "http://www.google.de:80/test";
$expected = "www.google.de";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->domain);
}
public function testPathFromUrlWithoutPath() {
$url = "http://www.google.de";
$expected = "";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->path);
}
public function testPathFromUrlWithPath() {
$url = "http://www.google.de/test";
$expected = "test";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->path);
}
public function testPathFromUrlWithMultiLevelPath() {
$url = "http://www.google.de/test/test2";
$expected = "test/test2";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->path);
}
public function testPathFromUrlWithMultiLevelPathAndPort() {
$url = "http://www.google.de:80/test/test2";
$expected = "test/test2";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->path);
}
public function testPathFromUrlWithoutProtocol() {
$url = "www.google.de/test/test2";
$expected = "test/test2";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->path);
}
public function testProtocolWhenFTP() {
$url = "ftp://www.google.de/test/test2";
$expected = "ftp";
$url_object = new Url($url);
$this->assertEquals($expected, $url_object->protocol);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment