Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
ValidatorTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 use Magento\Sales\Model\ValidatorResultInterfaceFactory;
19 
23 class ValidatorTest extends \PHPUnit\Framework\TestCase
24 {
30  private $validator;
31 
37  private $objectManager;
38 
42  private $objectManagerMock;
43 
47  private $validatorResultFactoryMock;
48 
52  private $validatorResultMock;
53 
57  private $validatorMock;
58 
62  private $entityMock;
63 
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 
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 
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 }
__()
Definition: __.php:13
$message