Skip to content

Commit

Permalink
Adding php.unit.xml file; fixing some tests, adding more for code cov…
Browse files Browse the repository at this point in the history
…erage
  • Loading branch information
jasonroman committed Sep 13, 2017
1 parent 7745f98 commit 6955434
Show file tree
Hide file tree
Showing 20 changed files with 301 additions and 33 deletions.
19 changes: 19 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Test Coverage">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>.</directory>
<exclude>
<directory>./tests</directory>
<file>./src/Request/Stats/Stats/AllStar/AllStarBallotPredictorRequest.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
9 changes: 9 additions & 0 deletions src/Params/Stats/OpponentTeamIdParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ public static function getDefaultValue(): int
{
return self::MIN_ALL;
}

/**
* {@inheritdoc}
* @return int
*/
public static function getExampleValue(): int
{
return self::MIN_ALL;
}
}
3 changes: 2 additions & 1 deletion src/Params/StatsProd/SeasonParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JasonRoman\NbaApi\Params\StatsProd;

use JasonRoman\NbaApi\Params\SeasonParam as BaseSeasonParam;
use JasonRoman\NbaApi\Params\SeasonYearParam;

class SeasonParam extends SeasonYearParam
Expand All @@ -12,6 +13,6 @@ class SeasonParam extends SeasonYearParam
*/
public static function getDefaultValue(): int
{
return SeasonParam::currentSeasonStartYear();
return BaseSeasonParam::currentSeasonStartYear();
}
}
44 changes: 26 additions & 18 deletions src/Request/AbstractNbaApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,14 @@ public static function getValue($propertyName, $methodName)

// use the property if it exists in the $methodName() method of the Request Type -> Param class
// for example, for a Data request, this looks in JasonRoman\NbaApi\Params\Data\<Property>Param
$requestTypeFqcn = $abstractParamClass::getRequestTypeParamClass($requestClass::getDomain(), $propertyName);
$requestTypeParamClass = $abstractParamClass::getRequestTypeParamClass(
$requestClass::getDomain(),
$propertyName
);

