-
Notifications
You must be signed in to change notification settings - Fork 1
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
31f1a47
commit 441a2ec
Showing
11 changed files
with
439 additions
and
4 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
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
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,80 @@ | ||
<?php | ||
|
||
namespace Cos\RestClientBundle\Tests\Adapter; | ||
|
||
|
||
use Cos\RestClientBundle\Adapter\RestAdapter; | ||
use Cos\RestClientBundle\Endpoint\Endpoint; | ||
use Cos\RestClientBundle\Endpoint\EndpointCollection; | ||
use Cos\RestClientBundle\Request\Request; | ||
use Cos\RestClientBundle\Request\RequestBuilder; | ||
use Cos\RestClientBundle\Request\RequestExecutorInterface; | ||
use Cos\RestClientBundle\Tests\Fixtures\MockApi; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RestAdapterTest extends TestCase | ||
{ | ||
private $endpoint; | ||
private $parameters = [ | ||
'id' => 1 | ||
]; | ||
private $request; | ||
|
||
protected function setUp() | ||
{ | ||
$uri = 'http://test/com'; | ||
$this->endpoint = new Endpoint($uri); | ||
$this->request = new Request($uri, 'get'); | ||
} | ||
|
||
public function testCall() | ||
{ | ||
$adapter = new RestAdapter( | ||
$this->getRequestExecutorMock(), | ||
$this->getEndpointCollectionMock(), | ||
$this->getRequestBuilderMock() | ||
); | ||
$adapter->call(MockApi::class, 'get', [0 => 1]); | ||
\Mockery::close(); | ||
} | ||
|
||
private function getRequestExecutorMock() | ||
{ | ||
return \Mockery::mock(RequestExecutorInterface::class) | ||
->shouldReceive('execute') | ||
->withAnyArgs() | ||
->once() | ||
->andReturnNull() | ||
->getMock(); | ||
} | ||
|
||
private function getEndpointCollectionMock() | ||
{ | ||
return \Mockery::mock(EndpointCollection::class) | ||
->shouldReceive('get') | ||
->with(MockApi::class, 'get') | ||
->once() | ||
->andReturn($this->endpoint) | ||
->getMock(); | ||
} | ||
|
||
private function getRequestBuilderMock() | ||
{ | ||
$mock = \Mockery::mock(RequestBuilder::class); | ||
|
||
$mock->shouldReceive('setEndpoint') | ||
->with($this->endpoint) | ||
->andReturn($mock) | ||
->getMock(); | ||
|
||
$mock->shouldReceive('setParameters') | ||
->with($this->parameters) | ||
->andReturn($mock) | ||
->getMock(); | ||
|
||
return $mock->shouldReceive('build') | ||
->withNoArgs() | ||
->andReturn($this->request) | ||
->getMock(); | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace Cos\RestClientBundle\Tests\Endpoint; | ||
|
||
|
||
use Cos\RestClientBundle\Endpoint\Endpoint; | ||
use Cos\RestClientBundle\Endpoint\EndpointCollection; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class EndpointCollectionTest extends TestCase | ||
{ | ||
/** | ||
* @var EndpointCollection | ||
*/ | ||
private $endpointCollection; | ||
|
||
protected function setUp() | ||
{ | ||
$this->endpointCollection = new EndpointCollection(); | ||
$this->endpointCollection->add('class1', 'method1', $this->getEndpointMock()); | ||
$this->endpointCollection->add('class1', 'method2', $this->getEndpointMock()); | ||
$this->endpointCollection->add('class2', 'method1', $this->getEndpointMock()); | ||
} | ||
|
||
public function testGet() | ||
{ | ||
$this->assertNotNull($this->endpointCollection->get('class1', 'method1')); | ||
$this->assertNotNull($this->endpointCollection->get('class2', 'method1')); | ||
} | ||
|
||
/** | ||
* @expectedException Cos\RestClientBundle\Exception\InvalidConfigurationException | ||
*/ | ||
public function testNotFound() | ||
{ | ||
$this->endpointCollection->get('class3', 'method1'); | ||
} | ||
|
||
public function testHas() | ||
{ | ||
$this->assertTrue($this->endpointCollection->has('class1')); | ||
$this->assertFalse($this->endpointCollection->has('class3')); | ||
} | ||
|
||
private function getEndpointMock() | ||
{ | ||
return \Mockery::mock(Endpoint::class); | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
namespace Cos\RestClientBundle\Tests\Endpoint; | ||
|
||
|
||
use Cos\RestClientBundle\Annotation\Endpoint; | ||
use Cos\RestClientBundle\Annotation\Query; | ||
use Cos\RestClientBundle\Endpoint\EndpointFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class EndpointFactoryTest extends TestCase | ||
{ | ||
/** | ||
* @var EndpointFactory | ||
*/ | ||
private $factory; | ||
private $baseUri = 'http://test.com'; | ||
|
||
protected function setUp() | ||
{ | ||
$this->factory = new EndpointFactory(); | ||
} | ||
|
||
public function testCreate() | ||
{ | ||
$annotations = [ | ||
new Endpoint([ | ||
'uri' => '/posts', | ||
'method' => 'get'] | ||
), | ||
new Query() | ||
]; | ||
$endpoint = $this->factory->create($this->baseUri, $annotations); | ||
|
||
$this->assertEquals("http://test.com/posts", $endpoint->getUri()); | ||
$this->assertCount(2, $endpoint->getAnnotations()); | ||
} | ||
|
||
/** | ||
* @expectedException Cos\RestClientBundle\Exception\InvalidConfigurationException | ||
*/ | ||
public function testCreateWithoutEndpointAnnotation() | ||
{ | ||
$this->factory->create($this->baseUri, []); | ||
} | ||
} |
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,122 @@ | ||
<?php | ||
|
||
namespace Cos\RestClientBundle\Tests\Endpoint; | ||
|
||
|
||
use Cos\RestClientBundle\Annotation\Client; | ||
use Cos\RestClientBundle\Annotation\Endpoint as EndpointAnnotation; | ||
use Cos\RestClientBundle\Endpoint\Endpoint; | ||
use Cos\RestClientBundle\Endpoint\EndpointCollection; | ||
use Cos\RestClientBundle\Endpoint\EndpointFactory; | ||
use Cos\RestClientBundle\Endpoint\EndpointLoader; | ||
use Cos\RestClientBundle\Tests\Fixtures\MockApi; | ||
use Doctrine\Common\Annotations\Reader; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class EndpointLoaderTest extends TestCase | ||
{ | ||
private $clients = [ | ||
'default' => ['baseUri' => 'http://test.com'] | ||
]; | ||
|
||
/** @var EndpointLoader */ | ||
private $loader; | ||
|
||
private $endpoint; | ||
|
||
private $methodAnnotations; | ||
|
||
private $classAnnotations; | ||
|
||
protected function setUp() | ||
{ | ||
$this->endpoint = new Endpoint('http://test.com'); | ||
$endpoint = new EndpointAnnotation(['uri' => '/foo']); | ||
$this->methodAnnotations = [$endpoint]; | ||
$client = new Client(); | ||
$client->name = 'default'; | ||
$this->classAnnotations = [$client]; | ||
} | ||
|
||
public function testLoad() | ||
{ | ||
$loader = new EndpointLoader( | ||
$this->getReaderMock($this->classAnnotations), | ||
$this->getEndpointCollectionMock(), | ||
$this->getEndpointFactoryMock(), | ||
$this->clients | ||
); | ||
|
||
$loader->load(MockApi::class); | ||
|
||
\Mockery::close(); | ||
} | ||
|
||
/** | ||
* @expectedException Cos\RestClientBundle\Exception\InvalidConfigurationException | ||
*/ | ||
public function testLoadWithoutClient() | ||
{ | ||
$loader = new EndpointLoader( | ||
$this->getReaderMock(), | ||
$this->getEndpointCollectionMock(0), | ||
$this->getEndpointFactoryMock(0), | ||
$this->clients | ||
); | ||
$loader->load(MockApi::class); | ||
\Mockery::close(); | ||
} | ||
|
||
/** | ||
* @expectedException Cos\RestClientBundle\Exception\ParameterNotSetException | ||
*/ | ||
public function testLoadWithWrongClient() | ||
{ | ||
$loader = new EndpointLoader( | ||
$this->getReaderMock($this->classAnnotations), | ||
$this->getEndpointCollectionMock(0), | ||
$this->getEndpointFactoryMock(0), | ||
[] | ||
); | ||
$loader->load(MockApi::class); | ||
\Mockery::close(); | ||
} | ||
|
||
|
||
private function getReaderMock(array $returnedAnnotations = array()) | ||
{ | ||
return \Mockery::mock(Reader::class) | ||
->shouldReceive('getClassAnnotations') | ||
->withAnyArgs() | ||
->andReturn($returnedAnnotations) | ||
->getMock() | ||
->shouldReceive('getMethodAnnotations') | ||
->withAnyArgs() | ||
->andReturn($this->methodAnnotations) | ||
->getMock(); | ||
} | ||
|
||
private function getEndpointCollectionMock($limit = 1) | ||
{ | ||
return \Mockery::mock(EndpointCollection::class) | ||
->shouldReceive('add') | ||
->with(MockApi::class, 'get', $this->endpoint) | ||
->times($limit) | ||
->andReturnNull() | ||
->getMock() | ||
->shouldReceive('has') | ||
->withAnyArgs() | ||
->andReturn(false) | ||
->getMock(); | ||
} | ||
|
||
private function getEndpointFactoryMock($limit = 1) | ||
{ | ||
return \Mockery::mock(EndpointFactory::class) | ||
->shouldReceive('create') | ||
->with('http://test.com', $this->methodAnnotations) | ||
->times($limit) | ||
->andReturn($this->endpoint) | ||
->getMock(); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Cos\RestClientBundle\Tests\Fixtures; | ||
|
||
use Cos\RestClientBundle\Annotation\Client; | ||
use Cos\RestClientBundle\Annotation\Endpoint; | ||
use Cos\RestClientBundle\Annotation\Path; | ||
|
||
/** | ||
* @Client(name="default") | ||
*/ | ||
interface MockApi | ||
{ | ||
/** | ||
* @Path(name="id", paramName="id") | ||
* @Endpoint(uri="/foo/{id}") | ||
*/ | ||
public function get($id); | ||
} |
Oops, something went wrong.