Skip to content

Instantly share code, notes, and snippets.

@omerucel
Created July 18, 2014 11:23
Show Gist options
  • Save omerucel/382cccf1893135d07b8f to your computer and use it in GitHub Desktop.
Save omerucel/382cccf1893135d07b8f to your computer and use it in GitHub Desktop.
PHPUnit test case for protected methods
<?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