Skip to content

Instantly share code, notes, and snippets.

@wouterj
Created April 25, 2016 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wouterj/89177cf945e174eb3884f6cb57a098b7 to your computer and use it in GitHub Desktop.
Save wouterj/89177cf945e174eb3884f6cb57a098b7 to your computer and use it in GitHub Desktop.
<?php
// ...
class MyUnit
{
private $authChecker;
public function __construct(AuthorizationCheckerInterface $authChecker)
{
$this->authChecker = $authChecker;
}
public function someMethod()
{
if (!$this->authChecker->isGranted('SOME_ACTION')) {
throw new \AccessDeniedException;
}
// ...
}
}
<?php
// ...
class MyUnitWithMocksTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
* @expectedException AccessDeniedException
*/
public function it_avoids_X_being_done_without_proper_priveleges()
{
// mock the auth checker
$authChecker = $this->prophesize(AuthorizationCheckerInterface::class);
$authChecker->isGranted('SOME_ACTION')->will($this->returnValue(false));
$subject = new MyUnit($authChecker->reveal());
$subject->someMethod();
}
}
<?php
// ...
class MyUnitWithoutMocksTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
* @expectedException AccessDeniedException
*/
public function it_avoids_X_being_done_without_proper_priveleges()
{
// instantiate the auth checker
$authChecker = new AuthorizationChecker(...);
$subject = new MyUnit($authChecker);
$subject->someMethod();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment