Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductTest.php
Go to the documentation of this file.
1 <?php
7 
15 class ProductTest extends \PHPUnit\Framework\TestCase
16 {
20  const BASE_IMAGE_PATH = 'http://localhost/pub/media/catalog/product/cache/8d4d2075b1a30681853bef5bdc41b164';
21 
29  public function testGetCollectionNone()
30  {
32  \Magento\Sitemap\Model\ResourceModel\Catalog\Product::class
33  );
34  $products = $model->getCollection(\Magento\Store\Model\Store::DISTRO_STORE_ID);
35 
36  $this->_checkProductCollection($products, 3, [1, 4, 5]);
37 
38  // Check that no image attributes were loaded
39  foreach ($products as $product) {
40  $this->assertEmpty($product->getName(), 'Attribute name is not empty');
41  $this->assertEmpty($product->getImage(), 'Attribute image is not empty');
42  $this->assertEmpty($product->getThumbnail(), 'Attribute thumbnail is not empty');
43  }
44 
45  $this->assertEmpty($products[4]->getImages(), 'Images were loaded');
46  }
47 
56  public function testGetCollectionAll()
57  {
59  \Magento\Sitemap\Model\ResourceModel\Catalog\Product::class
60  );
61  $products = $model->getCollection(\Magento\Store\Model\Store::DISTRO_STORE_ID);
62 
63  $this->_checkProductCollection($products, 3, [1, 4, 5]);
64 
65  // Check name attribute was loaded
66  foreach ($products as $product) {
67  $this->assertNotEmpty($product->getName(), 'name attribute was not loaded');
68  }
69 
70  // Check thumbnail attribute
71  $this->assertEmpty($products[1]->getThumbnail(), 'Thumbnail attribute was loaded');
72  $this->assertEmpty($products[4]->getImage(), 'Image attribute was loaded');
73  $this->assertEquals('/m/a/magento_image_sitemap.png', $products[4]->getThumbnail(), 'Incorrect thumbnail');
74 
75  // Check images loading
76  $this->assertEmpty($products[1]->getImages(), 'Images were loaded');
77  $this->assertNotEmpty($products[4]->getImages(), 'Images were not loaded');
78  $this->assertEquals('Simple Images', $products[4]->getImages()->getTitle(), 'Incorrect title');
79  $this->assertEquals(
80  self::BASE_IMAGE_PATH.'/m/a/magento_image_sitemap.png',
81  $products[4]->getImages()->getThumbnail(),
82  'Incorrect thumbnail'
83  );
84  $this->assertCount(2, $products[4]->getImages()->getCollection(), 'Not all images were loaded');
85 
86  $imagesCollection = $products[4]->getImages()->getCollection();
87  $this->assertEquals(
88  self::BASE_IMAGE_PATH.'/m/a/magento_image_sitemap.png',
89  $imagesCollection[0]->getUrl(),
90  'Incorrect image url'
91  );
92  $this->assertEquals(
93  self::BASE_IMAGE_PATH.'/s/e/second_image.png',
94  $imagesCollection[1]->getUrl(),
95  'Incorrect image url'
96  );
97  $this->assertEmpty($imagesCollection[0]->getCaption(), 'Caption not empty');
98 
99  // Check no selection
100  $this->assertEmpty($products[5]->getImage(), 'image is not empty');
101  $this->assertEquals('no_selection', $products[5]->getThumbnail(), 'thumbnail is incorrect');
102  $imagesCollection = $products[5]->getImages()->getCollection();
103  $this->assertCount(1, $imagesCollection);
104  $this->assertEquals(
105  self::BASE_IMAGE_PATH.'/s/e/second_image_1.png',
106  $imagesCollection[0]->getUrl(),
107  'Image url is incorrect'
108  );
109  $this->assertEquals(
110  self::BASE_IMAGE_PATH.'/s/e/second_image_1.png',
111  $products[5]->getImages()->getThumbnail(),
112  'Product thumbnail is incorrect'
113  );
114  }
115 
124  public function testGetCollectionBase()
125  {
127  \Magento\Sitemap\Model\ResourceModel\Catalog\Product::class
128  );
129  $products = $model->getCollection(\Magento\Store\Model\Store::DISTRO_STORE_ID);
130 
131  $this->_checkProductCollection($products, 3, [1, 4, 5]);
132 
133  // Check name attribute was loaded
134  foreach ($products as $product) {
135  $this->assertNotEmpty($product->getName(), 'name attribute was not loaded');
136  }
137 
138  // Check thumbnail attribute
139  $this->assertEmpty($products[1]->getImage(), 'image attribute was loaded');
140  $this->assertEmpty($products[4]->getThumbnail(), 'thumbnail attribute was loaded');
141  $this->assertEquals('/s/e/second_image.png', $products[4]->getImage(), 'Incorrect image attribute');
142 
143  // Check images loading
144  $this->assertEmpty($products[1]->getImages(), 'Images were loaded');
145  $this->assertNotEmpty($products[4]->getImages(), 'Images were not loaded');
146  $this->assertEquals('Simple Images', $products[4]->getImages()->getTitle(), 'Incorrect title');
147  $this->assertEquals(
148  self::BASE_IMAGE_PATH.'/s/e/second_image.png',
149  $products[4]->getImages()->getThumbnail(),
150  'Incorrect thumbnail'
151  );
152  $this->assertCount(1, $products[4]->getImages()->getCollection(), 'Number of loaded images is incorrect');
153 
154  $imagesCollection = $products[4]->getImages()->getCollection();
155  $this->assertEquals(
156  self::BASE_IMAGE_PATH.'/s/e/second_image.png',
157  $imagesCollection[0]->getUrl(),
158  'Incorrect image url'
159  );
160  $this->assertEmpty($imagesCollection[0]->getCaption(), 'Caption not empty');
161 
162  // Check no selection
163  $this->assertEmpty($products[5]->getThumbnail(), 'thumbnail is not empty');
164  $this->assertEquals('no_selection', $products[5]->getImage(), 'image is incorrect');
165  $this->assertEmpty($products[5]->getImages(), 'Product images were loaded');
166  }
167 
177  protected function _checkProductCollection(array $products, $expectedCount, array $expectedKeys)
178  {
179  // Check all expected products were added into collection
180  $this->assertCount($expectedCount, $products, 'Number of loaded products is incorrect');
181  foreach ($expectedKeys as $expectedKey) {
182  $this->assertArrayHasKey($expectedKey, $products);
183  }
184 
185  // Check all expected attributes are present
186  foreach ($products as $product) {
187  $this->assertNotEmpty($product->getUpdatedAt());
188  $this->assertNotEmpty($product->getId());
189  $this->assertNotEmpty($product->getUrl());
190  }
191  }
192 }
_checkProductCollection(array $products, $expectedCount, array $expectedKeys)