// make sure the class exists and is an AbstractParam type
if (class_exists($requestTypeFqcn) && is_subclass_of($requestTypeFqcn, $abstractParamClass)) {
$requestTypeValue = $requestTypeFqcn::$methodName();
// make sure the class exists and is an instance of the base param class
if (class_exists($requestTypeParamClass) && is_subclass_of($requestTypeParamClass, $abstractParamClass)) {
$requestTypeValue = $requestTypeParamClass::$methodName();

// if the default is anything other than null, set the value
if (!is_null($requestTypeValue)) {
Expand All @@ -325,11 +328,11 @@ public static function getValue($propertyName, $methodName)

// use the property if it exists in the $methodName() method of the base Param class
// for example, this looks in JasonRoman\NbaApi\Params\<Property>Param
$paramFqcn = $abstractParamClass::getParamClass($propertyName);
$paramClass = $abstractParamClass::getParamClass($propertyName);

// make sure the class exists and is an AbstractParam type
if (class_exists($paramFqcn) && is_subclass_of($paramFqcn, $abstractParamClass)) {
$value = $paramFqcn::$methodName();
// make sure the class exists and is an instance of the base param class
if (class_exists($paramClass) && is_subclass_of($paramClass, $abstractParamClass)) {
$value = $paramClass::$methodName();

// if the default is anything other than null, set the value
if (!is_null($value)) {
Expand Down Expand Up @@ -592,33 +595,38 @@ public static function getNamespace(): string
*/
protected function convertParamValueToString($propertyName)
{
$abstractParamClass = static::BASE_PARAM_CLASS;

// use the property if it exists in the getDefaultValue() method of the Request Type -> Param class
// for example, for a Data request, this looks in JasonRoman\NbaApi\Params\Data\<Property>Param
$requestTypeClass = AbstractParam::getRequestTypeParamClass($this->getRequestType(), $propertyName);
$requestTypeParamClass = $abstractParamClass::getRequestTypeParamClass(
$this::getDomain(),
$propertyName
);

// make sure the class exists and is an AbstractParam type
// make sure the class exists and is an instance of the base param class
if (
class_exists($requestTypeClass) &&
is_subclass_of($requestTypeClass, AbstractParam::class) &&
method_exists($requestTypeClass, static::CONVERT_TO_STRING_METHOD)
class_exists($requestTypeParamClass) &&
is_subclass_of($requestTypeParamClass, $abstractParamClass) &&
method_exists($requestTypeParamClass, static::CONVERT_TO_STRING_METHOD)
) {
return $requestTypeClass::{static::CONVERT_TO_STRING_METHOD}($this->$propertyName);
return $requestTypeParamClass::{static::CONVERT_TO_STRING_METHOD}($this->$propertyName);
}

// use the property if it exists in the getDefaultValue() method of the base Param class
// for example, for a Data request, this looks in JasonRoman\NbaApi\Params\<Property>Param
$paramClass = AbstractParam::getParamClass($propertyName);
$paramClass = $abstractParamClass::getParamClass($propertyName);

// make sure the class exists and is an AbstractParam type
// make sure the class exists and is an instance of the base param class
if (
class_exists($paramClass) &&
is_subclass_of($paramClass, AbstractParam::class) &&
is_subclass_of($paramClass, $abstractParamClass) &&
method_exists($paramClass, static::CONVERT_TO_STRING_METHOD)
) {
return $paramClass::{static::CONVERT_TO_STRING_METHOD}($this->$propertyName);
}

// if got here, no specific param class exists, so just cast to string
// if got here, no specific param class exists, so just cast to string using the request property utility
return RequestPropertyUtility::getStringValue($this->$propertyName);
}
}
4 changes: 1 addition & 3 deletions src/Request/DocBlockUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public function getDescription($docComment): string
*/
public function getVar($docComment): string
{
$docComment = (string) $docComment;

preg_match(self::REGEX_VAR_PATTERN, $docComment, $matches);
preg_match(self::REGEX_VAR_PATTERN, (string) $docComment, $matches);

if ($matches) {
return $matches[1];
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Client/ClientAllRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function testAllRequests()
);

// sleep to avoid sending too many requests and have the NBA servers stop sending valid responses
usleep(500000);
usleep(250000);
}

// in case all tests are skipped, this will not give a warning about a risky test
Expand Down
73 changes: 65 additions & 8 deletions tests/Unit/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,86 @@ public function testRequestInvalid()
$this->validator
->expects($this->once())
->method('validate')
->willReturn('some violations');
->willReturn('some violations')
;

$response = $this->client->request($nbaApiRequest);
$this->assertInstanceOf(NbaApiResponse::class, $response);
$this->assertInstanceOf(NbaApiResponse::class, $this->client->request($nbaApiRequest));
}

public function testRequestInvalidNoValidationCheck()
{
$client = new Client([], false, $this->guzzle, null);

$nbaApiRequest = $this->getMockBuilder('JasonRoman\NbaApi\Request\AbstractNbaApiRequest')->getMock();

$guzzleResponse = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();

$this->guzzle
->expects($this->once())
->method('request')
->willReturn($guzzleResponse)
;

// normally returns array, but just need value to check against (string) cast
$this->validator
->expects($this->never())
->method('validate')
;

$this->assertInstanceOf(NbaApiResponse::class, $client->request($nbaApiRequest));
}

public function testRequest()
{
$nbaApiRequest = $this->getMockBuilder('JasonRoman\NbaApi\Request\AbstractNbaApiRequest')->getMock();

$nbaApiRequest
->expects($this->once())
->method('getResponseType')
;

$this->validator
->expects($this->once())
->method('validate')
->willReturn(null)
;

$guzzleResponse = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();

$this->guzzle
->expects($this->once())
->method('request')
->willReturn($guzzleResponse)
;

$this->assertInstanceOf(NbaApiResponse::class, $this->client->request($nbaApiRequest));
}

public function testRequestOtherAcceptHeaders()
{
$nbaApiRequest = $this->getMockBuilder('JasonRoman\NbaApi\Request\AbstractNbaApiRequest')->getMock();

$nbaApiRequest
->expects($this->exactly(2))
->method('getResponseType')
->willReturn('xml')
;

$this->validator
->expects($this->once())
->method('validate')
->willReturn(null);
->willReturn(null)
;

$guzzleResponse = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();

$this->guzzle
->expects($this->once())
->method('request')
->willReturn($guzzleResponse);
->willReturn($guzzleResponse)
;

$response = $this->client->request($nbaApiRequest);
$this->assertInstanceOf(NbaApiResponse::class, $response);
$this->assertInstanceOf(NbaApiResponse::class, $this->client->request($nbaApiRequest));
}

public function testApiRequest()
Expand All @@ -92,7 +148,8 @@ public function testApiRequest()
$this->guzzle
->expects($this->once())
->method('request')
->willReturn($guzzleResponse);
->willReturn($guzzleResponse)
;

$response = $this->client->apiRequest('GET', '/test');

Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Params/Data/GameDateParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public function testGetStringValueFromDateTime(\DateTime $dateTime, string $expe
{
$this->assertSame($expected, GameDateParam::getStringValue($dateTime));
}

public function testGetDefaultValue()
{
$this->assertEquals(new \DateTime(), GameDateParam::getDefaultValue());
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Params/Fixtures/OverrideParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ class OverrideParam extends AbstractUnitParam
const DEFAULT = 'base_default_override';
const EXAMPLE = 'base_example_override';

/**
* @param mixed $value
* @return string
*/
public static function getStringValue($value): string
{
return (string) $value .'_and_some_extra';
}

/**
* {@inheritdoc}
* @return string
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Params/Fixtures/Unit/DomainOverrideParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ class DomainOverrideParam extends AbstractUnitParam
const DEFAULT = 'domain_default_override';
const EXAMPLE = 'domain_example_override';

/**
* @param mixed $value
* @return string
*/
public static function getStringValue($value): string
{
return (string) $value .'_and_some_domain_extra';
}

/**
* {@inheritdoc}
* @return string
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Params/SeasonYearParamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace JasonRoman\NbaApi\Tests\Unit\Params;

use JasonRoman\NbaApi\Params\SeasonParam;
use JasonRoman\NbaApi\Params\SeasonYearParam;
use PHPUnit\Framework\TestCase;

class SeasonYearParamTest extends TestCase
{
public function testGetDefaultValue()
{
$this->assertSame(SeasonParam::currentSeasonStartYear(), SeasonYearParam::getDefaultValue());
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Params/Stats/OpponentTeamIdParamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace JasonRoman\NbaApi\Tests\Unit\Params\Stats;

use JasonRoman\NbaApi\Params\Stats\OpponentTeamIdParam;
use PHPUnit\Framework\TestCase;

class OpponentTeamIdParamTest extends TestCase
{
// this is not hit by integration tests...adding here for code coverage
public function testGetDefaultValue()
{
$this->assertSame(OpponentTeamIdParam::MIN_ALL, OpponentTeamIdParam::getDefaultValue());
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Params/StatsProd/SeasonParamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace JasonRoman\NbaApi\Tests\Unit\Params\StatsProd;

use JasonRoman\NbaApi\Params\SeasonParam as BaseSeasonParam;
use JasonRoman\NbaApi\Params\StatsProd\SeasonParam;
use PHPUnit\Framework\TestCase;

class SeasonParamTest extends TestCase
{
public function testGetDefaultValue()
{
$this->assertSame(BaseSeasonParam::currentSeasonStartYear(), SeasonParam::getDefaultValue());
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Params/YearParamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace JasonRoman\NbaApi\Tests\Unit\Params;

use JasonRoman\NbaApi\Params\SeasonParam;
use JasonRoman\NbaApi\Params\SeasonYearParam;
use JasonRoman\NbaApi\Params\YearParam;
use PHPUnit\Framework\TestCase;

class YearParamTest extends TestCase
{
public function testGetDefaultValue()
{
$this->assertSame(SeasonParam::currentSeasonStartYear(), YearParam::getDefaultValue());
}
}
Loading

0 comments on commit 6955434

Please sign in to comment.