From 441a2ec37d6730c659afc85746e52ca761d66f4c Mon Sep 17 00:00:00 2001 From: Cosmin Seceleanu Date: Sat, 18 Mar 2017 21:44:45 +0200 Subject: [PATCH] add unit tests --- Endpoint/EndpointCollection.php | 4 +- Endpoint/EndpointFactory.php | 3 +- Endpoint/EndpointLoader.php | 3 +- README.md | 1 - Tests/Adapter/RestAdapterTest.php | 80 ++++++++++++++ Tests/Endpoint/EndpointCollectionTest.php | 49 +++++++++ Tests/Endpoint/EndpointFactoryTest.php | 46 ++++++++ Tests/Endpoint/EndpointLoaderTest.php | 122 ++++++++++++++++++++++ Tests/Fixtures/MockApi.php | 19 ++++ Tests/Request/RequestBuilderTest.php | 112 ++++++++++++++++++++ composer.json | 4 + 11 files changed, 439 insertions(+), 4 deletions(-) create mode 100644 Tests/Adapter/RestAdapterTest.php create mode 100644 Tests/Endpoint/EndpointCollectionTest.php create mode 100644 Tests/Endpoint/EndpointFactoryTest.php create mode 100644 Tests/Endpoint/EndpointLoaderTest.php create mode 100644 Tests/Fixtures/MockApi.php create mode 100644 Tests/Request/RequestBuilderTest.php diff --git a/Endpoint/EndpointCollection.php b/Endpoint/EndpointCollection.php index 319bf58..d8ffbec 100644 --- a/Endpoint/EndpointCollection.php +++ b/Endpoint/EndpointCollection.php @@ -3,6 +3,8 @@ namespace Cos\RestClientBundle\Endpoint; +use Cos\RestClientBundle\Exception\InvalidConfigurationException; + class EndpointCollection { /** @@ -21,7 +23,7 @@ public function add($class, $method, Endpoint $endpoint) public function get($class, $method) { if (!isset($this->endpoints[$class][$method])) { - throw new \InvalidArgumentException(sprintf("No endpoint was configured for class %s", $class)); + throw new InvalidConfigurationException(sprintf("No endpoint was configured for class %s", $class)); } return $this->endpoints[$class][$method]; diff --git a/Endpoint/EndpointFactory.php b/Endpoint/EndpointFactory.php index 1a09668..056ff3b 100644 --- a/Endpoint/EndpointFactory.php +++ b/Endpoint/EndpointFactory.php @@ -3,6 +3,7 @@ namespace Cos\RestClientBundle\Endpoint; use Cos\RestClientBundle\Annotation\Endpoint as EndpointAnnotation; +use Cos\RestClientBundle\Exception\InvalidConfigurationException; class EndpointFactory { @@ -17,6 +18,6 @@ public function create($baseUri, array $annotations) } } - throw new \InvalidArgumentException("Could not create endpoint! No Endpoint annotation used!"); + throw new InvalidConfigurationException("Could not create endpoint! No Endpoint annotation used!"); } } \ No newline at end of file diff --git a/Endpoint/EndpointLoader.php b/Endpoint/EndpointLoader.php index 1e95408..4a02065 100644 --- a/Endpoint/EndpointLoader.php +++ b/Endpoint/EndpointLoader.php @@ -5,6 +5,7 @@ use Cos\RestClientBundle\Annotation\Client; use Cos\RestClientBundle\Exception\InvalidConfigurationException; +use Cos\RestClientBundle\Exception\ParameterNotSetException; use Doctrine\Common\Annotations\Reader; class EndpointLoader @@ -56,7 +57,7 @@ private function getBaseUri(\ReflectionClass $class) private function getClientBaseUri($clientName) { if (!isset($this->clients[$clientName])) { - throw new InvalidConfigurationException("No client with name {$clientName} was configured"); + throw new ParameterNotSetException("No client with name {$clientName} was configured"); } return $this->clients[$clientName]['baseUri']; diff --git a/README.md b/README.md index b1d44c5..ed5f8a5 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ Symfony **Rest Client Bundle** using [GuzzleHttp](http://docs.guzzlephp.org/en/latest/index.html) and [Ocramius/ProxyManager](http://ocramius.github.io/ProxyManager/) -**Work in progress!!!** Installation ============ diff --git a/Tests/Adapter/RestAdapterTest.php b/Tests/Adapter/RestAdapterTest.php new file mode 100644 index 0000000..cdf4712 --- /dev/null +++ b/Tests/Adapter/RestAdapterTest.php @@ -0,0 +1,80 @@ + 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(); + } +} \ No newline at end of file diff --git a/Tests/Endpoint/EndpointCollectionTest.php b/Tests/Endpoint/EndpointCollectionTest.php new file mode 100644 index 0000000..01ea25f --- /dev/null +++ b/Tests/Endpoint/EndpointCollectionTest.php @@ -0,0 +1,49 @@ +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); + } +} \ No newline at end of file diff --git a/Tests/Endpoint/EndpointFactoryTest.php b/Tests/Endpoint/EndpointFactoryTest.php new file mode 100644 index 0000000..998a582 --- /dev/null +++ b/Tests/Endpoint/EndpointFactoryTest.php @@ -0,0 +1,46 @@ +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, []); + } +} \ No newline at end of file diff --git a/Tests/Endpoint/EndpointLoaderTest.php b/Tests/Endpoint/EndpointLoaderTest.php new file mode 100644 index 0000000..ebb2f5c --- /dev/null +++ b/Tests/Endpoint/EndpointLoaderTest.php @@ -0,0 +1,122 @@ + ['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(); + } +} \ No newline at end of file diff --git a/Tests/Fixtures/MockApi.php b/Tests/Fixtures/MockApi.php new file mode 100644 index 0000000..d06291c --- /dev/null +++ b/Tests/Fixtures/MockApi.php @@ -0,0 +1,19 @@ + 1 + ]; + + private $path; + + /** + * @var RequestBuilder + */ + private $builder; + + protected function setUp() + { + $this->path = new Path(); + $this->path->name = 'url_id'; + $this->path->paramName = 'id'; + $this->builder = new RequestBuilder($this->getRequestOptionsMock()); + } + + public function testBuild() + { + $endpoint = new Endpoint('http://test.com/foo', 'post'); + $request = $this->builder->setEndpoint($endpoint) + ->build(); + + $this->assertEquals("http://test.com/foo", $request->getUri()); + $this->assertEquals("post", $request->getMethod()); + } + + public function testBuildWithPathInUri() + { + + $endpoint = new Endpoint('http://test.com/foo/{url_id}', 'get', [$this->path]); + $request = $this->builder->setEndpoint($endpoint) + ->setParameters($this->parameters) + ->build(); + + $this->assertEquals('http://test.com/foo/1', $request->getUri()); + } + + /** + * @expectedException Cos\RestClientBundle\Exception\InvalidConfigurationException + */ + public function testBuildWithWrongPathInUri() + { + $endpoint = new Endpoint('http://test.com/foo/{id}', 'get', [$this->path]); + $request = $this->builder->setEndpoint($endpoint) + ->setParameters($this->parameters) + ->build(); + + $this->assertEquals('http://test.com/foo/1', $request->getUri()); + } + + /** + * @expectedException Cos\RestClientBundle\Exception\ParameterNotSetException + */ + public function testBuildWithWrongParamForPath() + { + $this->path->paramName = 'wrongId'; + $endpoint = new Endpoint('http://test.com/foo/{url_id}', 'get', [$this->path]); + $request = $this->builder->setEndpoint($endpoint) + ->setParameters($this->parameters) + ->build(); + + $this->assertEquals('http://test.com/foo/1', $request->getUri()); + } + + public function testWithRequestOptions() + { + $requestOption = \Mockery::mock(RequestOptionInterface::class) + ->shouldReceive('supports') + ->withAnyArgs() + ->andReturn(true) + ->getMock() + ->shouldReceive('addValue') + ->withAnyArgs() + ->once() + ->getMock(); + $query = new Query(); + $query->name = 'id'; + $endpoint = new Endpoint('uri', 'get', [$query]); + $builder = new RequestBuilder($this->getRequestOptionsMock([$requestOption])); + $builder->setParameters($this->parameters) + ->setEndpoint($endpoint) + ->build(); + \Mockery::close(); + } + + private function getRequestOptionsMock(array $options = []) + { + return \Mockery::mock(RequestOptionsCollection::class) + ->shouldReceive('getOptions') + ->withNoArgs() + ->andReturn($options) + ->getMock(); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index bbf7c17..93bd1c7 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,10 @@ "guzzlehttp/guzzle": "^6.2", "doctrine/annotations": "^1.3" }, + "require-dev": { + "phpunit/phpunit": ">4.8.0", + "mockery/mockery": ">=0.9.0" + }, "autoload": { "psr-0": { "Cos\\RestClientBundle": "" } },