Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Zend\Http Request and Response refactoring:
Browse files Browse the repository at this point in the history
  - added tests for various objects
  - moved request line and response line into request and response object out of header object
Zend\Stdlib:
  - refactored Message and Parameters for generalized usage
  - added tests
Zend\Uri\UriFactory
  - fixed register scheme method to use proper property
  • Loading branch information
Ralph Schindler committed Aug 1, 2011
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function __toString()
);
}
$request .= "\r\n" . $this->getContent();

return $request;
}

public function fromString($string)
Expand Down
24 changes: 24 additions & 0 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,28 @@ public function offsetGet($name)
}
return null;
}

/**
* @param string $name
* @param mixed $default optional default value
* @return mixed
*/
public function get($name, $default = null)
{
if (isset($this[$name])) {
return parent::offsetGet($name);
}
return $default;
}

/**
* @param string $name
* @param mixed $value
* @return $this
*/
public function set($name, $value)
{
$this[$name] = $value;
return $this;
}
}
4 changes: 4 additions & 0 deletions src/ParametersDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public function toArray();

/* Allow serialization to query format; e.g., for PUT or POST requests */
public function toString();

public function get($name, $default = null);

public function set($name, $value);
}
76 changes: 76 additions & 0 deletions test/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* @namespace
*/
namespace ZendTest\Http;

use Zend\Stdlib\Message;

class MessageTest extends \PHPUnit_Framework_TestCase
{
public function testMessageCanSetAndGetContent()
{
$message = new Message();
$ret = $message->setContent('I can set content');
$this->assertInstanceOf('Zend\Stdlib\Message', $ret);
$this->assertEquals('I can set content', $message->getContent());
}

public function testMessageCanSetAndGetMetadataKeyAsString()
{
$message = new Message();
$ret = $message->setMetadata('foo', 'bar');
$this->assertInstanceOf('Zend\Stdlib\Message', $ret);
$this->assertEquals('bar', $message->getMetadata('foo'));
$this->assertEquals(array('foo' => 'bar'), $message->getMetadata());
}

public function testMessageCanSetAndGetMetadataKeyAsArray()
{
$message = new Message();
$ret = $message->setMetadata(array('foo' => 'bar'));
$this->assertInstanceOf('Zend\Stdlib\Message', $ret);
$this->assertEquals('bar', $message->getMetadata('foo'));
}

public function testMessageGetMetadataWillUseDefaultValueIfNoneExist()
{
$message = new Message();
$this->assertEquals('bar', $message->getMetadata('foo', 'bar'));
}

public function testMessageThrowsExceptionOnInvalidKeyForMetadataSet()
{
$message = new Message();

$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
$message->setMetadata(new \stdClass());
}

public function testMessageThrowsExceptionOnInvalidKeyForMetadataGet()
{
$message = new Message();

$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
$message->getMetadata(new \stdClass());
}

public function testMessageToStringWorks()
{
$message = new Message();
$message->setMetadata(array('Foo' => 'bar', 'One' => 'Two'));
$message->setContent('This is my content');
$expected = "Foo: bar\r\nOne: Two\r\n\r\nThis is my content";
$this->assertEquals($expected, $message->__toString());
}

public function testMessageThrowsExceptionOnFromString()
{
$message = new Message();

$this->setExpectedException('Zend\Stdlib\Exception\DomainException');
$message->fromString('some string');
}

}
58 changes: 58 additions & 0 deletions test/ParametersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace ZendTest\Stdlib;

use Zend\Stdlib\Parameters;

class ParametersTest extends \PHPUnit_Framework_TestCase
{

public function testParametersConstructionAndClassStructure()
{
$parameters = new Parameters();
$this->assertInstanceOf('Zend\Stdlib\ParametersDescription', $parameters);
$this->assertInstanceOf('ArrayObject', $parameters);
$this->assertInstanceOf('ArrayAccess', $parameters);
$this->assertInstanceOf('Countable', $parameters);
$this->assertInstanceOf('Serializable', $parameters);
$this->assertInstanceOf('Traversable', $parameters);
}

public function testParametersPersistNameAndValues()
{
$parameters = new Parameters(array('foo' => 'bar'));
$this->assertEquals('bar', $parameters['foo']);
$this->assertEquals('bar', $parameters->foo);
$parameters->offsetSet('baz', 5);
$this->assertEquals(5, $parameters->baz);

$parameters->fromArray(array('bar' => 'foo'));
$this->assertEquals('foo', $parameters->bar);

$parameters->fromString('bar=foo&five=5');
$this->assertEquals('foo', $parameters->bar);
$this->assertEquals('5', $parameters->five);
$this->assertEquals(array('bar' => 'foo', 'five' => '5'), $parameters->toArray());
$this->assertEquals('bar=foo&five=5', $parameters->toString());

$parameters->fromArray(array());
$parameters->set('foof', 'barf');
$this->assertEquals('barf', $parameters->get('foof'));
$this->assertEquals('barf', $parameters->foof);

}

public function testParametersOffsetgetReturnsNullIfNonexistentKeyIsProvided()
{
$parameters = new Parameters;
$this->assertNull($parameters->foo);
}

public function testParametersGetReturnsDefaultValueIfNonExistent()
{
$parameters = new Parameters();

$this->assertEquals(5, $parameters->get('nonExistentProp', 5));
}

}

0 comments on commit 9048c2d

Please sign in to comment.