Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackendModelFactoryTest.php
Go to the documentation of this file.
1 <?php
7 
8 class BackendModelFactoryTest extends \PHPUnit\Framework\TestCase
9 {
11  protected $model;
12 
14  protected $objectManagerMock;
15 
18 
23 
25  protected $collection;
26 
28  protected $backendModel;
29 
30  protected function setUp()
31  {
32  $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
33  ->getMockForAbstractClass();
34  $this->metadataProviderMock = $this->getMockBuilder(\Magento\Theme\Model\Design\Config\MetadataProvider::class)
35  ->disableOriginalConstructor()
36  ->getMock();
37  $this->collectionFactoryMock = $this->getMockBuilder(
38  \Magento\Theme\Model\ResourceModel\Design\Config\CollectionFactory::class
39  )
40  ->disableOriginalConstructor()
41  ->setMethods(['create'])
42  ->getMock();
43  $this->collection = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Design\Config\Collection::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46  $this->backendModel = $this->getMockBuilder(\Magento\Framework\App\Config\Value::class)
47  ->disableOriginalConstructor()
48  ->setMethods(['setValue'])
49  ->getMock();
50 
51  $this->model = new \Magento\Theme\Model\Design\BackendModelFactory(
52  $this->objectManagerMock,
53  $this->metadataProviderMock,
54  $this->collectionFactoryMock
55  );
56  }
57 
58  public function testCreate()
59  {
60  $scope = 'website';
61  $scopeId = 1;
62  $data = [
63  'scope' => $scope,
64  'scopeId' => $scopeId,
65  'value' => 'value',
66  'config' => [
67  'path' => 'design/head/default_title',
68  'backend_model' => \Magento\Framework\App\Config\Value::class
69  ]
70  ];
71  $this->metadataProviderMock->expects($this->once())
72  ->method('get')
73  ->willReturn([
74  'head_default_title' => [
75  'path' => 'design/head/default_title'
76  ]
77  ]);
78  $this->collectionFactoryMock->expects($this->once())
79  ->method('create')
80  ->willReturn($this->collection);
81  $this->collection->expects($this->once())
82  ->method('addPathsFilter')
83  ->with(['head_default_title' => 'design/head/default_title']);
84  $this->collection->expects($this->once())
85  ->method('addFieldToFilter')
86  ->with('scope', $scope);
87  $this->collection->expects($this->once())
88  ->method('addScopeIdFilter')
89  ->with($scopeId);
90  $this->collection->expects($this->once())
91  ->method('getData')
92  ->willReturn([
93  [
94  'config_id' => 1,
95  'path' => 'design/head/default_title'
96  ]
97  ]);
98  $this->objectManagerMock->expects($this->once())
99  ->method('create')
100  ->with(
101  \Magento\Framework\App\Config\Value::class,
102  [
103  'data' => [
104  'path' => 'design/head/default_title',
105  'scope' => $scope,
106  'scope_id' => $scopeId,
107  'field_config' => $data['config'],
108  'config_id' => 1
109  ]
110  ]
111  )
112  ->willReturn($this->backendModel);
113  $this->backendModel->expects($this->once())
114  ->method('setValue')
115  ->willReturn('value');
116  $this->assertSame($this->backendModel, $this->model->create($data));
117  }
118 
119  public function testCreateByPath()
120  {
121  $path = 'design/head/default_title';
122  $backendModelType = \Magento\Theme\Model\Design\Backend\Exceptions::class;
123  $backendModel = $this->getMockBuilder($backendModelType)
124  ->disableOriginalConstructor()
125  ->getMock();
126 
127  $this->metadataProviderMock->expects($this->once())
128  ->method('get')
129  ->willReturn([
130  'head_default_title' => [
131  'path' => $path,
132  'backend_model' => $backendModelType
133  ]
134  ]);
135  $this->objectManagerMock->expects($this->once())
136  ->method('create')
137  ->with($backendModelType, ['data' => []])
138  ->willReturn($backendModel);
139  $this->assertEquals($backendModel, $this->model->createByPath($path));
140  }
141 }