Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
CodeValidatorTest.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 Magento\InventoryApi\Api\Data\SourceInterfaceFactory;
15 use PHPUnit\Framework\TestCase;
16 
17 class CodeValidatorTest extends TestCase
18 {
19 
23  private $codeValidator;
24 
28  private $source;
29 
33  private $validationResultFactory;
34 
35  protected function setUp()
36  {
37  $this->validationResultFactory = $this->getMockBuilder(ValidationResultFactory::class)->getMock();
38  $this->source = $this->getMockBuilder(SourceInterface::class)->getMock();
39  }
40 
41  public function testValidateCodeNotEmpty()
42  {
43  $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class);
44  $this->validationResultFactory->expects($this->once())
45  ->method('create')
46  ->with(['errors' => [__('"%field" can not be empty.', ['field' => SourceInterface::SOURCE_CODE])]])
47  ->willReturn($emptyValidatorResult);
48  $this->codeValidator = (new ObjectManager($this))->getObject(CodeValidator::class, [
49  'validationResultFactory' => $this->validationResultFactory
50  ]);
51 
52  $this->source->expects($this->once())
53  ->method('getSourceCode')
54  ->willReturn(' ');
55  $this->codeValidator->validate($this->source);
56  }
57 
59  {
60  $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class);
61  $this->validationResultFactory->expects($this->once())
62  ->method('create')
63  ->with([
64  'errors' => [__('"%field" can not contain whitespaces.', ['field' => SourceInterface::SOURCE_CODE])]
65  ])
66  ->willReturn($emptyValidatorResult);
67  $this->codeValidator = (new ObjectManager($this))->getObject(CodeValidator::class, [
68  'validationResultFactory' => $this->validationResultFactory
69  ]);
70  $this->source->expects($this->once())
71  ->method('getSourceCode')
72  ->willReturn(' source code ');
73  $this->codeValidator->validate($this->source);
74  }
75 
76  public function testValidateCodeSuccessfully()
77  {
78  $emptyValidatorResult = $this->createMock(\Magento\Framework\Validation\ValidationResult::class);
79  $this->validationResultFactory->expects($this->once())
80  ->method('create')
81  ->willReturn($emptyValidatorResult);
82  $this->codeValidator = (new ObjectManager($this))->getObject(CodeValidator::class, [
83  'validationResultFactory' => $this->validationResultFactory
84  ]);
85  $this->source->expects($this->once())
86  ->method('getSourceCode')
87  ->willReturn(' source_code ');
88 
89  $result = $this->codeValidator->validate($this->source);
90  $errors = $result->getErrors();
91  $this->assertCount(0, $errors);
92  }
93 }
__()
Definition: __.php:13
$errors
Definition: overview.phtml:9