Skip to content

Commit

Permalink
Finish unit tests closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Hughes committed Feb 27, 2015
1 parent 79f4eaa commit a28fb00
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 6 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"phpunit/phpunit": "4.0.*"
},
"autoload": {
"psr-4": { "Choccybiccy\\": "src" }
"psr-4": { "Choccybiccy\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Choccybiccy\\": "tests/" }
}
}
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="Main test suite">
<directory>./tests/unit/</directory>
<directory>./tests/unit/*/</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
5 changes: 4 additions & 1 deletion src/HumanApi/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ protected function fetchResults($url, array $params = array())
array(
$url,
array(
"query" => array(
"query" => array_merge(
array(
"access_token" => $this->accessToken,
),
$params
)
)
)
Expand Down
11 changes: 9 additions & 2 deletions tests/HumanApi/Endpoint/LocationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@

namespace Choccybiccy\HumanApi\Endpoint;

use Choccybiccy\HumanApi\Traits\ReflectionMethods;

/**
* Class LocationsTest
* @package Choccybiccy\HumanApi\Endpoint
*/
class LocationsTest extends \PHPUnit_Framework_TestCase
{

use ReflectionMethods;

/**
* Test build specific entry url
*/
public function testBuildSpecificEntryUrl()
{

$locations = $this->getMock('Choccybiccy\HumanApi\Endpoint\Locations');
$locations = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\Locations')
->setMethods(array("buildUrlParts"))
->disableOriginalConstructor()
->getMock();
$type = $this->getProtectedProperty($locations, "type");
$id = 1;

$locations->expects($this->once())
->method("buildUrlParts")
->with($type, $id);
->with(array($type, $id));

$this->runProtectedMethod($locations, "buildSpecificEntryUrl", array($id));

Expand Down
66 changes: 66 additions & 0 deletions tests/HumanApi/Endpoint/MeasurementEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,77 @@

namespace Choccybiccy\HumanApi\Endpoint;

use Choccybiccy\HumanApi\Traits\ReflectionMethods;

/**
* Class MeasurementEndpointTest
* @package Choccybiccy\HumanApi\Endpoint
*/
class MeasurementEndpointTest extends \PHPUnit_Framework_TestCase
{

use ReflectionMethods;

/**
* Test build specific entry url
*/
public function testBuildSpecificEntryUrl()
{

$measurement = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\MeasurementEndpoint')
->setMethods(array("buildUrlParts"))
->disableOriginalConstructor()
->getMock();
$type = $this->getProtectedProperty($measurement, "type");
$plural = $this->getProtectedProperty($measurement, "plural");
$id = 1;

$measurement->expects($this->once())
->method("buildUrlParts")
->with(array($type, $plural, $id));

$this->runProtectedMethod($measurement, "buildSpecificEntryUrl", array($id));

}

/**
* Test build list url
*/
public function testBuildListUrl()
{

$measurement = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\MeasurementEndpoint')
->setMethods(array("buildUrlParts"))
->disableOriginalConstructor()
->getMock();
$type = $this->getProtectedProperty($measurement, "type");
$plural = $this->getProtectedProperty($measurement, "plural");

$measurement->expects($this->once())
->method("buildUrlParts")
->with(array($type, $plural));

$this->runProtectedMethod($measurement, "buildListUrl");

}

/**
* Test build recent url
*/
public function testBuildRecentUrl()
{

$measurement = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\MeasurementEndpoint')
->setMethods(array("buildUrlParts"))
->disableOriginalConstructor()
->getMock();
$type = $this->getProtectedProperty($measurement, "type");

$measurement->expects($this->once())
->method("buildUrlParts")
->with(array($type));

$this->runProtectedMethod($measurement, "buildRecentUrl");

}
}
65 changes: 65 additions & 0 deletions tests/HumanApi/Endpoint/PeriodicEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,76 @@

namespace Choccybiccy\HumanApi\Endpoint;

use Choccybiccy\HumanApi\Traits\ReflectionMethods;

/**
* Class PeriodicEndpointTest
* @package Choccybiccy\HumanApi\Endpoint
*/
class PeriodicEndpointTest extends \PHPUnit_Framework_TestCase
{

use ReflectionMethods;

/**
* Test build specific entry url
*/
public function testBuildSpecificEntryUrl()
{

$periodic = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\PeriodicEndpoint')
->setMethods(array("buildUrlParts"))
->disableOriginalConstructor()
->getMock();
$type = $this->getProtectedProperty($periodic, "type");
$id = 1;

$periodic->expects($this->once())
->method("buildUrlParts")
->with(array($type, $id));

$this->runProtectedMethod($periodic, "buildSpecificEntryUrl", array($id));

}

/**
* Test build list url
*/
public function testBuildListUrl()
{

$periodic = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\PeriodicEndpoint')
->setMethods(array("buildUrlParts"))
->disableOriginalConstructor()
->getMock();
$type = $this->getProtectedProperty($periodic, "type");

$periodic->expects($this->once())
->method("buildUrlParts")
->with(array($type));

$this->runProtectedMethod($periodic, "buildListUrl");

}

/**
* Test build recent url
*/
public function testBuildRecentUrl()
{

$periodic = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\PeriodicEndpoint')
->setMethods(array("buildUrlParts"))
->disableOriginalConstructor()
->getMock();
$type = $this->getProtectedProperty($periodic, "type");
$plural = $this->getProtectedProperty($periodic, "plural");

$periodic->expects($this->once())
->method("buildUrlParts")
->with(array($type, $plural));

$this->runProtectedMethod($periodic, "buildRecentUrl");

}
}
55 changes: 55 additions & 0 deletions tests/HumanApi/Endpoint/SimpleEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,66 @@

namespace Choccybiccy\HumanApi\Endpoint;

use Choccybiccy\HumanApi\Traits\ReflectionMethods;

/**
* Class SimpleEndpointTest
* @package Choccybiccy\HumanApi\Endpoint
*/
class SimpleEndpointTest extends \PHPUnit_Framework_TestCase
{

use ReflectionMethods;

/**
* Test build list url
*/
public function testBuildListUrl()
{

$simple = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\SimpleEndpoint')
->setMethods(array("buildUrlParts"))
->disableOriginalConstructor()
->getMock();
$type = $this->getProtectedProperty($simple, "type");

$simple->expects($this->once())
->method("buildUrlParts")
->with(array($type));

$this->runProtectedMethod($simple, "buildListUrl");

}

/**
* Test buildSpecificEntryUrl throws unsupported exception
*/
public function testBuildSpecificEntryUrlThrowsUnsuportedException()
{

$simple = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\SimpleEndpoint')
->setMethods(null)
->disableOriginalConstructor()
->getMock();

$this->setExpectedException('Choccybiccy\HumanApi\Endpoint\UnsupportedEndpointMethodException');
$this->runProtectedMethod($simple, "buildSpecificEntryUrl", array(1));

}

/**
* Test buildRecentUrl throws unsupported exception
*/
public function testBuildRecentUrlThrowsUnsuportedException()
{

$simple = $this->getMockBuilder('Choccybiccy\HumanApi\Endpoint\SimpleEndpoint')
->setMethods(null)
->disableOriginalConstructor()
->getMock();

$this->setExpectedException('Choccybiccy\HumanApi\Endpoint\UnsupportedEndpointMethodException');
$this->runProtectedMethod($simple, "buildRecentUrl", array(1));

}
}
45 changes: 45 additions & 0 deletions tests/HumanApi/EndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,49 @@ public function testBuildCollection()
$this->assertInstanceOf('\Choccybiccy\HumanApi\Model', $collection->current());

}

/**
* Test fetchResults
*/
public function testFetchResults()
{

$accessToken = "testAccessToken";
$url = "test";
$params = array("limit" => 5);
$endpoint = $this->getMockEndpoint(array("get", "buildCollection"));

$result = array("id" => 1, "exampleKey" => "Test");

$response = $this->getMockBuilder('GuzzleHttp\Message\ResponseInterface')
->disableOriginalConstructor()
->setMethods(array("json"))
->getMockForAbstractClass();

$response->expects($this->once())
->method("json")
->willReturn($result);

$this->setProtectedProperty($endpoint, "method", "get");
$this->setProtectedProperty($endpoint, "accessToken", $accessToken);
$this->setProtectedProperty($endpoint, "listReturnsArray", false);

$query = array_merge(
array(
"access_token" => $accessToken,
),
$params
);

$endpoint->expects($this->once())
->method("get")
->with($url, array("query" => $query))
->willReturn($response);
$endpoint->expects($this->once())
->method("buildCollection")
->with(array($result));

$this->runProtectedMethod($endpoint, "fetchResults", array($url, $params));

}
}

0 comments on commit a28fb00

Please sign in to comment.