Last active
June 19, 2019 14:13
-
-
Save justinyost/6ca61b0902786ddadeb5d2d4c4bbdfcb to your computer and use it in GitHub Desktop.
CakePHP 3 Example Setting a Mocked Model Instance for a Controller
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 | |
public function testResetPasswordPostRequestSaveFails() { | |
$UserEntity = $this | |
->getMockBuilder('\App\Model\Entity\User') | |
->getMock(); | |
$UsersTable = $this | |
->getMockBuilder('\App\Model\Table\UsersTable') | |
->setMethods([ | |
'clearExpiredPasswordResetCodes', | |
]) | |
->disableOriginalConstructor() | |
->getMock(); | |
$UsersTable->expects($this->once()) | |
->method('clearExpiredPasswordResetCodes') | |
->with() | |
->will($this->returnValue($UserEntity)); | |
TableRegistry::remove('Users'); | |
TableRegistry::set('Users', $UsersTable); | |
$this->get("/controller/action", $data); | |
// assert whatever | |
} | |
public function controllerAction($resetcode = null) { | |
$UserEntity = $this->UsersModelClass->clearExpiredPasswordResetCodes(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment