Skip to content

Commit 08cc7fe

Browse files
author
Stanislav Idolov
authored
ENGCOM-2928: [Backport] Sales: Add unit test for validator model class #17940
2 parents 32ea761 + 088b952 commit 08cc7fe

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sales\Test\Unit\Model;
8+
9+
use Magento\Framework\DataObject;
10+
use Magento\Framework\Exception\ConfigurationMismatchException;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Sales\Api\Data\OrderInterface;
14+
use Magento\Sales\Model\Validator;
15+
use Magento\Sales\Model\ValidatorInterface;
16+
use Magento\Sales\Model\ValidatorResultInterface;
17+
use Magento\Sales\Model\ValidatorResultInterfaceFactory;
18+
19+
/**
20+
* @covers \Magento\Sales\Model\Validator
21+
*/
22+
class ValidatorTest extends \PHPUnit_Framework_TestCase
23+
{
24+
/**
25+
* Testable Object
26+
*
27+
* @var Validator
28+
*/
29+
private $validator;
30+
31+
/**
32+
* Object Manager
33+
*
34+
* @var ObjectManager
35+
*/
36+
private $objectManager;
37+
38+
/**
39+
* @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $objectManagerMock;
42+
43+
/**
44+
* @var ValidatorResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $validatorResultFactoryMock;
47+
48+
/**
49+
* @var ValidatorResultInterface|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
private $validatorResultMock;
52+
53+
/**
54+
* @var ValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
55+
*/
56+
private $validatorMock;
57+
58+
/**
59+
* @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject
60+
*/
61+
private $entityMock;
62+
63+
/**
64+
* Set Up
65+
*
66+
* @return void
67+
*/
68+
protected function setUp()
69+
{
70+
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
71+
->disableOriginalConstructor()->getMock();
72+
$this->entityMock = $this->getMockBuilder(OrderInterface::class)
73+
->disableOriginalConstructor()->getMock();
74+
$this->validatorMock = $this->getMockBuilder(ValidatorInterface::class)
75+
->disableOriginalConstructor()->getMock();
76+
$this->validatorResultFactoryMock = $this->getMockBuilder(ValidatorResultInterfaceFactory::class)
77+
->setMethods(['create'])->disableOriginalConstructor()->getMock();
78+
$this->validatorResultMock = $this->getMockBuilder(ValidatorResultInterface::class)
79+
->disableOriginalConstructor()->getMock();
80+
$this->validatorResultFactoryMock->expects($this->any())->method('create')
81+
->willReturn($this->validatorResultMock);
82+
$this->objectManager = new ObjectManager($this);
83+
$this->validator = $this->objectManager->getObject(
84+
Validator::class,
85+
[
86+
'objectManager' => $this->objectManagerMock,
87+
'validatorResult' => $this->validatorResultFactoryMock,
88+
]
89+
);
90+
}
91+
92+
/**
93+
* Test validate method
94+
*
95+
* @return void
96+
*
97+
* @throws ConfigurationMismatchException
98+
*/
99+
public function testValidate()
100+
{
101+
$validatorName = 'test';
102+
$validators = [$validatorName];
103+
$context = new DataObject();
104+
$validatorArguments = ['context' => $context];
105+
$message = __('Sample message.');
106+
$messages = [$message];
107+
108+
$this->objectManagerMock->expects($this->once())->method('create')
109+
->with($validatorName, $validatorArguments)->willReturn($this->validatorMock);
110+
$this->validatorMock->expects($this->once())->method('validate')->with($this->entityMock)
111+
->willReturn($messages);
112+
$this->validatorResultMock->expects($this->once())->method('addMessage')->with($message);
113+
114+
$expected = $this->validatorResultMock;
115+
$actual = $this->validator->validate($this->entityMock, $validators, $context);
116+
$this->assertEquals($expected, $actual);
117+
}
118+
119+
/**
120+
* Test validate method
121+
*
122+
* @return void
123+
*
124+
* @throws ConfigurationMismatchException
125+
*/
126+
public function testValidateWithException()
127+
{
128+
$validatorName = 'test';
129+
$validators = [$validatorName];
130+
$this->objectManagerMock->expects($this->once())->method('create')->willReturn(null);
131+
$this->validatorResultMock->expects($this->never())->method('addMessage');
132+
$this->setExpectedException(ConfigurationMismatchException::class);
133+
$this->validator->validate($this->entityMock, $validators);
134+
}
135+
}

0 commit comments

Comments
 (0)