public function AgreementHandlerTest::testAgree in Agreement 8.2
Same name and namespace in other branches
- 3.0.x tests/src/Unit/AgreementHandlerTest.php \Drupal\Tests\agreement\Unit\AgreementHandlerTest::testAgree()
Asserts that database operation errors are handled.
@dataProvider agreeProvider
Parameters
bool $expected: The expected return value.
string $errorDuring: The expected cause of the error.
File
- tests/
src/ Unit/ AgreementHandlerTest.php, line 30
Class
- AgreementHandlerTest
- Tests logic in the agreement handler service.
Namespace
Drupal\Tests\agreement\UnitCode
public function testAgree($expected, $errorDuring = NULL) {
$entityTypeManagerProphet = $this
->prophesize('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$pathMatcherProphet = $this
->prophesize('\\Drupal\\Core\\Path\\PathMatcherInterface');
$accountProphet = $this
->prophesize('\\Drupal\\Core\\Session\\AccountProxyInterface');
$accountProphet
->isAnonymous()
->willReturn(FALSE);
$accountProphet
->id()
->willReturn(5);
$agreementProphet = $this
->prophesize('\\Drupal\\agreement\\Entity\\Agreement');
$agreementProphet
->id()
->willReturn('agreement');
$transactionProphet = $this
->prophesize('\\Drupal\\Core\\Database\\Transaction');
$transactionProphet
->rollback();
$connectionProphet = $this
->prophesize('\\Drupal\\Core\\Database\\Connection');
$connectionProphet
->startTransaction()
->willReturn($transactionProphet
->reveal());
// Prophecy does not allow mocking objects that return $this because.
$delete = $this
->getMockBuilder('\\Drupal\\Core\\Database\\Query\\Delete')
->disableOriginalConstructor()
->getMock();
$delete
->expects($this
->any())
->method('condition')
->willReturnSelf();
$delete
->expects($this
->any())
->method('execute')
->willReturnCallback(function () use ($expected, $errorDuring) {
if (!$expected && $errorDuring === 'delete') {
throw new DatabaseExceptionWrapper();
}
// SAVED_DELETED constant.
return 3;
});
$insert = $this
->getMockBuilder('\\Drupal\\Core\\Database\\Query\\Insert')
->disableOriginalConstructor()
->getMock();
$insert
->expects($this
->any())
->method('fields')
->willReturnSelf();
$insert
->expects($this
->any())
->method('execute')
->willReturnCallback(function () use ($expected, $errorDuring) {
if (!$expected && $errorDuring === 'insert') {
throw new DatabaseExceptionWrapper();
}
// SAVED_NEW constant.
return 1;
});
$connectionProphet
->delete('agreement')
->willReturn($delete);
$connectionProphet
->insert('agreement')
->willReturn($insert);
$timeProphet = $this
->prophesize('\\Drupal\\Component\\Datetime\\TimeInterface');
$timeProphet
->getRequestTime()
->willReturn(time());
$requestStack = new RequestStack();
$requestStack
->push(Request::create('/'));
$handler = new AgreementHandler($connectionProphet
->reveal(), $entityTypeManagerProphet
->reveal(), $pathMatcherProphet
->reveal(), $timeProphet
->reveal(), $requestStack);
$this
->assertEquals($expected, $handler
->agree($agreementProphet
->reveal(), $accountProphet
->reveal()));
}