Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cosminseceleanu committed Mar 18, 2017
1 parent 31f1a47 commit 441a2ec
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Endpoint/EndpointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Cos\RestClientBundle\Endpoint;


use Cos\RestClientBundle\Exception\InvalidConfigurationException;

class EndpointCollection
{
/**
Expand All @@ -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];
Expand Down
3 changes: 2 additions & 1 deletion Endpoint/EndpointFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cos\RestClientBundle\Endpoint;

use Cos\RestClientBundle\Annotation\Endpoint as EndpointAnnotation;
use Cos\RestClientBundle\Exception\InvalidConfigurationException;

class EndpointFactory
{
Expand All @@ -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!");
}
}
3 changes: 2 additions & 1 deletion Endpoint/EndpointLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
============
Expand Down
80 changes: 80 additions & 0 deletions Tests/Adapter/RestAdapterTest.php
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();
}
}
49 changes: 49 additions & 0 deletions Tests/Endpoint/EndpointCollectionTest.php
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);
}
}
46 changes: 46 additions & 0 deletions Tests/Endpoint/EndpointFactoryTest.php
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, []);
}
}
122 changes: 122 additions & 0 deletions Tests/Endpoint/EndpointLoaderTest.php
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();
}
}
19 changes: 19 additions & 0 deletions Tests/Fixtures/MockApi.php
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);
}
Loading

0 comments on commit 441a2ec

Please sign in to comment.