Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigFactoryTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class ConfigFactoryTest extends \PHPUnit\Framework\TestCase
11 {
14 
16  protected $metadataProvider;
17 
20 
23 
25  protected $designConfig;
26 
28  protected $designConfigData;
29 
32 
34  protected $scopeValidator;
35 
37  protected $factory;
38 
40  protected $storeManager;
41 
43  protected $website;
44 
45  public function setUp()
46  {
47  $this->designConfigFactory = $this->createPartialMock(
48  \Magento\Theme\Api\Data\DesignConfigInterfaceFactory::class,
49  ['create']
50  );
51  $this->metadataProvider = $this->getMockForAbstractClass(
52  \Magento\Theme\Model\Design\Config\MetadataProviderInterface::class,
53  [],
54  '',
55  false
56  );
57  $this->designConfigDataFactory = $this->createPartialMock(
58  \Magento\Theme\Api\Data\DesignConfigDataInterfaceFactory::class,
59  ['create']
60  );
61  $this->configExtensionFactory = $this->createPartialMock(
62  \Magento\Theme\Api\Data\DesignConfigExtensionFactory::class,
63  ['create']
64  );
65  $this->designConfig = $this->getMockForAbstractClass(
66  \Magento\Theme\Api\Data\DesignConfigInterface::class,
67  [],
68  '',
69  false
70  );
71  $this->designConfigData = $this->getMockForAbstractClass(
72  \Magento\Theme\Api\Data\DesignConfigDataInterface::class,
73  [],
74  '',
75  false
76  );
77  $this->designConfigExtension = $this->getMockForAbstractClass(
78  \Magento\Theme\Api\Data\DesignConfigExtension::class,
79  [],
80  '',
81  false,
82  false,
83  true,
84  ['setDesignConfigData']
85  );
86  $this->scopeValidator = $this->getMockBuilder(\Magento\Framework\App\ScopeValidatorInterface::class)
87  ->getMockForAbstractClass();
88  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
89  ->getMockForAbstractClass();
90  $this->website = $this->getMockBuilder(\Magento\Store\Api\Data\WebsiteInterface::class)
91  ->getMockForAbstractClass();
92 
93  $this->factory = new ConfigFactory(
94  $this->designConfigFactory,
95  $this->metadataProvider,
96  $this->designConfigDataFactory,
97  $this->configExtensionFactory,
98  $this->scopeValidator,
99  $this->storeManager
100  );
101  }
102 
103  public function testCreate()
104  {
105  $scope = 'default';
106  $scopeId = 0;
107  $data = [
108  'header_default_title' => 'value'
109  ];
110  $metadata = [
111  'header_default_title' => [
112  'path' => 'design/header/default_title',
113  'fieldset' => 'head'
114  ],
115  'head_default_description' => [
116  'path' => 'design/head/default_description',
117  'fieldset' => 'head'
118  ],
119  ];
120 
121  $this->scopeValidator->expects($this->once())
122  ->method('isValidScope')
123  ->with($scope, $scopeId)
124  ->willReturn(true);
125  $this->storeManager->expects($this->once())
126  ->method('isSingleStoreMode')
127  ->willReturn(false);
128 
129  $this->designConfigFactory->expects($this->once())
130  ->method('create')
131  ->willReturn($this->designConfig);
132  $this->designConfig->expects($this->once())
133  ->method('setScope')
134  ->willReturn('default');
135  $this->designConfig->expects($this->once())
136  ->method('setScopeId')
137  ->willReturn(0);
138  $this->metadataProvider->expects($this->once())
139  ->method('get')
140  ->willReturn($metadata);
141  $this->designConfigDataFactory->expects($this->exactly(2))
142  ->method('create')
143  ->willReturn($this->designConfigData);
144  $this->designConfigData->expects($this->exactly(2))
145  ->method('setPath')
146  ->withConsecutive(
147  ['design/header/default_title'],
148  ['design/head/default_description']
149  );
150  $this->designConfigData->expects($this->exactly(2))
151  ->method('setFieldConfig')
152  ->withConsecutive(
153  [
154  [
155  'path' => 'design/header/default_title',
156  'fieldset' => 'head',
157  'field' => 'header_default_title'
158  ]
159  ],
160  [
161  [
162  'path' => 'design/head/default_description',
163  'fieldset' => 'head',
164  'field' => 'head_default_description'
165  ]
166  ]
167  );
168  $this->designConfigData->expects($this->once())
169  ->method('setValue')
170  ->with('value');
171  $this->configExtensionFactory->expects($this->once())
172  ->method('create')
173  ->willReturn($this->designConfigExtension);
174  $this->designConfigExtension->expects($this->once())
175  ->method('setDesignConfigData')
176  ->with([$this->designConfigData, $this->designConfigData]);
177  $this->designConfig->expects($this->once())
178  ->method('setExtensionAttributes')
179  ->with($this->designConfigExtension);
180  $this->assertSame($this->designConfig, $this->factory->create($scope, $scopeId, $data));
181  }
182 
183  public function testCreateInSingleStoreMode()
184  {
185  $scope = 'default';
186  $scopeId = 0;
187  $data = [
188  'header_default_title' => 'value'
189  ];
190  $metadata = [
191  'header_default_title' => [
192  'path' => 'design/header/default_title',
193  'fieldset' => 'head'
194  ],
195  'head_default_description' => [
196  'path' => 'design/head/default_description',
197  'fieldset' => 'head'
198  ],
199  ];
200 
201  $this->scopeValidator->expects($this->once())
202  ->method('isValidScope')
203  ->with($scope, $scopeId)
204  ->willReturn(true);
205  $this->storeManager->expects($this->once())
206  ->method('isSingleStoreMode')
207  ->willReturn(true);
208  $this->storeManager->expects($this->once())
209  ->method('getWebsites')
210  ->willReturn([$this->website]);
211  $this->website->expects($this->once())
212  ->method('getId')
213  ->willReturn(1);
214 
215  $this->designConfigFactory->expects($this->once())
216  ->method('create')
217  ->willReturn($this->designConfig);
218  $this->designConfig->expects($this->once())
219  ->method('setScope')
220  ->willReturn('websites');
221  $this->designConfig->expects($this->once())
222  ->method('setScopeId')
223  ->willReturn(1);
224  $this->metadataProvider->expects($this->once())
225  ->method('get')
226  ->willReturn($metadata);
227  $this->designConfigDataFactory->expects($this->exactly(2))
228  ->method('create')
229  ->willReturn($this->designConfigData);
230  $this->designConfigData->expects($this->exactly(2))
231  ->method('setPath')
232  ->withConsecutive(
233  ['design/header/default_title'],
234  ['design/head/default_description']
235  );
236  $this->designConfigData->expects($this->exactly(2))
237  ->method('setFieldConfig')
238  ->withConsecutive(
239  [
240  [
241  'path' => 'design/header/default_title',
242  'fieldset' => 'head',
243  'field' => 'header_default_title'
244  ]
245  ],
246  [
247  [
248  'path' => 'design/head/default_description',
249  'fieldset' => 'head',
250  'field' => 'head_default_description'
251  ]
252  ]
253  );
254  $this->designConfigData->expects($this->once())
255  ->method('setValue')
256  ->with('value');
257  $this->configExtensionFactory->expects($this->once())
258  ->method('create')
259  ->willReturn($this->designConfigExtension);
260  $this->designConfigExtension->expects($this->once())
261  ->method('setDesignConfigData')
262  ->with([$this->designConfigData, $this->designConfigData]);
263  $this->designConfig->expects($this->once())
264  ->method('setExtensionAttributes')
265  ->with($this->designConfigExtension);
266  $this->assertSame($this->designConfig, $this->factory->create($scope, $scopeId, $data));
267  }
268 }