Skip to content

added ReflectionMethod in TestCase #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions test/Github/Tests/Api/AbstractApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use Github\Api\AbstractApi;
use GuzzleHttp\Psr7\Response;
use ReflectionMethod;

class AbstractApiTest extends \PHPUnit_Framework_TestCase
class AbstractApiTest extends TestCase
{
/**
* @test
Expand All @@ -30,9 +29,10 @@ public function shouldPassGETRequestToClient()

$api = $this->getAbstractApiObject($client);

$method = $this->getMethodReflection($api, 'get');
$actual = $this->getMethod($api, 'get')
->invokeArgs($api, ['/path', ['param1' => 'param1value'], ['header1' => 'header1value']]);

$this->assertEquals($expectedArray, $method->invokeArgs($api, ['/path', ['param1' => 'param1value'], ['header1' => 'header1value']]));
$this->assertEquals($expectedArray, $actual);
}

/**
Expand All @@ -57,9 +57,10 @@ public function shouldPassPOSTRequestToClient()
->willReturn($httpClient);

$api = $this->getAbstractApiObject($client);
$method = $this->getMethodReflection($api, 'post');
$actual = $this->getMethod($api, 'post')
->invokeArgs($api, ['/path', ['param1' => 'param1value'], ['option1' => 'option1value']]);

$this->assertEquals($expectedArray, $method->invokeArgs($api, ['/path', array('param1' => 'param1value'), array('option1' => 'option1value')]));
$this->assertEquals($expectedArray, $actual);
}

/**
Expand All @@ -84,9 +85,10 @@ public function shouldPassPATCHRequestToClient()
->willReturn($httpClient);

$api = $this->getAbstractApiObject($client);
$method = $this->getMethodReflection($api, 'patch');
$actual = $this->getMethod($api, 'patch')
->invokeArgs($api, ['/path', ['param1' => 'param1value'], ['option1' => 'option1value']]);

$this->assertEquals($expectedArray, $method->invokeArgs($api, ['/path', array('param1' => 'param1value'), array('option1' => 'option1value')]));
$this->assertEquals($expectedArray, $actual);
}

/**
Expand All @@ -111,9 +113,10 @@ public function shouldPassPUTRequestToClient()
->willReturn($httpClient);

$api = $this->getAbstractApiObject($client);
$method = $this->getMethodReflection($api, 'put');
$actual = $this->getMethod($api, 'put')
->invokeArgs($api, ['/path', ['param1' => 'param1value'], ['option1' => 'option1value']]);

$this->assertEquals($expectedArray, $method->invokeArgs($api, ['/path', array('param1' => 'param1value'), array('option1' => 'option1value')]));
$this->assertEquals($expectedArray, $actual);
}

/**
Expand All @@ -139,9 +142,10 @@ public function shouldPassDELETERequestToClient()


$api = $this->getAbstractApiObject($client);
$method = $this->getMethodReflection($api, 'delete');
$actual = $this->getMethod($api, 'delete')
->invokeArgs($api, ['/path', ['param1' => 'param1value'], ['option1' => 'option1value']]);

$this->assertEquals($expectedArray, $method->invokeArgs($api, ['/path', array('param1' => 'param1value'), array('option1' => 'option1value')]));
$this->assertEquals($expectedArray, $actual);
}

/**
Expand All @@ -166,34 +170,29 @@ public function shouldNotPassEmptyRefToClient()
->willReturn($httpClient);

$api = $this->getAbstractApiObject($client);
$method = $this->getMethodReflection($api, 'get');
$actual = $this->getMethod($api, 'get')->invokeArgs($api, ['/path', ['ref' => null]]);

$this->assertInternalType('array', $method->invokeArgs($api, ['/path', array('ref' => null)]));
$this->assertInternalType('array', $actual);
}

/**
* @param $client
* @return AbstractApi
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getAbstractApiObject($client)
{
return $this->getMockBuilder(AbstractApi::class)
return $this->getMockBuilder($this->getApiClass())
->setMethods(null)
->setConstructorArgs([$client])
->getMock();
}

/**
* @param $api
* @param $methodName
* @return ReflectionMethod
* @return string
*/
protected function getMethodReflection($api, $methodName)
protected function getApiClass()
{
$method = new ReflectionMethod($api, $methodName);
$method->setAccessible(true);

return $method;
return AbstractApi::class;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions test/Github/Tests/Api/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Github\Tests\Api;

use ReflectionMethod;

abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* @return string
*/
abstract protected function getApiClass();

/**
Expand All @@ -25,4 +30,17 @@ protected function getApiMock()
->setConstructorArgs(array($client))
->getMock();
}

/**
* @param object $object
* @param string $methodName
* @return ReflectionMethod
*/
protected function getMethod($object, $methodName)
{
$method = new ReflectionMethod($object, $methodName);
$method->setAccessible(true);

return $method;
}
}