Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GroupedCollectionTest.php
Go to the documentation of this file.
1 <?php
7 
8 class GroupedCollectionTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_object;
14 
18  protected $_asset;
19 
20  protected function setUp()
21  {
22  $factory = $this->createMock(\Magento\Framework\View\Asset\PropertyGroupFactory::class);
23  $factory->expects(
24  $this->any()
25  )->method(
26  'create'
27  )->will(
28  $this->returnCallback([$this, 'createAssetGroup'])
29  );
30  $this->_object = new \Magento\Framework\View\Asset\GroupedCollection($factory);
31  $this->_asset = new \Magento\Framework\View\Asset\Remote('http://127.0.0.1/magento/test.css');
32  $this->_object->add('asset', $this->_asset);
33  }
34 
35  protected function tearDown()
36  {
37  $this->_object = null;
38  $this->_asset = null;
39  }
40 
47  public function createAssetGroup(array $arguments)
48  {
49  return new \Magento\Framework\View\Asset\PropertyGroup($arguments['properties']);
50  }
51 
58  protected function _assertGroups(array $expectedGroups, array $actualGroupObjects)
59  {
60  $this->assertInternalType('array', $actualGroupObjects);
61  $actualGroups = [];
63  foreach ($actualGroupObjects as $actualGroup) {
64  $this->assertInstanceOf(\Magento\Framework\View\Asset\PropertyGroup::class, $actualGroup);
65  $actualGroups[] = ['properties' => $actualGroup->getProperties(), 'assets' => $actualGroup->getAll()];
66  }
67  $this->assertEquals($expectedGroups, $actualGroups);
68  }
69 
70  public function testAdd()
71  {
72  $assetNew = new \Magento\Framework\View\Asset\Remote('http://127.0.0.1/magento/test_new.css');
73  $this->_object->add('asset_new', $assetNew, ['test_property' => 'test_value']);
74  $this->assertEquals(['asset' => $this->_asset, 'asset_new' => $assetNew], $this->_object->getAll());
75  }
76 
77  public function testRemove()
78  {
79  $this->_object->remove('asset');
80  $this->assertEquals([], $this->_object->getAll());
81  }
82 
83  public function testGetGroups()
84  {
85  $cssAsset = new \Magento\Framework\View\Asset\Remote('http://127.0.0.1/style.css', 'css');
86  $jsAsset = new \Magento\Framework\View\Asset\Remote('http://127.0.0.1/script.js', 'js');
87  $jsAssetAllowingMerge = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\MergeableInterface::class);
88  $jsAssetAllowingMerge->expects($this->any())->method('getContentType')->will($this->returnValue('js'));
89 
90  // assets with identical properties should be grouped together
91  $this->_object->add('css_asset_one', $cssAsset, ['property' => 'test_value']);
92  $this->_object->add('css_asset_two', $cssAsset, ['property' => 'test_value']);
93 
94  // assets with identical properties but empty properties should be grouped together
95  $this->_object->add('css_asset_four', $cssAsset, ['property' => 'test_value2', 'junk1' => null]);
96  $this->_object->add('css_asset_five', $cssAsset, ['property' => 'test_value2', 'junk2' => '']);
97 
98  // assets with different properties should go to different groups
99  $this->_object->add('css_asset_three', $cssAsset, ['property' => 'different_value']);
100  $this->_object->add('js_asset_one', $jsAsset, ['property' => 'test_value']);
101 
102  // assets with identical properties in a different order should be grouped
103  $this->_object->add('js_asset_two', $jsAsset, ['property1' => 'value1', 'property2' => 'value2']);
104  $this->_object->add('js_asset_three', $jsAsset, ['property2' => 'value2', 'property1' => 'value1']);
105 
106  // assets allowing merge should go to separate group regardless of having identical properties
107  $this->_object->add('asset_allowing_merge', $jsAssetAllowingMerge, ['property' => 'test_value']);
108 
109  $expectedGroups = [
110  [
111  'properties' => ['content_type' => 'unknown', 'can_merge' => false],
112  'assets' => ['asset' => $this->_asset],
113  ],
114  [
115  'properties' => ['property' => 'test_value', 'content_type' => 'css', 'can_merge' => false],
116  'assets' => ['css_asset_one' => $cssAsset, 'css_asset_two' => $cssAsset]
117  ],
118  [
119  'properties' => ['property' => 'test_value2', 'content_type' => 'css', 'can_merge' => false],
120  'assets' => ['css_asset_four' => $cssAsset, 'css_asset_five' => $cssAsset]
121  ],
122  [
123  'properties' => ['property' => 'different_value', 'content_type' => 'css', 'can_merge' => false],
124  'assets' => ['css_asset_three' => $cssAsset]
125  ],
126  [
127  'properties' => ['property' => 'test_value', 'content_type' => 'js', 'can_merge' => false],
128  'assets' => ['js_asset_one' => $jsAsset]
129  ],
130  [
131  'properties' => [
132  'property1' => 'value1',
133  'property2' => 'value2',
134  'content_type' => 'js',
135  'can_merge' => false,
136  ],
137  'assets' => ['js_asset_two' => $jsAsset, 'js_asset_three' => $jsAsset]
138  ],
139  [
140  'properties' => ['property' => 'test_value', 'content_type' => 'js', 'can_merge' => true],
141  'assets' => ['asset_allowing_merge' => $jsAssetAllowingMerge]
142  ],
143  ];
144 
145  $this->_assertGroups($expectedGroups, $this->_object->getGroups());
146  }
147 }
$arguments