AdapterFactoryTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Image\Test\Unit;
  7. use \Magento\Framework\Image\AdapterFactory;
  8. class AdapterFactoryTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\Image\Adapter\ConfigInterface | \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $configMock;
  14. protected function setUp()
  15. {
  16. $this->configMock = $this->createPartialMock(
  17. \Magento\Framework\Image\Adapter\ConfigInterface::class,
  18. ['getAdapterAlias', 'getAdapters']
  19. );
  20. $this->configMock->expects(
  21. $this->once()
  22. )->method(
  23. 'getAdapters'
  24. )->will(
  25. $this->returnValue(
  26. [
  27. 'GD2' => ['class' => \Magento\Framework\Image\Adapter\Gd2::class],
  28. 'IMAGEMAGICK' => ['class' => \Magento\Framework\Image\Adapter\ImageMagick::class],
  29. 'wrongInstance' => ['class' => 'stdClass'],
  30. 'test' => [],
  31. ]
  32. )
  33. );
  34. }
  35. /**
  36. * @dataProvider createDataProvider
  37. * @param string $alias
  38. * @param string $class
  39. */
  40. public function testCreate($alias, $class)
  41. {
  42. $objectManagerMock =
  43. $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, ['create']);
  44. $imageAdapterMock = $this->createPartialMock($class, ['checkDependencies']);
  45. $imageAdapterMock->expects($this->once())->method('checkDependencies');
  46. $objectManagerMock->expects(
  47. $this->once()
  48. )->method(
  49. 'create'
  50. )->with(
  51. $class
  52. )->will(
  53. $this->returnValue($imageAdapterMock)
  54. );
  55. $adapterFactory = new AdapterFactory($objectManagerMock, $this->configMock);
  56. $imageAdapter = $adapterFactory->create($alias);
  57. $this->assertInstanceOf($class, $imageAdapter);
  58. }
  59. /**
  60. * @see self::testCreate()
  61. * @return array
  62. */
  63. public function createDataProvider()
  64. {
  65. return [
  66. ['GD2', \Magento\Framework\Image\Adapter\Gd2::class],
  67. ['IMAGEMAGICK', \Magento\Framework\Image\Adapter\ImageMagick::class]
  68. ];
  69. }
  70. /**
  71. * @covers \Magento\Framework\Image\AdapterFactory::create
  72. */
  73. public function testCreateWithoutName()
  74. {
  75. $adapterAlias = 'IMAGEMAGICK';
  76. $adapterClass = \Magento\Framework\Image\Adapter\ImageMagick::class;
  77. $this->configMock->expects($this->once())->method('getAdapterAlias')->will($this->returnValue($adapterAlias));
  78. $objectManagerMock =
  79. $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, ['create']);
  80. $imageAdapterMock = $this->createPartialMock($adapterClass, ['checkDependencies']);
  81. $imageAdapterMock->expects($this->once())->method('checkDependencies');
  82. $objectManagerMock->expects(
  83. $this->once()
  84. )->method(
  85. 'create'
  86. )->with(
  87. $adapterClass
  88. )->will(
  89. $this->returnValue($imageAdapterMock)
  90. );
  91. $adapterFactory = new AdapterFactory($objectManagerMock, $this->configMock);
  92. $imageAdapter = $adapterFactory->create();
  93. $this->assertInstanceOf($adapterClass, $imageAdapter);
  94. }
  95. /**
  96. * @covers \Magento\Framework\Image\AdapterFactory::create
  97. * @expectedException \InvalidArgumentException
  98. * @expectedExceptionMessage Image adapter is not selected.
  99. */
  100. public function testInvalidArgumentException()
  101. {
  102. $this->configMock->expects($this->once())->method('getAdapterAlias')->will($this->returnValue(''));
  103. $objectManagerMock =
  104. $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, ['create']);
  105. $adapterFactory = new AdapterFactory($objectManagerMock, $this->configMock);
  106. $adapterFactory->create();
  107. }
  108. /**
  109. * @covers \Magento\Framework\Image\AdapterFactory::create
  110. * @expectedException \InvalidArgumentException
  111. * @expectedExceptionMessage Image adapter for 'test' is not setup.
  112. */
  113. public function testNonAdapterClass()
  114. {
  115. $alias = 'test';
  116. $objectManagerMock =
  117. $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, ['create']);
  118. $adapterFactory = new AdapterFactory($objectManagerMock, $this->configMock);
  119. $adapterFactory->create($alias);
  120. }
  121. /**
  122. * @covers \Magento\Framework\Image\AdapterFactory::create
  123. * @expectedException \InvalidArgumentException
  124. * @expectedExceptionMessage stdClass is not instance of \Magento\Framework\Image\Adapter\AdapterInterface
  125. */
  126. public function testWrongInstance()
  127. {
  128. $alias = 'wrongInstance';
  129. $class = 'stdClass';
  130. $objectManagerMock =
  131. $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, ['create']);
  132. $imageAdapterMock = $this->createPartialMock($class, ['checkDependencies']);
  133. $objectManagerMock->expects(
  134. $this->once()
  135. )->method(
  136. 'create'
  137. )->with(
  138. $class
  139. )->will(
  140. $this->returnValue($imageAdapterMock)
  141. );
  142. $adapterFactory = new AdapterFactory($objectManagerMock, $this->configMock);
  143. $adapterFactory->create($alias);
  144. }
  145. }