SwatchAttributeCodesTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Test\Unit\Model;
  7. use Magento\Eav\Model\Entity\Attribute;
  8. use Magento\Framework\App\CacheInterface;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\DB\Adapter\Pdo\Mysql;
  11. use Magento\Framework\DB\Select;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  13. use Magento\Swatches\Model\SwatchAttributeCodes;
  14. class SwatchAttributeCodesTest extends \PHPUnit\Framework\TestCase
  15. {
  16. const ATTRIBUTE_TABLE = 'eav_attribute';
  17. const ATTRIBUTE_OPTION_TABLE = 'eav_attribute_option';
  18. const SWATCH_OPTION_TABLE = 'eav_attribute_option_swatch';
  19. const CACHE_KEY = 'swatch-attribute-list';
  20. /**
  21. * @var SwatchAttributeCodes
  22. */
  23. private $swatchAttributeCodesModel;
  24. /**
  25. * @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $cache;
  28. /**
  29. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $resourceConnection;
  32. /**
  33. * @var array
  34. */
  35. private $swatchAttributesCodes = [
  36. 10 => 'text_swatch',
  37. 11 => 'image_swatch',
  38. ];
  39. protected function setUp()
  40. {
  41. $this->cache = $this->createPartialMock(CacheInterface::class, [
  42. 'getFrontend',
  43. 'load',
  44. 'save',
  45. 'remove',
  46. 'clean'
  47. ]);
  48. $this->resourceConnection = $this->createPartialMock(
  49. ResourceConnection::class,
  50. ['getConnection', 'getTableName']
  51. );
  52. $this->swatchAttributeCodesModel = (new ObjectManager($this))->getObject(SwatchAttributeCodes::class, [
  53. 'cache' => $this->cache,
  54. 'resourceConnection' => $this->resourceConnection,
  55. 'cacheKey' => self::CACHE_KEY,
  56. 'cacheTags' => [Attribute::CACHE_TAG],
  57. ]);
  58. }
  59. /**
  60. * @dataProvider dataForGettingCodes
  61. * @param array|bool $swatchAttributeCodesCache
  62. * @param array $expected
  63. */
  64. public function testGetCodes($swatchAttributeCodesCache, $expected)
  65. {
  66. $this->cache
  67. ->method('load')
  68. ->with(self::CACHE_KEY)
  69. ->willReturn($swatchAttributeCodesCache);
  70. $adapterMock = $this->createPartialMock(Mysql::class, ['select', 'fetchPairs']);
  71. $selectMock = $this->createPartialMock(Select::class, ['from', 'where', 'join']);
  72. $selectMock
  73. ->method('from')
  74. ->withConsecutive(
  75. [
  76. self::identicalTo(
  77. ['a' => self::ATTRIBUTE_TABLE],
  78. [
  79. 'attribute_id' => 'a.attribute_id',
  80. 'attribute_code' => 'a.attribute_code',
  81. ]
  82. )
  83. ],
  84. [
  85. self::identicalTo(
  86. ['o' => self::ATTRIBUTE_OPTION_TABLE],
  87. ['attribute_id' => 'o.attribute_id']
  88. )
  89. ]
  90. )
  91. ->willReturnSelf();
  92. // used anything for second argument because of new \Zend_Db_Expt used in code.
  93. $selectMock->method('where')
  94. ->with(
  95. self::identicalTo('a.attribute_id IN (?)'),
  96. self::anything()
  97. )
  98. ->willReturnSelf();
  99. $adapterMock->method('select')
  100. ->willReturn($selectMock);
  101. $adapterMock->method('fetchPairs')
  102. ->with($selectMock)
  103. ->willReturn($this->swatchAttributesCodes);
  104. $this->resourceConnection
  105. ->method('getConnection')
  106. ->willReturn($adapterMock);
  107. $this->resourceConnection
  108. ->method('getTableName')
  109. ->withConsecutive(
  110. [self::ATTRIBUTE_TABLE],
  111. [self::ATTRIBUTE_OPTION_TABLE],
  112. [self::SWATCH_OPTION_TABLE]
  113. )->will(self::onConsecutiveCalls(
  114. self::ATTRIBUTE_TABLE,
  115. self::ATTRIBUTE_OPTION_TABLE,
  116. self::SWATCH_OPTION_TABLE
  117. ));
  118. $result = $this->swatchAttributeCodesModel->getCodes();
  119. $this->assertEquals($expected, $result);
  120. }
  121. /**
  122. * @return array
  123. */
  124. public function dataForGettingCodes()
  125. {
  126. return [
  127. [false, $this->swatchAttributesCodes],
  128. [json_encode($this->swatchAttributesCodes), $this->swatchAttributesCodes]
  129. ];
  130. }
  131. }