Created
July 13, 2020 19:13
-
-
Save pascualmg/710bfd8eae60c97da61424d57eaa0b14 to your computer and use it in GitHub Desktop.
HOWTO test a command handler ( symfony , phpunit )
This file contains hidden or 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 | |
| declare(strict_types=1); | |
| namespace PcComponentes\AfterSales\Tests\Application\CreateRepairCenterByFamilyAssignment; | |
| use InvalidArgumentException; | |
| use PcComponentes\AfterSales\Application\CreateRepairCenterByFamilyAssignment\CreateRepairCenterByFamilyAssignmentCommand; | |
| use PcComponentes\AfterSales\Application\CreateRepairCenterByFamilyAssignment\CreateRepairCenterByFamilyAssignmentCommandHandler; | |
| use PcComponentes\AfterSales\Domain\Service\RepairCenterByFamilyAssigementCreator; | |
| use PHPUnit\Framework\TestCase; | |
| class CreateRepairCenterByFamilyAssignmentCommandHandlerTest extends TestCase | |
| { | |
| public function test_given_create_repair_center_by_family_assignment_command_handler_when_invoke_whith_correct_payload_then_save() | |
| { | |
| $validJson = ' | |
| { | |
| "id": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "familyId": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "caseType": 1, | |
| "channel": "ES", | |
| "repairCenterId": 1 | |
| } | |
| '; | |
| $serviceMocked = $this->getMockBuilder( | |
| RepairCenterByFamilyAssigementCreator::class, | |
| )->disableOriginalConstructor()->getMock(); | |
| $serviceMocked | |
| ->expects(self::once()) | |
| ->method('__invoke'); | |
| $handler = new CreateRepairCenterByFamilyAssignmentCommandHandler( | |
| $serviceMocked, | |
| ); | |
| $payload = \json_decode( | |
| $validJson, | |
| true, | |
| 512, | |
| \JSON_THROW_ON_ERROR, | |
| ); | |
| $command = CreateRepairCenterByFamilyAssignmentCommand::fromPayload($payload); | |
| $handler($command); | |
| } | |
| /** | |
| * @dataProvider invalidJsons | |
| * @throws \JsonException | |
| */ | |
| public function test_given_create_repair_center_by_family_assignment_command_handler_when_invoke_whith_non_valid_channel_then_throws_assert_invalid_argument_exception( | |
| $invalidJson | |
| ) { | |
| $this->expectException(InvalidArgumentException::class); | |
| $serviceMocked = $optionCreatorMock = $this->getMockBuilder( | |
| RepairCenterByFamilyAssigementCreator::class, | |
| )->disableOriginalConstructor()->getMock(); | |
| $handler = new CreateRepairCenterByFamilyAssignmentCommandHandler( | |
| $serviceMocked, | |
| ); | |
| $payload = \json_decode( | |
| $invalidJson, | |
| true, | |
| 512, | |
| \JSON_THROW_ON_ERROR, | |
| ); | |
| $command = CreateRepairCenterByFamilyAssignmentCommand::fromPayload($payload); | |
| $handler($command); | |
| } | |
| public function invalidJsons() | |
| { | |
| return [ | |
| //invalid channel | |
| [ | |
| '{ | |
| "id": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "familyId": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "caseType": 1, | |
| "channel": "RU", | |
| "repairCenterId": 1 | |
| }', | |
| ], | |
| //invalid caseType lower than 1 | |
| [ | |
| '{ | |
| "id": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "familyId": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "caseType": 0, | |
| "channel": "ES", | |
| "repairCenterId": 1 | |
| }', | |
| ], | |
| //invalid caseType higher than 5 | |
| [ | |
| '{ | |
| "id": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "familyId": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "caseType": 6, | |
| "channel": "ES", | |
| "repairCenterId": 1 | |
| }', | |
| ], | |
| //blank repairCenterId | |
| [ | |
| '{ | |
| "id": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "familyId": "6ac9e203-e751-45ce-87c3-869655f8106d", | |
| "caseType": 6, | |
| "channel": "ES", | |
| "repairCenterId": "" | |
| }', | |
| ], | |
| ]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment