Skip to content

Instantly share code, notes, and snippets.

@justinyost
Last active June 19, 2019 14:13
Show Gist options
  • Save justinyost/6ca61b0902786ddadeb5d2d4c4bbdfcb to your computer and use it in GitHub Desktop.
Save justinyost/6ca61b0902786ddadeb5d2d4c4bbdfcb to your computer and use it in GitHub Desktop.
CakePHP 3 Example Setting a Mocked Model Instance for a Controller
<?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