From 231f09708bd219715bdc22bc81cf5c95f68328e0 Mon Sep 17 00:00:00 2001 From: Dmytro Cheshun Date: Sun, 16 Sep 2018 10:49:49 +0300 Subject: [PATCH 1/3] Sales: add missing unit tests for model classes --- .../Unit/Model/ValidatorResultMergerTest.php | 87 +++++++++++++++++++ .../Test/Unit/Model/ValidatorResultTest.php | 75 ++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php create mode 100644 app/code/Magento/Sales/Test/Unit/Model/ValidatorResultTest.php diff --git a/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php b/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php new file mode 100644 index 0000000000000..4236890a2a37d --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php @@ -0,0 +1,87 @@ +validatorResultFactoryMock = $this->getMockBuilder(ValidatorResultInterfaceFactory::class) + ->setMethods(['create'])->disableOriginalConstructor()->getMock(); + $this->objectManager = new ObjectManager($this); + $this->validatorResultMerger = $this->objectManager->getObject( + ValidatorResultMerger::class, + [ + 'validatorResultInterfaceFactory' => $this->validatorResultFactoryMock, + ] + ); + } + + /** + * Test merge method + * + * @return void + */ + public function testMerge() + { + $validatorResultMock = $this->createMock(ValidatorResultInterface::class); + $orderValidationResultMock = $this->createMock(ValidatorResultInterface::class); + $creditmemoValidationResultMock = $this->createMock(ValidatorResultInterface::class); + $itemsValidationMessages = [['test04', 'test05'], ['test06']]; + $this->validatorResultFactoryMock->expects($this->once())->method('create') + ->willReturn($validatorResultMock); + $orderValidationResultMock->expects($this->once())->method('getMessages')->willReturn(['test01', 'test02']); + $creditmemoValidationResultMock->expects($this->once())->method('getMessages')->willReturn(['test03']); + + $validatorResultMock->expects($this->at(0))->method('addMessage')->with('test01'); + $validatorResultMock->expects($this->at(1))->method('addMessage')->with('test02'); + $validatorResultMock->expects($this->at(2))->method('addMessage')->with('test03'); + $validatorResultMock->expects($this->at(3))->method('addMessage')->with('test04'); + $validatorResultMock->expects($this->at(4))->method('addMessage')->with('test05'); + $validatorResultMock->expects($this->at(5))->method('addMessage')->with('test06'); + $expected = $validatorResultMock; + $actual = $this->validatorResultMerger->merge( + $orderValidationResultMock, + $creditmemoValidationResultMock, + ...$itemsValidationMessages + ); + $this->assertEquals($expected, $actual); + } +} diff --git a/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultTest.php b/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultTest.php new file mode 100644 index 0000000000000..f4ab2d4f48e6f --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultTest.php @@ -0,0 +1,75 @@ +objectManager = new ObjectManager($this); + $this->validatorResult = $this->objectManager->getObject(ValidatorResult::class); + } + + /** + * Test addMessage method + * + * @return void + */ + public function testAddMessages() + { + $messageFirst = 'Sample message 01.'; + $messageSecond = 'Sample messages 02.'; + $messageThird = 'Sample messages 03.'; + $expected = [$messageFirst, $messageSecond, $messageThird]; + $this->validatorResult->addMessage($messageFirst); + $this->validatorResult->addMessage($messageSecond); + $this->validatorResult->addMessage($messageThird); + $actual = $this->validatorResult->getMessages(); + $this->assertEquals($expected, $actual); + } + + /** + * Test hasMessages method + * + * @return void + */ + public function testHasMessages() + { + $this->assertFalse($this->validatorResult->hasMessages()); + $messageFirst = 'Sample message 01.'; + $messageSecond = 'Sample messages 02.'; + $this->validatorResult->addMessage($messageFirst); + $this->validatorResult->addMessage($messageSecond); + $this->assertTrue($this->validatorResult->hasMessages()); + } +} From c65b5a412670900b515f018b368987d902e08d3f Mon Sep 17 00:00:00 2001 From: Dmytro Cheshun Date: Sat, 29 Sep 2018 09:26:54 +0300 Subject: [PATCH 2/3] Fix excessively long variable names --- .../Test/Unit/Model/ValidatorResultMergerTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php b/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php index 4236890a2a37d..e30d45844bf09 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php @@ -62,13 +62,13 @@ protected function setUp() public function testMerge() { $validatorResultMock = $this->createMock(ValidatorResultInterface::class); - $orderValidationResultMock = $this->createMock(ValidatorResultInterface::class); - $creditmemoValidationResultMock = $this->createMock(ValidatorResultInterface::class); + $validationResult = $this->createMock(ValidatorResultInterface::class); + $cmValidationResult = $this->createMock(ValidatorResultInterface::class); $itemsValidationMessages = [['test04', 'test05'], ['test06']]; $this->validatorResultFactoryMock->expects($this->once())->method('create') ->willReturn($validatorResultMock); - $orderValidationResultMock->expects($this->once())->method('getMessages')->willReturn(['test01', 'test02']); - $creditmemoValidationResultMock->expects($this->once())->method('getMessages')->willReturn(['test03']); + $validationResult->expects($this->once())->method('getMessages')->willReturn(['test01', 'test02']); + $cmValidationResult->expects($this->once())->method('getMessages')->willReturn(['test03']); $validatorResultMock->expects($this->at(0))->method('addMessage')->with('test01'); $validatorResultMock->expects($this->at(1))->method('addMessage')->with('test02'); @@ -78,8 +78,8 @@ public function testMerge() $validatorResultMock->expects($this->at(5))->method('addMessage')->with('test06'); $expected = $validatorResultMock; $actual = $this->validatorResultMerger->merge( - $orderValidationResultMock, - $creditmemoValidationResultMock, + $validationResult, + $cmValidationResult, ...$itemsValidationMessages ); $this->assertEquals($expected, $actual); From 828e742371da1c42fa1e4b2bc07c876b410df099 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Sat, 29 Sep 2018 12:18:26 +0200 Subject: [PATCH 3/3] Minor code style fix --- .../Sales/Test/Unit/Model/ValidatorResultMergerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php b/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php index e30d45844bf09..ca2dd0a31b1e7 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/ValidatorResultMergerTest.php @@ -44,7 +44,7 @@ class ValidatorResultMergerTest extends \PHPUnit\Framework\TestCase protected function setUp() { $this->validatorResultFactoryMock = $this->getMockBuilder(ValidatorResultInterfaceFactory::class) - ->setMethods(['create'])->disableOriginalConstructor()->getMock(); + ->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->objectManager = new ObjectManager($this); $this->validatorResultMerger = $this->objectManager->getObject( ValidatorResultMerger::class, @@ -64,7 +64,7 @@ public function testMerge() $validatorResultMock = $this->createMock(ValidatorResultInterface::class); $validationResult = $this->createMock(ValidatorResultInterface::class); $cmValidationResult = $this->createMock(ValidatorResultInterface::class); - $itemsValidationMessages = [['test04', 'test05'], ['test06']]; + $validationMessages = [['test04', 'test05'], ['test06']]; $this->validatorResultFactoryMock->expects($this->once())->method('create') ->willReturn($validatorResultMock); $validationResult->expects($this->once())->method('getMessages')->willReturn(['test01', 'test02']); @@ -80,7 +80,7 @@ public function testMerge() $actual = $this->validatorResultMerger->merge( $validationResult, $cmValidationResult, - ...$itemsValidationMessages + ...$validationMessages ); $this->assertEquals($expected, $actual); }