Best Mockery code snippet using are.andThrow
DoctrineErrorHandlerTest.php
Source:DoctrineErrorHandlerTest.php
...289 $lowerLayer290 ->shouldReceive('transaction')291 ->once()292 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))293 ->andThrow(294 new DeadlockException(295 PDOException::new(new \PDOException('pdo deadlock exception')),296 null,297 ),298 );299 $lowerLayer300 ->shouldReceive('transaction')301 ->once()302 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))303 ->andReturn(173);304 $connection = \Mockery::mock(Connection::class);305 $lowerLayer306 ->shouldReceive('getConnection')307 ->once()308 ->withNoArgs()309 ->andReturn($connection);310 $connection311 ->shouldReceive('rollBack')312 ->once()313 ->withNoArgs()314 ->andThrow(new \Exception('some random rollback exception'));315 $lowerLayer316 ->shouldReceive('setTransaction')317 ->once()318 ->with(false);319 // Error handler instantiation320 $errorHandler = new DBErrorHandler();321 $errorHandler->setLowerLayer($lowerLayer);322 // Do the transaction function323 $result = $errorHandler->transaction($func, $a, $b, $c);324 // Check that we got back the transaction result325 $this->assertEquals(173, $result);326 }327 public function testRedoTransactionAfterConnectionProblem(): void328 {329 // Lower layer mock330 $lowerLayer = \Mockery::mock(DBRawInterface::class);331 // Example function to pass along332 $func = function (int $a, int $b, int $c): int {333 return $a + $b + $c;334 };335 // Example variables to pass along336 $a = 5;337 $b = 13;338 $c = 155;339 // We only get the forwarding to lower layer if there is no transaction active340 $lowerLayer341 ->shouldReceive('inTransaction')342 ->withNoArgs()343 ->andReturn(false);344 // The call we are expecting to the lower layer345 $lowerLayer346 ->shouldReceive('transaction')347 ->once()348 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))349 ->andThrow(350 new ConnectionException(351 PDOException::new(new \PDOException('MySQL server has gone away')),352 null,353 ),354 );355 $lowerLayer356 ->shouldReceive('transaction')357 ->once()358 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))359 ->andReturn(173);360 $connection = \Mockery::mock(Connection::class);361 $lowerLayer362 ->shouldReceive('getConnection')363 ->once()364 ->withNoArgs()365 ->andReturn($connection);366 $connection367 ->shouldReceive('rollBack')368 ->once()369 ->withNoArgs()370 ->andThrow(new \Exception('some random rollback exception'));371 $connection372 ->shouldReceive('close')373 ->once()374 ->withNoArgs();375 $connection376 ->shouldReceive('connect')377 ->once()378 ->withNoArgs();379 $lowerLayer380 ->shouldReceive('setTransaction')381 ->once()382 ->with(false);383 // Error handler instantiation384 $errorHandler = new DBErrorHandler();385 $errorHandler->setLowerLayer($lowerLayer);386 // Do the transaction function387 $result = $errorHandler->transaction($func, $a, $b, $c);388 // Check that we got back the transaction result389 $this->assertEquals(173, $result);390 }391 public function testRedoTransactionAfterConnectionProblemMultipleAttempts(): void392 {393 // Lower layer mock394 $lowerLayer = \Mockery::mock(DBRawInterface::class);395 // Example function to pass along396 $func = function (int $a, int $b, int $c): int {397 return $a + $b + $c;398 };399 // Example variables to pass along400 $a = 5;401 $b = 13;402 $c = 155;403 // We only get the forwarding to lower layer if there is no transaction active404 $lowerLayer405 ->shouldReceive('inTransaction')406 ->withNoArgs()407 ->andReturn(false);408 // The call we are expecting to the lower layer409 $lowerLayer410 ->shouldReceive('transaction')411 ->once()412 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))413 ->andThrow(414 new ConnectionException(415 PDOException::new(new \PDOException('MySQL server has gone away')),416 null,417 ),418 );419 $lowerLayer420 ->shouldReceive('transaction')421 ->once()422 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))423 ->andReturn(173);424 $connection = \Mockery::mock(Connection::class);425 $lowerLayer426 ->shouldReceive('getConnection')427 ->once()428 ->withNoArgs()429 ->andReturn($connection);430 $connection431 ->shouldReceive('rollBack')432 ->once()433 ->withNoArgs()434 ->andThrow(new \Exception('some random rollback exception'));435 $connection436 ->shouldReceive('close')437 ->times(3)438 ->withNoArgs();439 $connection440 ->shouldReceive('connect')441 ->times(2)442 ->withNoArgs()443 ->andThrow(444 new ConnectionException(445 PDOException::new(new \PDOException('MySQL server has gone away')),446 null,447 ),448 );449 $connection450 ->shouldReceive('connect')451 ->times(1)452 ->withNoArgs();453 $lowerLayer454 ->shouldReceive('setTransaction')455 ->once()456 ->with(false);457 // Error handler instantiation458 $errorHandler = new DBErrorHandler();459 $errorHandler->setLowerLayer($lowerLayer);460 // Do the transaction function461 $result = $errorHandler->transaction($func, $a, $b, $c);462 // Check that we got back the transaction result463 $this->assertEquals(173, $result);464 }465 public function testExceptionNoRetriesTransactionAfterDeadlock(): void466 {467 $this->expectException(DBLockException::class);468 // Lower layer mock469 $lowerLayer = \Mockery::mock(DBRawInterface::class);470 // Example function to pass along471 $func = function (int $a, int $b, int $c): int {472 return $a + $b + $c;473 };474 // Example variables to pass along475 $a = 5;476 $b = 13;477 $c = 155;478 // We only get the forwarding to lower layer if there is no transaction active479 $lowerLayer480 ->shouldReceive('inTransaction')481 ->withNoArgs()482 ->andReturn(false);483 // The call we are expecting to the lower layer484 $lowerLayer485 ->shouldReceive('transaction')486 ->once()487 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))488 ->andThrow(489 new DeadlockException(490 PDOException::new(new \PDOException('pdo deadlock exception')),491 null,492 ),493 );494 $connection = \Mockery::mock(Connection::class);495 $lowerLayer496 ->shouldReceive('getConnection')497 ->once()498 ->withNoArgs()499 ->andReturn($connection);500 $connection501 ->shouldReceive('rollBack')502 ->once()503 ->withNoArgs();504 $lowerLayer505 ->shouldReceive('setTransaction')506 ->once()507 ->with(false);508 // Error handler instantiation509 $errorHandler = new DBErrorHandler();510 $errorHandler->setLowerLayer($lowerLayer);511 $errorHandler->setLockRetries([]);512 // Do the transaction function513 $errorHandler->transaction($func, $a, $b, $c);514 }515 public function testExceptionNoRetriesTransactionAfterConnectionProblem(): void516 {517 $this->expectException(DBConnectionException::class);518 // Lower layer mock519 $lowerLayer = \Mockery::mock(DBRawInterface::class);520 // Example function to pass along521 $func = function (int $a, int $b, int $c): int {522 return $a + $b + $c;523 };524 // Example variables to pass along525 $a = 5;526 $b = 13;527 $c = 155;528 // We only get the forwarding to lower layer if there is no transaction active529 $lowerLayer530 ->shouldReceive('inTransaction')531 ->withNoArgs()532 ->andReturn(false);533 // The call we are expecting to the lower layer534 $lowerLayer535 ->shouldReceive('transaction')536 ->once()537 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))538 ->andThrow(539 new ConnectionException(540 PDOException::new(new \PDOException('MySQL server has gone away')),541 null,542 ),543 );544 $lowerLayer545 ->shouldReceive('transaction')546 ->once()547 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))548 ->andReturn(173);549 $connection = \Mockery::mock(Connection::class);550 $lowerLayer551 ->shouldReceive('getConnection')552 ->once()553 ->withNoArgs()554 ->andReturn($connection);555 $connection556 ->shouldReceive('rollBack')557 ->once()558 ->withNoArgs();559 $lowerLayer560 ->shouldReceive('setTransaction')561 ->once()562 ->with(false);563 // Error handler instantiation564 $errorHandler = new DBErrorHandler();565 $errorHandler->setLowerLayer($lowerLayer);566 $errorHandler->setConnectionRetries([]);567 // Do the transaction function568 $errorHandler->transaction($func, $a, $b, $c);569 }570 public function testExceptionFromDriverLikeBadSQL(): void571 {572 $this->expectException(DBDriverException::class);573 // Lower layer mock574 $lowerLayer = \Mockery::mock(DBRawInterface::class);575 // Example function to pass along576 $func = function (int $a, int $b, int $c): int {577 return $a + $b + $c;578 };579 // Example variables to pass along580 $a = 5;581 $b = 13;582 $c = 155;583 // We only get the forwarding to lower layer if there is no transaction active584 $lowerLayer585 ->shouldReceive('inTransaction')586 ->withNoArgs()587 ->andReturn(false);588 // The call we are expecting to the lower layer589 $lowerLayer590 ->shouldReceive('transaction')591 ->once()592 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))593 ->andThrow(594 new DriverException(595 PDOException::new(new \PDOException('MySQL server has gone away')),596 null,597 ),598 );599 $connection = \Mockery::mock(Connection::class);600 $lowerLayer601 ->shouldReceive('getConnection')602 ->once()603 ->withNoArgs()604 ->andReturn($connection);605 $connection606 ->shouldReceive('rollBack')607 ->once()608 ->withNoArgs()609 ->andThrow(new \Exception('some rollback exception'));610 $lowerLayer611 ->shouldReceive('setTransaction')612 ->once()613 ->with(false);614 // Error handler instantiation615 $errorHandler = new DBErrorHandler();616 $errorHandler->setLowerLayer($lowerLayer);617 // Do the transaction function618 $errorHandler->transaction($func, $a, $b, $c);619 }620 public function testUnhandledException(): void621 {622 $this->expectException(\InvalidArgumentException::class);623 // Lower layer mock624 $lowerLayer = \Mockery::mock(DBRawInterface::class);625 // Example function to pass along626 $func = function (int $a, int $b, int $c): int {627 return $a + $b + $c;628 };629 // Example variables to pass along630 $a = 5;631 $b = 13;632 $c = 155;633 // We only get the forwarding to lower layer if there is no transaction active634 $lowerLayer635 ->shouldReceive('inTransaction')636 ->withNoArgs()637 ->andReturn(false);638 // The call we are expecting to the lower layer639 $lowerLayer640 ->shouldReceive('transaction')641 ->once()642 ->with(IsEqual::equalTo($func), IsEqual::equalTo($a), IsEqual::equalTo($b), IsEqual::equalTo($c))643 ->andThrow(644 new \InvalidArgumentException('some weird exception'),645 );646 $connection = \Mockery::mock(Connection::class);647 $lowerLayer648 ->shouldReceive('getConnection')649 ->once()650 ->withNoArgs()651 ->andReturn($connection);652 $connection653 ->shouldReceive('rollBack')654 ->once()655 ->withNoArgs()656 ->andThrow(new \Exception('some rollback exception'));657 $lowerLayer658 ->shouldReceive('setTransaction')659 ->once()660 ->with(false);661 // Error handler instantiation662 $errorHandler = new DBErrorHandler();663 $errorHandler->setLowerLayer($lowerLayer);664 // Do the transaction function665 $errorHandler->transaction($func, $a, $b, $c);666 }667 public function testExceptionSelectWithinTransactionDeadlock(): void668 {669 $this->expectException(DeadlockException::class);670 // Lower layer mock671 $lowerLayer = \Mockery::mock(DBRawInterface::class);672 // The call we are expecting to the lower layer673 $lowerLayer674 ->shouldReceive('select')675 ->once()676 ->with('SELECT * FROM table')677 ->andThrow(678 new DeadlockException(679 PDOException::new(new \PDOException('pdo deadlock exception')),680 null,681 ),682 );683 $lowerLayer684 ->shouldReceive('inTransaction')685 ->once()686 ->withNoArgs()687 ->andReturn(true);688 // Error handler instantiation689 $errorHandler = new DBErrorHandler();690 $errorHandler->setLowerLayer($lowerLayer);691 $errorHandler->setLockRetries([1]);692 // Do the transaction function693 $errorHandler->select('SELECT * FROM table');694 }695 public function testExceptionNoRetriesSelectAfterDeadlock(): void696 {697 $this->expectException(DBLockException::class);698 // Lower layer mock699 $lowerLayer = \Mockery::mock(DBRawInterface::class);700 // The call we are expecting to the lower layer701 $lowerLayer702 ->shouldReceive('select')703 ->twice()704 ->with('SELECT * FROM table')705 ->andThrow(706 new DeadlockException(707 PDOException::new(new \PDOException('pdo deadlock exception')),708 null,709 ),710 );711 $lowerLayer712 ->shouldReceive('inTransaction')713 ->once()714 ->withNoArgs()715 ->andReturn(false);716 // Error handler instantiation717 $errorHandler = new DBErrorHandler();718 $errorHandler->setLowerLayer($lowerLayer);719 $errorHandler->setLockRetries([1]);720 // Do the transaction function721 $errorHandler->select('SELECT * FROM table');722 }723 public function testExceptionSelectWithinTransactionConnectionProblem(): void724 {725 $this->expectException(ConnectionException::class);726 // Lower layer mock727 $lowerLayer = \Mockery::mock(DBRawInterface::class);728 // The call we are expecting to the lower layer729 $lowerLayer730 ->shouldReceive('select')731 ->once()732 ->with('SELECT * FROM table')733 ->andThrow(734 new ConnectionException(735 PDOException::new(new \PDOException('MySQL server has gone away')),736 null,737 ),738 );739 $lowerLayer740 ->shouldReceive('inTransaction')741 ->once()742 ->withNoArgs()743 ->andReturn(true);744 // Error handler instantiation745 $errorHandler = new DBErrorHandler();746 $errorHandler->setLowerLayer($lowerLayer);747 $errorHandler->setConnectionRetries([1]);748 // Do the transaction function749 $errorHandler->select('SELECT * FROM table');750 }751 public function testExceptionNoRetriesSelectAfterConnectionProblem(): void752 {753 $this->expectException(DBConnectionException::class);754 // Lower layer mock755 $lowerLayer = \Mockery::mock(DBRawInterface::class);756 // The call we are expecting to the lower layer757 $lowerLayer758 ->shouldReceive('select')759 ->twice()760 ->with('SELECT * FROM table')761 ->andThrow(762 new ConnectionException(763 PDOException::new(new \PDOException('MySQL server has gone away')),764 null,765 ),766 );767 $lowerLayer768 ->shouldReceive('inTransaction')769 ->once()770 ->withNoArgs()771 ->andReturn(false);772 $connection = \Mockery::mock(Connection::class);773 $lowerLayer774 ->shouldReceive('getConnection')775 ->once()776 ->withNoArgs()777 ->andReturn($connection);778 $connection779 ->shouldReceive('close')780 ->once()781 ->withNoArgs();782 $connection783 ->shouldReceive('connect')784 ->once()785 ->withNoArgs();786 $connection787 ->shouldReceive('ping')788 ->once()789 ->withNoArgs()790 ->andReturn(true);791 // Error handler instantiation792 $errorHandler = new DBErrorHandler();793 $errorHandler->setLowerLayer($lowerLayer);794 $errorHandler->setConnectionRetries([1]);795 // Do the transaction function796 $errorHandler->select('SELECT * FROM table');797 }798 public function testExceptionSelectFromDriver(): void799 {800 $this->expectException(DBDriverException::class);801 // Lower layer mock802 $lowerLayer = \Mockery::mock(DBRawInterface::class);803 // The call we are expecting to the lower layer804 $lowerLayer805 ->shouldReceive('select')806 ->once()807 ->with('SELECT * FROM table')808 ->andThrow(809 new DriverException(810 PDOException::new(new \PDOException('MySQL server has gone away')),811 null,812 ),813 );814 // Error handler instantiation815 $errorHandler = new DBErrorHandler();816 $errorHandler->setLowerLayer($lowerLayer);817 // Do the transaction function818 $errorHandler->select('SELECT * FROM table');819 }820}...
RulesCollection.spec.php
Source:RulesCollection.spec.php
...26 $rule3 = Mockery::mock(Rule::class);27 $rules = new RulesCollection([$rule1, $rule2, $rule3]);28 $rule1->shouldReceive('validate')->once()->with('key', $input, $input);29 $rule2->shouldReceive('validate')->once()->with('key', $input, $input)30 ->andThrow(new ValidationException(['p1' => 'v1']));31 $rule2->shouldReceive('getName')->once()->andReturn('rule2');32 $rule3->shouldReceive('validate')->once()->with('key', $input, $input)33 ->andThrow(new ValidationException(['p2' => 'v2']));34 $rule3->shouldReceive('getName')->once()->andReturn('rule3');35 $test = $rules->validate('key', $input);36 expect($test)->to->be->an('array');37 expect($test)->to->have->length(2);38 expect($test[0])->to->be->an->instanceof(ValidationError::class);39 expect($test[0]->getRule())->to->be->equal('rule2');40 expect($test[0]->getParameters())->to->be->equal(['p1' => 'v1']);41 expect($test[1])->to->be->an->instanceof(ValidationError::class);42 expect($test[1]->getRule())->to->be->equal('rule3');43 expect($test[1]->getParameters())->to->be->equal(['p2' => 'v2']);44 });45 it('should be able to handle nested input', function () {46 $input = [47 'nested1' => [48 [49 'nested2' => [50 [51 'key' => 'value11',52 ],53 [54 'key' => 'value12',55 ],56 ]57 ],58 [59 'nested2' => [60 [61 'key' => 'value21',62 ],63 [64 'key' => 'value22',65 ],66 ]67 ],68 ],69 ];70 $rule1 = Mockery::mock(Rule::class);71 $rule2 = Mockery::mock(Rule::class);72 $rule3 = Mockery::mock(Rule::class);73 $rules = new RulesCollection([$rule1, $rule2, $rule3]);74 $rule1->shouldReceive('validate')->once()->with('key', ['key' => 'value11'], $input);75 $rule1->shouldReceive('validate')->once()->with('key', ['key' => 'value12'], $input);76 $rule1->shouldReceive('validate')->once()->with('key', ['key' => 'value21'], $input)77 ->andThrow(new ValidationException(['p1' => 'v1']));78 $rule1->shouldReceive('validate')->once()->with('key', ['key' => 'value22'], $input)79 ->andThrow(new ValidationException(['p2' => 'v2']));80 $rule1->shouldReceive('getName')->twice()->andReturn('rule1');81 $rule2->shouldReceive('validate')->once()->with('key', ['key' => 'value11'], $input);82 $rule2->shouldReceive('validate')->once()->with('key', ['key' => 'value12'], $input);83 $rule2->shouldReceive('validate')->once()->with('key', ['key' => 'value21'], $input);84 $rule2->shouldReceive('validate')->once()->with('key', ['key' => 'value22'], $input)85 ->andThrow(new ValidationException(['p3' => 'v3']));86 $rule2->shouldReceive('getName')->once()->andReturn('rule2');87 $rule3->shouldReceive('validate')->once()->with('key', ['key' => 'value11'], $input);88 $rule3->shouldReceive('validate')->once()->with('key', ['key' => 'value12'], $input);89 $rule3->shouldReceive('validate')->once()->with('key', ['key' => 'value21'], $input);90 $rule3->shouldReceive('validate')->once()->with('key', ['key' => 'value22'], $input);91 $test = $rules->validate('nested1.*.nested2.*.key', $input);92 expect($test)->to->be->an('array');93 expect($test)->to->have->length(3);94 expect($test[0])->to->be->an->instanceof(ValidationError::class);95 expect($test[0]->getRule())->to->be->equal('rule1');96 expect($test[0]->getParameters())->to->be->equal(['p1' => 'v1']);97 expect($test[1])->to->be->an->instanceof(ValidationError::class);98 expect($test[1]->getRule())->to->be->equal('rule1');99 expect($test[1]->getParameters())->to->be->equal(['p2' => 'v2']);100 expect($test[2])->to->be->an->instanceof(ValidationError::class);101 expect($test[2]->getRule())->to->be->equal('rule2');102 expect($test[2]->getParameters())->to->be->equal(['p3' => 'v3']);103 });104 it('should be able to handle input starting with a nested array', function () {105 $input = [106 ['key' => 'value1'],107 ['key' => 'value2'],108 ['key' => 'value3'],109 ];110 $rule1 = Mockery::mock(Rule::class);111 $rule2 = Mockery::mock(Rule::class);112 $rule3 = Mockery::mock(Rule::class);113 $rules = new RulesCollection([$rule1, $rule2, $rule3]);114 $rule1->shouldReceive('validate')->once()->with('key', ['key' => 'value1'], $input);115 $rule1->shouldReceive('validate')->once()->with('key', ['key' => 'value2'], $input)116 ->andThrow(new ValidationException(['p1' => 'v1']));117 $rule1->shouldReceive('validate')->once()->with('key', ['key' => 'value3'], $input)118 ->andThrow(new ValidationException(['p2' => 'v2']));119 $rule1->shouldReceive('getName')->twice()->andReturn('rule1');120 $rule2->shouldReceive('validate')->once()->with('key', ['key' => 'value1'], $input);121 $rule2->shouldReceive('validate')->once()->with('key', ['key' => 'value2'], $input);122 $rule2->shouldReceive('validate')->once()->with('key', ['key' => 'value3'], $input)123 ->andThrow(new ValidationException(['p3' => 'v3']));124 $rule2->shouldReceive('getName')->once()->andReturn('rule2');125 $rule3->shouldReceive('validate')->once()->with('key', ['key' => 'value1'], $input);126 $rule3->shouldReceive('validate')->once()->with('key', ['key' => 'value2'], $input);127 $rule3->shouldReceive('validate')->once()->with('key', ['key' => 'value3'], $input);128 $test = $rules->validate('*.key', $input);129 expect($test)->to->be->an('array');130 expect($test)->to->have->length(3);131 expect($test[0])->to->be->an->instanceof(ValidationError::class);132 expect($test[0]->getRule())->to->be->equal('rule1');133 expect($test[0]->getParameters())->to->be->equal(['p1' => 'v1']);134 expect($test[1])->to->be->an->instanceof(ValidationError::class);135 expect($test[1]->getRule())->to->be->equal('rule1');136 expect($test[1]->getParameters())->to->be->equal(['p2' => 'v2']);137 expect($test[2])->to->be->an->instanceof(ValidationError::class);138 expect($test[2]->getRule())->to->be->equal('rule2');139 expect($test[2]->getParameters())->to->be->equal(['p3' => 'v3']);140 });141 it('should be able to handle input ending with a nested array', function () {142 $nested = [143 ['nested' => 'value1'],144 ['nested' => 'value2'],145 ['nested' => 'value3'],146 ];147 $input = [148 'key' => $nested,149 ];150 $rule1 = Mockery::mock(Rule::class);151 $rule2 = Mockery::mock(Rule::class);152 $rules = new RulesCollection([$rule1, $rule2]);153 $rule1->shouldReceive('validate')->once()->with('*', $nested, $input)154 ->andThrow(new ValidationException(['p1' => 'v1']));155 $rule1->shouldReceive('getName')->once()->andReturn('rule1');156 $rule2->shouldReceive('validate')->once()->with('*', $nested, $input);157 $test = $rules->validate('key.*', $input);158 expect($test)->to->be->an('array');159 expect($test)->to->have->length(1);160 expect($test[0])->to->be->an->instanceof(ValidationError::class);161 expect($test[0]->getRule())->to->be->equal('rule1');162 expect($test[0]->getParameters())->to->be->equal(['p1' => 'v1']);163 });164 it('should be able to handle input containing only nested array', function () {165 $input = [166 ['key' => 'value1'],167 ['key' => 'value2'],168 ['key' => 'value3'],169 ];170 $rule1 = Mockery::mock(Rule::class);171 $rule2 = Mockery::mock(Rule::class);172 $rules = new RulesCollection([$rule1, $rule2]);173 $rule1->shouldReceive('validate')->once()->with('*', $input, $input)174 ->andThrow(new ValidationException(['p1' => 'v1']));175 $rule1->shouldReceive('getName')->once()->andReturn('rule1');176 $rule2->shouldReceive('validate')->once()->with('*', $input, $input);177 $test = $rules->validate('*', $input);178 expect($test)->to->be->an('array');179 expect($test)->to->have->length(1);180 expect($test[0])->to->be->an->instanceof(ValidationError::class);181 expect($test[0]->getRule())->to->be->equal('rule1');182 expect($test[0]->getParameters())->to->be->equal(['p1' => 'v1']);183 });184 });185});...
CommanderTest.php
Source:CommanderTest.php
...77 function erroring_commands_are_logged()78 {79 $erroringProcess = Mockery::mock(Process::class);80 $genericException = new \Exception('A generic exception');81 $erroringProcess->shouldReceive('run')->andThrow($genericException);82 $this->commander->setCommands([$erroringProcess])->handle();83 $this->log->shouldHaveReceived('error')->with($genericException);84 }85 /** @test */86 function failed_commands_are_logged()87 {88 $failingProcess = Mockery::mock(Process::class);89 $command = 'echo "some output"; nonexistingcommandIhopeneversomeonewouldnameacommandlikethis';90 $process = new SymfonyProcess($command);91 $process->run();92 $e = new ProcessFailedException($process);93 $failingProcess->shouldReceive('run')->andThrow($e);94 $failingProcess->shouldReceive('command')->andReturn($command);95 $this->commander->setCommands([$failingProcess])->handle();96 $this->log->shouldHaveReceived('error')->with(Mockery::on(function ($argument) use ($process) {97 return str_contains($argument, trim($process->getOutput()))98 && str_contains($argument, trim($process->getErrorOutput()));99 }));100 }101 /** @test */102 function the_literal_string_no_output_is_shown_if_theres_no_output()103 {104 $failingProcess = Mockery::mock(Process::class);105 $command = 'nonexistingcommandIhopeneversomeonewouldnameacommandlikethis';106 $process = new SymfonyProcess($command);107 $process->run();108 $e = new ProcessFailedException($process);109 $failingProcess->shouldReceive('run')->andThrow($e);110 $failingProcess->shouldReceive('command')->andReturn($command);111 $this->commander->setCommands([$failingProcess])->handle();112 $this->log->shouldHaveReceived('error')->with(Mockery::on(function ($argument) use ($process) {113 return str_contains($argument, 'Output: No output')114 && str_contains($argument, trim($process->getErrorOutput()));115 }));116 }117 /** @test */118 function the_literal_string_no_error_is_shown_if_theres_no_error()119 {120 $failingProcess = Mockery::mock(Process::class);121 $command = '(echo "some output"; exit 1)';122 $process = new SymfonyProcess($command);123 $process->run();124 $e = new ProcessFailedException($process);125 $failingProcess->shouldReceive('run')->andThrow($e);126 $failingProcess->shouldReceive('command')->andReturn($command);127 $this->commander->setCommands([$failingProcess])->handle();128 $this->log->shouldHaveReceived('error')->with(Mockery::on(function ($argument) use ($process) {129 return str_contains($argument, trim($process->getOutput()))130 && str_contains($argument, 'Error: No error');131 }));132 }133 /** @test */134 function commands_can_be_a_closure()135 {136 $this->commander->event(new class {137 public function foo() {138 return 'bar';139 }...
andThrow
Using AI Code Generation
1require_once 'vendor/autoload.php';2use PHPUnit\Framework\TestCase;3{4 public function testEmpty()5 {6 $stack = [];7 $this->assertEmpty($stack);8 return $stack;9 }10 public function testPush(array $stack)11 {12 array_push($stack, 'foo');13 $this->assertEquals('foo', $stack[count($stack)-1]);14 $this->assertNotEmpty($stack);15 return $stack;16 }17 public function testPop(array $stack)18 {19 $this->assertEquals('foo', array_pop($stack));20 $this->assertEmpty($stack);21 }22}23. 1 / 1 (100%)24OK (1 test, 3 assertions)25require_once 'vendor/autoload.php';26use PHPUnit\Framework\TestCase;27{28 public function testPushAndPop()29 {30 $stack = [];31 $this->assertEquals(0, count($stack));32 array_push($stack, 'foo');33 $this->assertEquals('foo', $stack[count($stack)-1]);34 $this->assertEquals(1, count($stack));35 $this->assertEquals('foo', array_pop($stack));36 $this->assertEquals(0, count($stack));37 }38 public function testReturnArgument()39 {40 $mock = $this->getMock('stdClass', ['foo']);41 $mock->expects($this->any())42 ->method('foo')43 ->will($this->returnArgument(0));44 $this->assertEquals('bar', $mock->foo('bar'));45 }46}47. 1 / 1 (100%)48OK (1 test, 5 assertions)
andThrow
Using AI Code Generation
1$are = new are();2$are->toThrow('exception');3$are = new are();4$are->toThrow('exception');5$are = new are();6$are->toThrow('exception');7$are = new are();8$are->toThrow('exception');9$are = new are();10$are->toThrow('exception');11$are = new are();12$are->toThrow('exception');13$are = new are();14$are->toThrow('exception');15$are = new are();16$are->toThrow('exception');17$are = new are();18$are->toThrow('exception');19$are = new are();20$are->toThrow('exception');21$are = new are();22$are->toThrow('exception');23$are = new are();24$are->toThrow('exception');25$are = new are();26$are->toThrow('exception');27$are = new are();28$are->toThrow('exception');29$are = new are();30$are->toThrow('exception');31$are = new are();32$are->toThrow('exception');
andThrow
Using AI Code Generation
1$are = new Are();2$are->toThrow(new Exception('This is an exception'));3$are = new Are();4$are->toThrow(new Exception('This is an exception'));5$are = new Are();6$are->toThrow(new Exception('This is an exception'));7$are = new Are();8$are->toThrow(new Exception('This is an exception'));9$are = new Are();10$are->toThrow(new Exception('This is an exception'));11$are = new Are();12$are->toThrow(new Exception('This is an exception'));13$are = new Are();14$are->toThrow(new Exception('This is an exception'));15$are = new Are();16$are->toThrow(new Exception('This is an exception'));17$are = new Are();18$are->toThrow(new Exception('This is an exception'));19$are = new Are();20$are->toThrow(new Exception('This is an exception'));21$are = new Are();22$are->toThrow(new Exception('This is an exception'));23$are = new Are();24$are->toThrow(new Exception('This is an exception'));25$are = new Are();26$are->toThrow(new Exception('This is an exception'));27$are = new Are();28$are->toThrow(new Exception('This is an exception'));
andThrow
Using AI Code Generation
1$are = new are();2$are->toThrow('error', 'error message');3$are = new are();4$are->toThrow('error', 'error message');5$are = new are();6$are->toThrow('error', 'error message');7$are = new are();8$are->toThrow('error', 'error message');9$are = new are();10$are->toThrow('error', 'error message');11$are = new are();12$are->toThrow('error', 'error message');13$are = new are();14$are->toThrow('error', 'error message');15$are = new are();16$are->toThrow('error', 'error message');17$are = new are();18$are->toThrow('error', 'error message');19$are = new are();20$are->toThrow('error', 'error message');21$are = new are();22$are->toThrow('error', 'error message');23$are = new are();24$are->toThrow('error', 'error message');25$are = new are();26$are->toThrow('error', 'error message');27$are = new are();28$are->toThrow('error', 'error message');29$are = new are();
andThrow
Using AI Code Generation
1require_once 'are.php';2$are = new are();3$are->andThrow(1,2,3,4,5,6,7,8,9,10);4require_once 'are.php';5$are = new are();6$are->andThrow(1,2,3,4,5,6,7,8,9,10);7require_once 'are.php';8$are = new are();9$are->andThrow(1,2,3,4,5,6,7,8,9,10);10require_once 'are.php';11$are = new are();12$are->andThrow(1,2,3,4,5,6,7,8,9,10);13require_once 'are.php';14$are = new are();15$are->andThrow(1,2,3,4,5,6,7,8,9,10);16require_once 'are.php';17$are = new are();18$are->andThrow(1,2,3,4,5,6,7,8,9,10);19require_once 'are.php';20$are = new are();21$are->andThrow(1,2,3,4,5,6,7,8,9,10);22require_once 'are.php';23$are = new are();24$are->andThrow(1,2,3,4,5,6,7,8,9,10);25require_once 'are.php';26$are = new are();27$are->andThrow(1,2,3,4,5,6,7,8,9,10);
andThrow
Using AI Code Generation
1$are = new are();2$are->toThrow('Exception', 'This is the exception message');3$are = new are();4$are->andReturn('This is the return value');5$are = new are();6$are->andReturnSelf();7$are = new are();8$are->andReturnArgument(0);9$are = new are();10$are->andReturnCallback(function($arg1, $arg2){return $arg1 + $arg2;});11$are = new are();12$are->andReturnMap([['foo', 'bar'], ['baz', 'foo']]);13$are = new are();14$are->andReturnValues(['foo', 'bar', 'baz']);15$are = new are();16$are->andReturnUsing(function(){return 'foo';});17$are = new are();18$are->andReturnArgument(0);19$are = new are();20$are->andReturnArgument(0);21$are = new are();22$are->andReturnArgument(0);23$are = new are();24$are->andReturnArgument(0);25$are = new are();26$are->andReturnArgument(0);27$are = new are();28$are->andReturnArgument(0);
andThrow
Using AI Code Generation
1$are = new are();2$are->toBe()->andThrow(new Exception('this is an exception'));3$are = new are();4$are->toBe()->andReturn('this is a return value');5$are = new are();6$are->toBe()->andThrow(new Exception('this is an exception'))->andReturn('this is a return value');7$are = new are();8$are->toBe()->andThrow(new Exception('this is an exception'))->andThrow(new Exception('this is an exception'));9$are = new are();10$are->toBe()->andReturn('this is a return value')->andThrow(new Exception('this is an exception'));11$are = new are();12$are->toBe()->andReturn('this is a return value')->andReturn('this is a return value');13$are = new are();14$are->toBe()->andReturn('this is a return value')->andThrow(new Exception('this is an exception'))->andThrow(new Exception('this is an exception'));15$are = new are();16$are->toBe()->andThrow(new Exception('this is an exception'))->andThrow(new Exception('this is an exception'))->andThrow(new Exception('this is an exception'));17$are = new are();18$are->toBe()->andThrow(new Exception('this is an exception'))->andThrow(new Exception('this is an exception'))->andReturn('this is a return value');19$are = new are();20$are->toBe()->andThrow(new Exception('this is an exception'))->andThrow(new Exception('this is an exception'))->andReturn('this is a return value')->andReturn('this is a return value');
andThrow
Using AI Code Generation
1$are = new are();2$are->andThrow();3$are = new are();4$are->andThrow();5$are = new are();6$are->andThrow();7$are = new are();8$are->andThrow();9$are = new are();10$are->andThrow();11$are = new are();12$are->andThrow();13$are = new are();14$are->andThrow();15$are = new are();16$are->andThrow();17$are = new are();18$are->andThrow();19$are = new are();20$are->andThrow();21$are = new are();22$are->andThrow();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with andThrow on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!