Created
April 25, 2016 17:49
-
-
Save wouterj/89177cf945e174eb3884f6cb57a098b7 to your computer and use it in GitHub Desktop.
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 | |
// ... | |
class MyUnit | |
{ | |
private $authChecker; | |
public function __construct(AuthorizationCheckerInterface $authChecker) | |
{ | |
$this->authChecker = $authChecker; | |
} | |
public function someMethod() | |
{ | |
if (!$this->authChecker->isGranted('SOME_ACTION')) { | |
throw new \AccessDeniedException; | |
} | |
// ... | |
} | |
} |
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 | |
// ... | |
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(); | |
} | |
} |
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 | |
// ... | |
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