Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ValidatorChainTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 use Magento\Framework\Validation\ValidationResultFactory;
14 use PHPUnit\Framework\TestCase;
15 
16 class ValidatorChainTest extends TestCase
17 {
21  private $qtyValidator;
22 
26  private $skuValidator;
27 
31  private $validationResultFactory;
32 
36  private $validatorChain;
37 
41  protected function setUp()
42  {
43  $this->validationResultFactory = $this->getMockBuilder(ValidationResultFactory::class)->getMock();
44  $this->qtyValidator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
45  $this->skuValidator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
46  }
47 
49  {
50  $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class);
51  $this->validationResultFactory->expects($this->once())
52  ->method('create')
53  ->with(['errors' =>[]])
54  ->willReturn($emptyValidatorResult);
55 
56  $this->validatorChain = (new ObjectManager($this))->getObject(
57  ValidatorChain::class,
58  [
59  'validationResultFactory' => $this->validationResultFactory,
60  'validators' => []
61  ]
62  );
63 
64  $result = $this->validatorChain->validate([], 1);
65  $this->assertEquals($emptyValidatorResult, $result);
66  }
67 
68  public function testValidateWithOutErros()
69  {
70  $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class);
71  $emptyValidatorResult->expects($this->once())->method('isValid')
72  ->willReturn(true);
73 
74  $this->validationResultFactory->expects($this->once())
75  ->method('create')
76  ->with(['errors' => []])
77  ->willReturn($emptyValidatorResult);
78 
79  $this->qtyValidator->method('validate')
80  ->willReturn($emptyValidatorResult);
81 
82  $this->validatorChain = (new ObjectManager($this))->getObject(
83  ValidatorChain::class,
84  [
85  'validationResultFactory' => $this->validationResultFactory,
86  'validators' => [$this->qtyValidator]
87  ]
88  );
89 
90  $result = $this->validatorChain->validate([], 1);
91  $this->assertEquals($emptyValidatorResult, $result);
92  }
93 
94  public function testValidateWithErros()
95  {
96  $validatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class);
97 
98  $validatorResult->expects($this->once())->method('isValid')
99  ->willReturn(false);
100 
101  $validatorResult->expects($this->once())
102  ->method('getErrors')
103  ->willReturn(['Qty can not negative', 'Additional error']);
104 
105  $this->qtyValidator->expects($this->once())->method('validate')
106  ->willReturn($validatorResult);
107 
108  $this->validationResultFactory->expects($this->once())
109  ->method('create')
110  ->with(['errors' => ['Qty can not negative', 'Additional error']])
111  ->willReturn($validatorResult);
112 
113  $this->validatorChain = (new ObjectManager($this))->getObject(
114  ValidatorChain::class,
115  [
116  'validationResultFactory' => $this->validationResultFactory,
117  'validators' => [$this->qtyValidator]
118  ]
119  );
120 
121  $result = $this->validatorChain->validate([-1], 1);
122  $this->assertEquals($validatorResult, $result);
123  }
124 }