Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeValidationTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 
18 class AttributeValidationTest extends \PHPUnit\Framework\TestCase
19 {
23  private $attributeValidation;
24 
28  private $storeManagerMock;
29 
33  private $storeMock;
34 
38  private $allowedEntityTypes;
39 
43  private $proceedMock;
44 
48  private $isProceedMockCalled = false;
49 
53  private $subjectMock;
54 
58  private $attributeMock;
59 
63  private $entityMock;
64 
65  protected function setUp()
66  {
67  $objectManager = new ObjectManager($this);
68 
69  $this->attributeMock = $this->getMockBuilder(AbstractBackend::class)
70  ->setMethods(['getAttributeCode'])
71  ->getMockForAbstractClass();
72  $this->subjectMock = $this->getMockBuilder(AbstractBackend::class)
73  ->setMethods(['getAttribute'])
74  ->getMockForAbstractClass();
75  $this->subjectMock->expects($this->any())
76  ->method('getAttribute')
77  ->willReturn($this->attributeMock);
78 
79  $this->storeMock = $this->getMockBuilder(StoreInterface::class)
80  ->setMethods(['getId'])
81  ->getMockForAbstractClass();
82  $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
83  ->setMethods(['getStore'])
84  ->getMockForAbstractClass();
85  $this->storeManagerMock->expects($this->any())
86  ->method('getStore')
87  ->willReturn($this->storeMock);
88 
89  $this->entityMock = $this->getMockBuilder(DataObject::class)
90  ->setMethods(['getData'])
91  ->getMock();
92 
93  $this->allowedEntityTypes = [$this->entityMock];
94 
95  $this->proceedMock = function () {
96  $this->isProceedMockCalled = true;
97  };
98 
99  $this->attributeValidation = $objectManager->getObject(
100  AttributeValidation::class,
101  [
102  'storeManager' => $this->storeManagerMock,
103  'allowedEntityTypes' => $this->allowedEntityTypes,
104  ]
105  );
106  }
107 
116  public function testAroundValidate(bool $shouldProceedRun, bool $defaultStoreUsed, $storeId)
117  {
118  $this->isProceedMockCalled = false;
119  $attributeCode = 'code';
120 
121  $this->storeMock->expects($this->once())
122  ->method('getId')
123  ->willReturn($storeId);
124  if ($defaultStoreUsed) {
125  $this->attributeMock->expects($this->once())
126  ->method('getAttributeCode')
127  ->willReturn($attributeCode);
128  $this->entityMock->expects($this->at(0))
129  ->method('getData')
130  ->willReturn([$attributeCode => null]);
131  $this->entityMock->expects($this->at(1))
132  ->method('getData')
133  ->with($attributeCode)
134  ->willReturn(null);
135  }
136 
137  $this->attributeValidation->aroundValidate($this->subjectMock, $this->proceedMock, $this->entityMock);
138  $this->assertSame($shouldProceedRun, $this->isProceedMockCalled);
139  }
140 
145  public function aroundValidateDataProvider(): array
146  {
147  return [
148  [true, false, '0'],
149  [true, false, 0],
150  [true, false, null],
151  [false, true, 1],
152  ];
153  }
154 }
$objectManager
Definition: bootstrap.php:17
testAroundValidate(bool $shouldProceedRun, bool $defaultStoreUsed, $storeId)
$attributeCode
Definition: extend.phtml:12