Created
July 18, 2014 11:23
-
-
Save omerucel/382cccf1893135d07b8f to your computer and use it in GitHub Desktop.
PHPUnit test case for protected methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Service; | |
use Application\Model\Service; | |
use Application\Model\User; | |
class ServiceValidatorTest extends \PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @var User | |
*/ | |
protected $user; | |
/** | |
* @var Service | |
*/ | |
protected $service; | |
/** | |
* @var ServiceValidator | |
*/ | |
protected $validator; | |
public function setUp() | |
{ | |
$this->user = new User(); | |
$this->service = new Service(); | |
$this->validator = new ServiceValidator($this->user, $this->service); | |
} | |
public function testValidate() | |
{ | |
$this->service->id = 1; | |
$this->service->status = Service::STATUS_ACTIVE; | |
$this->user->setServiceIds([$this->service->id]); | |
$this->validator->validate(); | |
} | |
public function testServiceIsNotActiveException() | |
{ | |
$this->service->status = Service::STATUS_PASSIVE; | |
$method = $this->getProtectedObjectMethod('checkIsServiceActive'); | |
$this->setExpectedException('Application\Service\Exception\ServiceIsNotActiveException'); | |
$method->invoke($this->validator); | |
} | |
public function testPermissionDeniedToAccessServiceException() | |
{ | |
$method = $this->getProtectedObjectMethod('checkIsServiceInUserServices'); | |
$this->setExpectedException('Application\Service\Exception\PermissionDeniedToAccessServiceException'); | |
$method->invoke($this->validator); | |
} | |
/** | |
* @param $methodName | |
* @return \ReflectionMethod | |
*/ | |
protected function getProtectedObjectMethod($methodName) | |
{ | |
$reflection = new \ReflectionClass($this->validator); | |
$method = $reflection->getMethod($methodName); | |
$method->setAccessible(true); | |
return $method; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment