Skip to content

Commit 5dc47da

Browse files
author
Stanislav Idolov
authored
ENGCOM-2907: Sales: Add unit test for validator model class #17876
2 parents 6a3f845 + cdc0f46 commit 5dc47da

File tree

1 file changed

+132
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)