-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7ba8d1
commit 0d45141
Showing
5 changed files
with
338 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
<?php | ||
|
||
namespace MRussell\REST\Tests\Endpoint; | ||
|
||
use MRussell\REST\Endpoint\JSON\ModelEndpoint; | ||
use MRussell\REST\Tests\Stubs\Endpoint\CollectionEndpointWithModel; | ||
use MRussell\REST\Tests\Stubs\Endpoint\CollectionEndpoint; | ||
|
||
|
||
/** | ||
* Class AbstractCollectionEndpointTest | ||
* @package MRussell\REST\Tests\Endpoint | ||
* @coversDefaultClass MRussell\REST\Endpoint\Abstracts\AbstractCollectionEndpoint | ||
* @group AbstractCollectionEndpointTest | ||
*/ | ||
class AbstractCollectionEndpointTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected static $_REFLECTED_CLASS = 'MRussell\REST\Tests\Stubs\Endpoint\CollectionEndpoint'; | ||
|
||
protected $collection = array( | ||
'abc123' => array( | ||
'id' => 'abc123', | ||
'name' => 'foo', | ||
'foo' => 'bar' | ||
), | ||
'efg234' => array( | ||
'id' => 'efg234', | ||
'name' => 'test', | ||
'foo' => '' | ||
) | ||
); | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
//Add Setup for static properties here | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
//Add Tear Down for static properties here | ||
} | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* @covers ::__construct | ||
* @covers ::setModelEndpoint | ||
*/ | ||
public function testConstructor(){ | ||
$Endpoint = new CollectionEndpoint(); | ||
$Class = new \ReflectionClass(static::$_REFLECTED_CLASS); | ||
$model = $Class->getProperty('model'); | ||
$model->setAccessible(TRUE); | ||
$this->assertEmpty($model->getValue($Endpoint)); | ||
|
||
$Endpoint = new CollectionEndpointWithModel(); | ||
$Class = new \ReflectionClass(static::$_REFLECTED_CLASS.'WithModel'); | ||
$model = $Class->getProperty('model'); | ||
$model->setAccessible(TRUE); | ||
$this->assertEquals('MRussell\\REST\\Tests\\Stubs\\Endpoint\\ModelEndpoint',$model->getValue($Endpoint)); | ||
} | ||
|
||
/** | ||
* @covers ::offsetSet | ||
* @covers ::offsetExists | ||
* @covers ::offsetUnset | ||
* @covers ::offsetGet | ||
* @covers ::update | ||
* @covers ::asArray | ||
* @covers ::get | ||
* @covers ::buildModel | ||
* @covers ::clear | ||
* @covers ::reset | ||
*/ | ||
public function testDataAccess(){ | ||
$Collection = new CollectionEndpointWithModel(); | ||
$Collection[] = array( | ||
'foo' => 'bar', | ||
'abc' => 123 | ||
); | ||
$this->assertEquals(array(array( | ||
'foo' => 'bar', | ||
'abc' => 123 | ||
)),$Collection->asArray()); | ||
$this->assertEquals(array( | ||
'foo' => 'bar', | ||
'abc' => 123 | ||
),$Collection[0]); | ||
$this->assertEquals(TRUE,isset($Collection[0])); | ||
unset($Collection[0]); | ||
$this->assertEquals(FALSE,isset($Collection[0])); | ||
$this->assertEquals(array(),$Collection->asArray()); | ||
$this->assertEquals($Collection,$Collection->update($this->collection)); | ||
$this->assertEquals($this->collection,$Collection->asArray()); | ||
$this->assertEquals(array( | ||
'id' => 'abc123', | ||
'name' => 'foo', | ||
'foo' => 'bar' | ||
),$Collection['abc123']); | ||
$Collection['k2r2d2'] = array( | ||
'id' => 'k2r2d2', | ||
'name' => 'Rogue One', | ||
'foo' => 'bar' | ||
); | ||
$this->assertEquals(array( | ||
'id' => 'k2r2d2', | ||
'name' => 'Rogue One', | ||
'foo' => 'bar' | ||
),$Collection['k2r2d2']); | ||
$Model = $Collection->get('abc123'); | ||
$this->assertEquals(TRUE,is_object($Model)); | ||
$this->assertEquals('bar',$Model->get('foo')); | ||
$this->assertEquals($Collection,$Collection->reset()); | ||
$this->assertEquals(array(),$Collection->asArray()); | ||
$this->assertEquals($Collection,$Collection->update($this->collection)); | ||
$this->assertEquals($this->collection,$Collection->asArray()); | ||
$this->assertEquals($Collection,$Collection->reset()); | ||
$this->assertEquals(array(),$Collection->asArray()); | ||
|
||
$Collection = new CollectionEndpoint(); | ||
$this->assertEquals($Collection,$Collection->update($this->collection)); | ||
$Model = $Collection->get('abc123'); | ||
$this->assertEquals(TRUE,is_array($Model)); | ||
$this->assertEquals(array( | ||
'id' => 'abc123', | ||
'name' => 'foo', | ||
'foo' => 'bar' | ||
),$Model); | ||
} | ||
|
||
/** | ||
* @covers ::setModelEndpoint | ||
*/ | ||
public function testSetModelEndpoint(){ | ||
$Collection = new CollectionEndpointWithModel(); | ||
$Collection->setModelEndpoint(new ModelEndpoint()); | ||
$Class = new \ReflectionClass(static::$_REFLECTED_CLASS); | ||
$model = $Class->getProperty('model'); | ||
$model->setAccessible(TRUE); | ||
$this->assertEquals('MRussell\\REST\\Endpoint\\JSON\\ModelEndpoint',$model->getValue($Collection)); | ||
$Collection->setModelEndpoint('MRussell\\REST\\Tests\\Stubs\\Endpoint\\ModelEndpoint'); | ||
$this->assertEquals('MRussell\\REST\\Tests\\Stubs\\Endpoint\\ModelEndpoint',$model->getValue($Collection)); | ||
} | ||
|
||
/** | ||
* @depends testSetModelEndpoint | ||
* @covers ::setModelEndpoint | ||
* @expectedException MRussell\REST\Exception\Endpoint\UnknownEndpoint | ||
*/ | ||
public function testUnknownEndpoint(){ | ||
$Collection = new CollectionEndpointWithModel(); | ||
$Collection->setModelEndpoint('test'); | ||
} | ||
|
||
/** | ||
* @covers ::getEndpointUrl | ||
*/ | ||
public function testGetEndpointUrl(){ | ||
$Collection = new CollectionEndpointWithModel(); | ||
$this->assertEquals("account",$Collection->getEndPointUrl()); | ||
$this->assertEquals($Collection,$Collection->setProperty('url','accounts')); | ||
$this->assertEquals("accounts",$Collection->getEndPointUrl()); | ||
$this->assertEquals($Collection,$Collection->setBaseUrl('localhost')); | ||
$this->assertEquals("localhost/accounts",$Collection->getEndPointUrl(TRUE)); | ||
$this->assertEquals($Collection,$Collection->setProperty('url','')); | ||
$this->assertEquals("localhost/account",$Collection->getEndPointUrl(TRUE)); | ||
} | ||
|
||
/** | ||
* @covers ::fetch | ||
*/ | ||
public function testFetch(){ | ||
$Collection = new \MRussell\REST\Endpoint\JSON\CollectionEndpoint(); | ||
$Collection->setBaseUrl('localhost'); | ||
$Collection->setProperty('url','foo'); | ||
$Collection->fetch(); | ||
$props = $Collection->getProperties(); | ||
$this->assertEquals('GET',$props['httpMethod']); | ||
} | ||
|
||
/** | ||
* @covers ::configureResponse | ||
* @covers ::updateCollection | ||
*/ | ||
public function testConfigureResponse(){ | ||
$Collection = new \MRussell\REST\Endpoint\JSON\CollectionEndpoint(); | ||
$Collection->setBaseUrl('localhost'); | ||
$Collection->setProperty('url','foo'); | ||
$Response = $Collection->getResponse(); | ||
|
||
$ReflectedResponse = new \ReflectionClass('MRussell\Http\Response\JSON'); | ||
$ReflectedCollection = new \ReflectionClass('MRussell\REST\Endpoint\JSON\CollectionEndpoint'); | ||
$status = $ReflectedResponse->getProperty('status'); | ||
$status->setAccessible(TRUE); | ||
$status->setValue($Response,'200'); | ||
$method = $ReflectedCollection->getMethod('configureResponse'); | ||
$method->setAccessible(TRUE); | ||
$Collection->setResponse($Response); | ||
$this->assertEquals($Response,$method->invoke($Collection,$Response)); | ||
$this->assertNotEmpty($Response->getRequest()); | ||
|
||
$body = $ReflectedResponse->getProperty('body'); | ||
$body->setAccessible(TRUE); | ||
$body->setValue($Response,json_encode(array( | ||
array( | ||
'id' => 'abc123', | ||
'name' => 'foo', | ||
'foo' => 'bar' | ||
), | ||
array( | ||
'id' => 'efg234', | ||
'name' => 'test', | ||
'foo' => '' | ||
) | ||
))); | ||
$Collection->setResponse($Response); | ||
$ReflectedCollection = new \ReflectionClass(static::$_REFLECTED_CLASS."WithModel"); | ||
$updateCollection = $ReflectedCollection->getMethod('updateCollection'); | ||
$updateCollection->setAccessible(TRUE); | ||
$updateCollection->invoke($Collection); | ||
$this->assertEquals(array( | ||
array( | ||
'id' => 'abc123', | ||
'name' => 'foo', | ||
'foo' => 'bar' | ||
), | ||
array( | ||
'id' => 'efg234', | ||
'name' => 'test', | ||
'foo' => '' | ||
) | ||
),$Collection->asArray()); | ||
|
||
$Collection = new CollectionEndpointWithModel(); | ||
$Collection->setResponse($Response); | ||
$ReflectedCollection = new \ReflectionClass(static::$_REFLECTED_CLASS."WithModel"); | ||
$updateCollection = $ReflectedCollection->getMethod('updateCollection'); | ||
$updateCollection->setAccessible(TRUE); | ||
$updateCollection->invoke($Collection); | ||
$this->assertEquals($this->collection,$Collection->asArray()); | ||
|
||
$body->setValue($Response,json_encode(array( | ||
array( | ||
'id' => 'abc123', | ||
'name' => 'foo', | ||
'foo' => 'bar' | ||
), | ||
array( | ||
'id' => 'efg234', | ||
'name' => 'test', | ||
'foo' => '' | ||
), | ||
array( | ||
'name' => 'no_id', | ||
'foo' => '' | ||
) | ||
))); | ||
$Collection->setResponse($Response); | ||
$updateCollection->invoke($Collection); | ||
$this->assertEquals(array( | ||
'abc123' => array( | ||
'id' => 'abc123', | ||
'name' => 'foo', | ||
'foo' => 'bar' | ||
), | ||
'efg234' => array( | ||
'id' => 'efg234', | ||
'name' => 'test', | ||
'foo' => '' | ||
), | ||
0 => array( | ||
'name' => 'no_id', | ||
'foo' => '' | ||
) | ||
),$Collection->asArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace MRussell\REST\Tests\Stubs\Endpoint; | ||
|
||
use MRussell\REST\Endpoint\Abstracts\AbstractCollectionEndpoint; | ||
|
||
class CollectionEndpoint extends AbstractCollectionEndpoint | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace MRussell\REST\Tests\Stubs\Endpoint; | ||
|
||
use MRussell\REST\Endpoint\Abstracts\AbstractCollectionEndpoint; | ||
|
||
class CollectionEndpointWithModel extends AbstractCollectionEndpoint | ||
{ | ||
protected static $_MODEL_CLASS = 'MRussell\\REST\\Tests\\Stubs\\Endpoint\\ModelEndpoint'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace MRussell\REST\Tests\Stubs\Endpoint; | ||
|
||
|
||
use MRussell\REST\Endpoint\Abstracts\AbstractModelEndpoint; | ||
|
||
class ModelEndpoint extends AbstractModelEndpoint | ||
{ | ||
protected static $_ENDPOINT_URL = 'account'; | ||
} |