Skip to content

Commit

Permalink
ENGCOM-3065: [Backport] Sales: add missing unit tests for model classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Idolov authored Oct 2, 2018
2 parents d73e7c2 + 4905eaf commit 66a25dd
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Test\Unit\Model;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Sales\Model\ValidatorResultInterface;
use Magento\Sales\Model\ValidatorResultInterfaceFactory;
use Magento\Sales\Model\ValidatorResultMerger;

/**
* @covers \Magento\Sales\Model\ValidatorResultMerger
*/
class ValidatorResultMergerTest extends \PHPUnit\Framework\TestCase
{
/**
* Testable Object
*
* @var ValidatorResultMerger
*/
private $validatorResultMerger;

/**
* Object Manager
*
* @var ObjectManager
*/
private $objectManager;

/**
* @var ValidatorResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $validatorResultFactoryMock;

/**
* Set Up
*
* @return void
*/
protected function setUp()
{
$this->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);
$validationResult = $this->createMock(ValidatorResultInterface::class);
$cmValidationResult = $this->createMock(ValidatorResultInterface::class);
$validationMessages = [['test04', 'test05'], ['test06']];
$this->validatorResultFactoryMock->expects($this->once())->method('create')
->willReturn($validatorResultMock);
$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');
$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(
$validationResult,
$cmValidationResult,
...$validationMessages
);
$this->assertEquals($expected, $actual);
}
}
75 changes: 75 additions & 0 deletions app/code/Magento/Sales/Test/Unit/Model/ValidatorResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Test\Unit\Model;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Sales\Model\ValidatorResult;

/**
* @covers \Magento\Sales\Model\ValidatorResult
*/
class ValidatorResultTest extends \PHPUnit\Framework\TestCase
{
/**
* Testable Object
*
* @var ValidatorResult
*/
private $validatorResult;

/**
* Object Manager
*
* @var ObjectManager
*/
private $objectManager;

/**
* Set Up
*
* @return void
*/
protected function setUp()
{
$this->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());
}
}

0 comments on commit 66a25dd

Please sign in to comment.