MediaGalleryTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Catalog;
  8. use Magento\TestFramework\TestCase\GraphQlAbstract;
  9. class MediaGalleryTest extends GraphQlAbstract
  10. {
  11. /**
  12. * @var \Magento\TestFramework\ObjectManager
  13. */
  14. private $objectManager;
  15. protected function setUp()
  16. {
  17. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  18. }
  19. /**
  20. * @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php
  21. */
  22. public function testProductSmallImageUrlWithExistingImage()
  23. {
  24. $productSku = 'simple';
  25. $query = <<<QUERY
  26. {
  27. products(filter: {sku: {eq: "{$productSku}"}}) {
  28. items {
  29. small_image {
  30. url
  31. }
  32. }
  33. }
  34. }
  35. QUERY;
  36. $response = $this->graphQlQuery($query);
  37. self::assertArrayHasKey('url', $response['products']['items'][0]['small_image']);
  38. self::assertContains('magento_image.jpg', $response['products']['items'][0]['small_image']['url']);
  39. self::assertTrue($this->checkImageExists($response['products']['items'][0]['small_image']['url']));
  40. }
  41. /**
  42. * @param string $url
  43. * @return bool
  44. */
  45. private function checkImageExists(string $url): bool
  46. {
  47. $connection = curl_init($url);
  48. curl_setopt($connection, CURLOPT_HEADER, true);
  49. curl_setopt($connection, CURLOPT_NOBODY, true);
  50. curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
  51. curl_exec($connection);
  52. $responseStatus = curl_getinfo($connection, CURLINFO_HTTP_CODE);
  53. return $responseStatus === 200 ? true : false;
  54. }
  55. }