ImagesAssert.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Setup\Fixtures\FixturesAsserts;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * Class ImagesAssert
  10. *
  11. * Class performs assertions to check that generated images are valid
  12. * after running setup:performance:generate-fixtures command
  13. */
  14. class ImagesAssert
  15. {
  16. /**
  17. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  18. */
  19. private $productRepository;
  20. /**
  21. * @var \Magento\Framework\Api\SearchCriteriaBuilder
  22. */
  23. private $searchCriteriaBuilder;
  24. /**
  25. * @var \Magento\Catalog\Model\Product\Gallery\ReadHandler
  26. */
  27. private $readHandler;
  28. /**
  29. * @var \Magento\Framework\Filesystem
  30. */
  31. private $filesystem;
  32. /**
  33. * @var \Magento\Catalog\Model\Product\Media\Config
  34. */
  35. private $mediaConfig;
  36. /**
  37. * @var \Magento\Framework\Filesystem\Directory\ReadInterface
  38. */
  39. private $mediaDirectory;
  40. /**
  41. * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
  42. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  43. * @param \Magento\Catalog\Model\Product\Gallery\ReadHandler $readHandler
  44. * @param \Magento\Framework\Filesystem $filesystem
  45. * @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
  46. */
  47. public function __construct(
  48. \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
  49. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  50. \Magento\Catalog\Model\Product\Gallery\ReadHandler $readHandler,
  51. \Magento\Framework\Filesystem $filesystem,
  52. \Magento\Catalog\Model\Product\Media\Config $mediaConfig
  53. ) {
  54. $this->productRepository = $productRepository;
  55. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  56. $this->readHandler = $readHandler;
  57. $this->filesystem = $filesystem;
  58. $this->mediaConfig = $mediaConfig;
  59. }
  60. /**
  61. * Performs assertions over images
  62. *
  63. * @return bool
  64. * @throws \AssertionError
  65. */
  66. public function assert()
  67. {
  68. $searchCriteria = $this->searchCriteriaBuilder->create();
  69. $products = $this->productRepository->getList($searchCriteria)->getItems();
  70. foreach ($products as $product) {
  71. $this->assertProductMediaGallery($product);
  72. $this->assertProductMediaAttributes($product);
  73. $this->assertProductImageExistsInFS($product);
  74. }
  75. return true;
  76. }
  77. /**
  78. * Performs assertions over media_gallery product attribute
  79. *
  80. * @param \Magento\Catalog\Model\Product $product
  81. * @throws \AssertionError
  82. */
  83. private function assertProductMediaGallery(\Magento\Catalog\Model\Product $product)
  84. {
  85. $extendedProduct = $this->readHandler->execute($product);
  86. $mediaGalleryImages = $extendedProduct->getMediaGalleryEntries();
  87. if (count($mediaGalleryImages) !== 1) {
  88. throw new \AssertionError('Product supposed to contain one image');
  89. }
  90. $image = reset($mediaGalleryImages);
  91. if ($image->getFile() === null) {
  92. throw new \AssertionError('Image path should not be null');
  93. }
  94. }
  95. /**
  96. * Performs assertions over product media attributes
  97. * e.g. image|small_image|swatch_image|thumbnail
  98. *
  99. * @param \Magento\Catalog\Model\Product $product
  100. * @throws \AssertionError
  101. */
  102. private function assertProductMediaAttributes(\Magento\Catalog\Model\Product $product)
  103. {
  104. foreach ($product->getMediaAttributeValues() as $attributeCode => $attributeValue) {
  105. if (empty($attributeValue)) {
  106. throw new \AssertionError(
  107. sprintf('Attribute: %s should not be empty', $attributeCode)
  108. );
  109. }
  110. }
  111. }
  112. /**
  113. * Performs assertions over image files in FS
  114. *
  115. * @param \Magento\Catalog\Model\Product $product
  116. * @throws \AssertionError
  117. */
  118. private function assertProductImageExistsInFS(\Magento\Catalog\Model\Product $product)
  119. {
  120. $mediaDirectory = $this->getMediaDirectory();
  121. $mediaAttributes = $product->getMediaAttributeValues();
  122. if (!$mediaDirectory->isExist($this->mediaConfig->getBaseMediaPath() . $mediaAttributes['image'])) {
  123. throw new \AssertionError('Image file for product supposed to exist');
  124. }
  125. }
  126. /**
  127. * Local cache for $mediaDirectory
  128. *
  129. * @return \Magento\Framework\Filesystem\Directory\ReadInterface
  130. */
  131. private function getMediaDirectory()
  132. {
  133. if ($this->mediaDirectory === null) {
  134. $this->mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
  135. }
  136. return $this->mediaDirectory;
  137. }
  138. }