Permalink
Browse files

More GetObject work, and a bunch of other tests to increase coverage

  • Loading branch information...
troydavisson committed Jun 9, 2014
1 parent 6bf259c commit eff81124a6c222d45a87a69ba0a276dd84a15357
@@ -1,3 +1,4 @@
vendor/
composer.lock
.idea
coverage/
@@ -175,8 +175,20 @@ public function setFromHeader($name, $value)
}
}
/**
* @return $this
*/
public function downloadFromURL()
{
return file_get_contents($this->getLocation());
$this->setContent(file_get_contents($this->getLocation()));
return $this;
}
/**
* @return int
*/
public function getSize()
{
return strlen($this->getContent());
}
}
@@ -83,7 +83,7 @@ public function parse(ResponseInterface $response)
$header = "Content-Type";
}
$headers[$header] = $value;
$headers[$header] = [$value];
}
}
if ($on_body == true) {
@@ -11,13 +11,13 @@ public function parse(ResponseInterface $response)
$obj = new Object;
$obj->setContent(($response->getBody()) ? $response->getBody()->__toString() : null);
$obj->setContentDescription(\array_get($headers, 'Content-Description'));
$obj->setContentSubDescription(\array_get($headers, 'Content-Sub-Description'));
$obj->setContentId(\array_get($headers, 'Content-ID'));
$obj->setObjectId(\array_get($headers, 'Object-ID'));
$obj->setContentType(\array_get($headers, 'Content-Type'));
$obj->setLocation(\array_get($headers, 'Location'));
$obj->setMimeVersion(\array_get($headers, 'MIME-Version'));
$obj->setContentDescription(\array_get($headers, 'Content-Description', [null])[0]);
$obj->setContentSubDescription(\array_get($headers, 'Content-Sub-Description', [null])[0]);
$obj->setContentId(\array_get($headers, 'Content-ID', [null])[0]);
$obj->setObjectId(\array_get($headers, 'Object-ID', [null])[0]);
$obj->setContentType(\array_get($headers, 'Content-Type', [null])[0]);
$obj->setLocation(\array_get($headers, 'Location', [null])[0]);
$obj->setMimeVersion(\array_get($headers, 'MIME-Version', [null])[0]);
return $obj;
}
@@ -13,4 +13,32 @@ public function it_tracks()
$this->assertNotNull($cpb->get('login'));
$this->assertNull($cpb->get('test'));
}
/** @test **/
public function it_barfs_when_not_given_enough_information_to_build_absolute_urls()
{
$this->setExpectedException('InvalidArgumentException');
$cpb = new Capabilities;
$cpb->add('Login', '/rets/Login');
}
/** @test **/
public function it_can_build_absolute_urls_from_relative_ones()
{
$cpb = new Capabilities;
$cpb->add('Login', 'http://www.google.com/login');
$cpb->add('Search', '/search');
$this->assertSame('http://www.google.com:80/search', $cpb->get('Search'));
}
/** @test **/
public function it_preserves_explicity_ports()
{
$cpb = new Capabilities;
$cpb->add('Login', 'http://www.google.com:8080/login');
$cpb->add('Search', '/search');
$this->assertSame('http://www.google.com:8080/search', $cpb->get('Search'));
}
}
@@ -3,7 +3,7 @@
use PHRETS\Configuration;
class ConfigurationTest extends PHPUnit_Framework_TestCase {
/** @test **/
public function it_does_the_basics()
{
@@ -46,4 +46,38 @@ public function it_loads_default_rets_version()
$this->assertInstanceOf('PHRETS\\Versions\\RETSVersion', $config->getRetsVersion());
$this->assertTrue($config->getRetsVersion()->is1_5());
}
/** @test **/
public function it_handles_versions_correctly()
{
$config = new Configuration;
$config->setRetsVersion('1.7.2');
$this->assertInstanceOf('PHRETS\\Versions\\RETSVersion', $config->getRetsVersion());
}
/** @test **/
public function it_handles_user_agents()
{
$config = new Configuration;
$config->setUserAgent('PHRETS/2.0');
$this->assertSame('PHRETS/2.0', $config->getUserAgent());
}
/** @test **/
public function it_handles_ua_passwords()
{
$config = new Configuration;
$config->setUserAgent('PHRETS/2.0');
$config->setUserAgentPassword('test12345');
$this->assertSame('PHRETS/2.0', $config->getUserAgent());
$this->assertSame('test12345', $config->getUserAgentPassword());
}
/** @test **/
public function it_tracks_options()
{
$config = new Configuration;
$config->setOption('param', true);
$this->assertTrue($config->readOption('param'));
}
}
@@ -0,0 +1 @@
"\r\n--FLEXeTNY6TFTGAwV1agjJsFyFogbnfoS1dm6y489g08F2TjwZWzQEW\r\nContent-Type: image\/jpeg\r\nContent-ID: 123456789\r\nObject-ID: 1\r\nLocation: http:\/\/url1.jpg\r\nContent-Description: Exterior Main View\r\nPreferred: 1\r\n\r\n\r\n--FLEXeTNY6TFTGAwV1agjJsFyFogbnfoS1dm6y489g08F2TjwZWzQEW\r\nContent-Type: image\/jpeg\r\nContent-ID: 123456789\r\nObject-ID: 2\r\nLocation: http:\/\/url2.jpg\r\nContent-Description: Exterior Back\r\n\r\n\r\n--FLEXeTNY6TFTGAwV1agjJsFyFogbnfoS1dm6y489g08F2TjwZWzQEW\r\nContent-Type: image\/jpeg\r\nContent-ID: 123456789\r\nObject-ID: 3\r\nLocation: http:\/\/url3.jpg\r\nContent-Description: Foyer\r\n\r\n\r\n--FLEXeTNY6TFTGAwV1agjJsFyFogbnfoS1dm6y489g08F2TjwZWzQEW\r\nContent-Type: image\/jpeg\r\nContent-ID: 123456789\r\nObject-ID: 4\r\nLocation: http:\/\/url4.jpg\r\nContent-Description: Living Room\r\n\r\n\r\n--FLEXeTNY6TFTGAwV1agjJsFyFogbnfoS1dm6y489g08F2TjwZWzQEW\r\nContent-Type: image\/jpeg\r\nContent-ID: 123456789\r\nObject-ID: 5\r\nLocation: http:\/\/url5.jpg\r\nContent-Description: Dining Room\r\n\r\n\r\n--FLEXeTNY6TFTGAwV1agjJsFyFogbnfoS1dm6y489g08F2TjwZWzQEW--\r\n"
@@ -16,6 +16,12 @@ public function it_combines_multiple_from_string()
$this->assertEquals(['12345:1','67890:1'], GetObject::ids('12345,67890', 1));
}
/** @test **/
public function it_combines_multiple_from_colon_string()
{
$this->assertEquals(['12345:1','67890:1'], GetObject::ids('12345:67890', 1));
}
/** @test **/
public function it_combines_multiple_from_array()
{
@@ -0,0 +1,18 @@
<?php
use PHRETS\Models\Bulletin;
class BulletinTest extends PHPUnit_Framework_TestCase {
/** @test **/
public function it_holds()
{
$this->assertSame('Hello World', (new Bulletin)->setBody('Hello World')->getBody());
}
/** @test **/
public function it_turns_itself_into_a_string()
{
$this->assertSame('Hello World', (string)(new Bulletin)->setBody('Hello World'));
}
}
@@ -0,0 +1,60 @@
<?php
use PHRETS\Models\Object;
class ObjectTest extends PHPUnit_Framework_TestCase {
/** @test **/
public function it_holds()
{
$o = new Object;
$o->setContent('Test Content');
$this->assertSame('Test Content', $o->getContent());
}
/** @test **/
public function it_returns_a_size()
{
$o = new Object;
$o->setContent('Hello');
$this->assertSame(5, $o->getSize());
}
/** @test **/
public function it_makes_from_headers()
{
$headers = [
'Content-Type' => 'image/jpeg',
'Content-ID' => '12345678',
'Object-ID' => '1',
'Location' => 'http://blah',
'Content-Description' => 'Main description',
'Content-Sub-Description' => 'Sub description',
'MIME-Version' => 'Mime Version',
];
$o = new Object;
foreach ($headers as $k => $v) {
$o->setFromHeader($k, $v);
}
$this->assertSame('image/jpeg', $o->getContentType());
$this->assertSame('12345678', $o->getContentId());
$this->assertSame('1', $o->getObjectId());
$this->assertSame('http://blah', $o->getLocation());
$this->assertSame('Main description', $o->getContentDescription());
$this->assertSame('Sub description', $o->getContentSubDescription());
$this->assertSame('Mime Version', $o->getMimeVersion());
}
/** @test **/
public function it_fetches_from_remote()
{
$o = new Object;
$o->setLocation('http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css');
$this->assertRegExp('/Twitter/', $o->downloadFromURL()->getContent());
}
}
@@ -0,0 +1,96 @@
<?php
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use PHRETS\Parsers\GetObject\Multiple;
class MultipleTest extends PHPUnit_Framework_TestCase
{
/** @test * */
public function it_breaks_things_apart()
{
$headers = [
'Server' => [
0 => 'Apache-Coyote/1.1',
],
'Cache-Control' => [
0 => 'private',
],
'RETS-Version' => [
0 => 'RETS/1.5',
],
'MIME-Version' => [
0 => '1.0',
],
'Content-Type' => [
0 => 'multipart/parallel; boundary="FLEXeTNY6TFTGAwV1agjJsFyFogbnfoS1dm6y489g08F2TjwZWzQEW"',
],
'Content-Length' => [
0 => '1249',
],
'Date' => array(
0 => 'Mon, 09 Jun 2014 00:10:51 GMT',
),
];
$body = json_decode(file_get_contents('tests/Fixtures/GetObject/Multiple1.txt', true));
$parser = new Multiple;
$collection = $parser->parse(new Response(200, $headers, Stream::factory($body)));
$this->assertSame(5, $collection->count());
/** @var PHRETS\Models\Object $obj */
$obj = $collection->first();
$this->assertSame('Exterior Main View', $obj->getContentDescription());
$this->assertSame('http://url1.jpg', $obj->getLocation());
}
/** @test **/
public function it_handles_empty_bodies()
{
$parser = new Multiple;
$collection = $parser->parse(new Response(200, [], null));
$this->assertInstanceOf('Illuminate\\Support\\Collection', $collection);
}
/** @test **/
public function it_handles_unquoted_boundaries()
{
$headers = [
'Server' => [
0 => 'Apache-Coyote/1.1',
],
'Cache-Control' => [
0 => 'private',
],
'RETS-Version' => [
0 => 'RETS/1.5',
],
'MIME-Version' => [
0 => '1.0',
],
'Content-Type' => [
0 => 'multipart/parallel; boundary=FLEXeTNY6TFTGAwV1agjJsFyFogbnfoS1dm6y489g08F2TjwZWzQEW',
],
'Content-Length' => [
0 => '1249',
],
'Date' => array(
0 => 'Mon, 09 Jun 2014 00:10:51 GMT',
),
];
$body = json_decode(file_get_contents('tests/Fixtures/GetObject/Multiple1.txt', true));
$parser = new Multiple;
$collection = $parser->parse(new Response(200, $headers, Stream::factory($body)));
$this->assertSame(5, $collection->count());
/** @var PHRETS\Models\Object $obj */
$obj = $collection->first();
$this->assertSame('Exterior Main View', $obj->getContentDescription());
$this->assertSame('http://url1.jpg', $obj->getLocation());
}
}
@@ -0,0 +1,19 @@
<?php
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use PHRETS\Parsers\GetObject\Single;
class SingleTest extends PHPUnit_Framework_TestCase {
/** @test **/
public function it_understands_the_basics()
{
$parser = new Single;
$single = new Response(200, ['Content-Type' => 'text/plain'], Stream::factory('Test'));
$obj = $parser->parse($single);
$this->assertSame('Test', $obj->getContent());
$this->assertSame('text/plain', $obj->getContentType());
}
}

0 comments on commit eff8112

Please sign in to comment.