DataTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Test\Unit\Model\Config;
  7. class DataTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Indexer\Model\Config\Data
  11. */
  12. protected $model;
  13. /**
  14. * @var \Magento\Framework\Indexer\Config\Reader|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $reader;
  17. /**
  18. * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $cache;
  21. /**
  22. * @var \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $stateCollection;
  25. /**
  26. * @var string
  27. */
  28. protected $cacheId = 'indexer_config';
  29. /**
  30. * @var string
  31. */
  32. protected $indexers = ['indexer1' => [], 'indexer3' => []];
  33. /**
  34. * @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $serializerMock;
  37. protected function setUp()
  38. {
  39. $this->reader = $this->createPartialMock(\Magento\Framework\Indexer\Config\Reader::class, ['read']);
  40. $this->cache = $this->getMockForAbstractClass(
  41. \Magento\Framework\Config\CacheInterface::class,
  42. [],
  43. '',
  44. false,
  45. false,
  46. true,
  47. ['test', 'load', 'save']
  48. );
  49. $this->stateCollection = $this->createPartialMock(
  50. \Magento\Indexer\Model\ResourceModel\Indexer\State\Collection::class,
  51. ['getItems']
  52. );
  53. $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
  54. }
  55. public function testConstructorWithCache()
  56. {
  57. $serializedData = 'serialized data';
  58. $this->cache->expects($this->once())->method('test')->with($this->cacheId)->will($this->returnValue(true));
  59. $this->cache->expects($this->once())
  60. ->method('load')
  61. ->with($this->cacheId)
  62. ->willReturn($serializedData);
  63. $this->serializerMock->expects($this->once())
  64. ->method('unserialize')
  65. ->with($serializedData)
  66. ->willReturn($this->indexers);
  67. $this->stateCollection->expects($this->never())->method('getItems');
  68. $this->model = new \Magento\Indexer\Model\Config\Data(
  69. $this->reader,
  70. $this->cache,
  71. $this->stateCollection,
  72. $this->cacheId,
  73. $this->serializerMock
  74. );
  75. }
  76. public function testConstructorWithoutCache()
  77. {
  78. $this->cache->expects($this->once())->method('test')->with($this->cacheId)->will($this->returnValue(false));
  79. $this->cache->expects($this->once())->method('load')->with($this->cacheId)->will($this->returnValue(false));
  80. $this->reader->expects($this->once())->method('read')->will($this->returnValue($this->indexers));
  81. $stateExistent = $this->createPartialMock(
  82. \Magento\Indexer\Model\Indexer\State::class,
  83. ['getIndexerId', '__wakeup', 'delete']
  84. );
  85. $stateExistent->expects($this->once())->method('getIndexerId')->will($this->returnValue('indexer1'));
  86. $stateExistent->expects($this->never())->method('delete');
  87. $stateNonexistent = $this->createPartialMock(
  88. \Magento\Indexer\Model\Indexer\State::class,
  89. ['getIndexerId', '__wakeup', 'delete']
  90. );
  91. $stateNonexistent->expects($this->once())->method('getIndexerId')->will($this->returnValue('indexer2'));
  92. $stateNonexistent->expects($this->once())->method('delete');
  93. $states = [$stateExistent, $stateNonexistent];
  94. $this->stateCollection->expects($this->once())->method('getItems')->will($this->returnValue($states));
  95. $this->model = new \Magento\Indexer\Model\Config\Data(
  96. $this->reader,
  97. $this->cache,
  98. $this->stateCollection,
  99. $this->cacheId,
  100. $this->serializerMock
  101. );
  102. }
  103. }