Skip to content

Instantly share code, notes, and snippets.

@jm42
Created December 20, 2015 21:10
Show Gist options
  • Save jm42/e1eba6add7c3b57fe334 to your computer and use it in GitHub Desktop.
Save jm42/e1eba6add7c3b57fe334 to your computer and use it in GitHub Desktop.
<?php
use App\Config\Configurator;
use App\Config\ConflictException;
use App\HTTP\Message;
use App\HTTP\Request;
use App\HTTP\Response;
class ConfiguratorTest extends \PHPUnit_Framework_TestCase
{
public function testEmpty()
{
$this->assertCount(0, new Configurator);
}
public function testInsert()
{
$config = new Configurator;
$config->insert('/', 'foo', 'foobar');
$this->assertCount(1, $config);
$config->insert('/', 'bar', 'barbaz');
$this->assertCount(2, $config);
}
public function testResolveIsIterator()
{
$this->assertInstanceOf('Iterator', (new Configurator)->resolve());
}
public function testResolveEmpty()
{
$config = new Configurator;
$result = $config->resolve();
$this->assertSame([], iterator_to_array($result));
}
/**
* @expectedException Aix\Config\ConflictException
*/
public function testResolveWithSamePathConflicts()
{
$config = new Configurator;
$config->insert('/', 'foo', 'bar');
$config->insert('/', 'foo', 'baz');
foreach ($config->resolve() as $key => $value) {
// Nothing
}
}
/**
* @expectedException Aix\Config\ConflictException
*/
public function testResolveWithoutBasePathConflicts()
{
$config = new Configurator;
$config->insert('/1', 'foo', 'bar');
$config->insert('/2', 'foo', 'baz');
foreach ($config->resolve() as $key => $value) {
// Nothing
}
}
public function testResolveWithDeeperPathOverrides()
{
$config = new Configurator;
$config->insert('/', 'foobar', '/');
$config->insert('/1', 'foobar', '/1');
$config->insert('/1/2', 'foobar', '/1/2');
$result = $config->resolve();
$this->assertSame(['foobar' => '/1/2'], iterator_to_array($result));
}
public function testResolveMultipleTimes()
{
$config = new Configurator;
$config->insert('/', 'bar', 'bar');
$result = $config->resolve();
$this->assertSame(['bar' => 'bar'], iterator_to_array($result));
$config->insert('/', 'baz', 'baz');
$result = $config->resolve();
$this->assertSame(['baz' => 'baz'], iterator_to_array($result));
}
public function testDispatchWillEmpty()
{
$config = new Configurator;
$config->insert('/', 'foo', 'foo');
$config->insert('/', 'bar', 'bar');
foreach ($config->resolve() as $key => $value) {
// Nothing
}
$this->assertCount(0, $config);
}
public function testPriority()
{
$config = new Configurator;
$config->insert('/' , 'name' , 'c(name)=aix' , 4);
$config->insert('/' , 'version', 'c(version)=0.1' , 4);
$config->insert('/' , 'home' , 'r(home)=The_Homepage', 2);
$config->insert('/' , 'about' , 'r(about)=About' , 2);
$config->insert('/mod', 'about' , 'r(about)=About_2' , 2);
$config->insert('/mod', 'version', 'c(version)=0.2' , 4);
$val = $config->resolve();
$exp = [
'version' => 'c(version)=0.2',
'name' => 'c(name)=aix',
'about' => 'r(about)=About_2',
'home' => 'r(home)=The_Homepage',
];
$this->assertSame($exp, iterator_to_array($val));
}
public function testMergeEmpty()
{
$config = new Configurator;
$config->merge(new Configurator);
$this->assertCount(0, $config);
}
public function testMergeSelf()
{
$config = new Configurator;
$config->insert('/', 'foo', 'bar');
$config->merge($config);
$this->assertCount(2, $config);
}
public function testMergeWithDiffPathOverrides()
{
$config1 = new Configurator;
$config1->insert('/1', 'foo', '1');
$config0 = new Configurator;
$config0->insert('/', 'foo', '0');
$config0->merge($config1);
$result = $config0->resolve();
$this->assertSame(['foo' => '1'], iterator_to_array($result));
}
}
class MessageTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$msg = new Message;
$this->assertAttributeInternalType('array', 'headers', $msg);
$this->assertAttributeInternalType('null', 'body', $msg);
}
public function testHeader()
{
$msg = new Message(['Location' => '/']);
$this->assertTrue($msg->hasHeader('Location'));
$this->assertSame('/', $msg->getHeader('Location'));
$this->assertSame(['Location'], $msg->getHeaders());
}
public function testHeaderNotFound()
{
$msg = new Message;
$this->assertFalse($msg->hasHeader('Location'));
$this->assertNull($msg->getHeader('Location'));
$this->assertSame([], $msg->getHeaders());
}
public function testBody()
{
$msg = new Message([], 'foobar');
$this->assertAttributeInternalType('resource', 'body', $msg);
$this->assertEquals('foobar', stream_get_contents($msg->getBody()));
}
public function testBodyWithStream()
{
$body = fopen('php://memory', 'r+');
fwrite($body, 'foobar');
rewind($body);
$msg = new Message([], $body);
$this->assertAttributeInternalType('resource', 'body', $msg);
$this->assertEquals('foobar', stream_get_contents($msg->getBody()));
}
}
class RequestTest extends \PHPUnit_Framework_TestCase
{
public function testConstrcutor()
{
$req = new Request('GET', '/');
$this->assertSame('GET', $req->getMethod());
$this->assertSame('/', $req->getTarget());
}
}
class ResponseTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$res = new Response;
$this->assertSame(200, $res->getStatusCode());
$this->assertSame('OK', $res->getReasonPhrase());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment