IndexerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Test\Unit\App;
  7. class IndexerTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Indexer\App\Indexer
  11. */
  12. protected $entryPoint;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Indexer\Model\Processor
  15. */
  16. protected $processor;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem
  19. */
  20. protected $filesystem;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Console\Response
  23. */
  24. protected $_response;
  25. protected function setUp()
  26. {
  27. $this->filesystem = $this->createPartialMock(\Magento\Framework\Filesystem::class, ['getDirectoryWrite']);
  28. $this->processor = $this->createMock(\Magento\Indexer\Model\Processor::class);
  29. $this->_response = $this->createPartialMock(
  30. \Magento\Framework\App\Console\Response::class,
  31. ['setCode', 'getCode']
  32. );
  33. $this->entryPoint = new \Magento\Indexer\App\Indexer(
  34. 'reportDir',
  35. $this->filesystem,
  36. $this->processor,
  37. $this->_response
  38. );
  39. }
  40. /**
  41. * @param bool $isExist
  42. * @param array $callCount
  43. * @dataProvider executeProvider
  44. */
  45. public function testExecute($isExist, $callCount)
  46. {
  47. $this->_response->expects($this->once())->method('setCode')->with(0);
  48. $this->_response->expects($this->once())->method('getCode')->will($this->returnValue(0));
  49. $dir = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
  50. $dir->expects($this->any())->method('getRelativePath')->will($this->returnArgument(0));
  51. $dir->expects($this->once())->method('isExist')->will($this->returnValue($isExist));
  52. $dir->expects($this->exactly($callCount))->method('delete')->will($this->returnValue(true));
  53. $this->filesystem->expects($this->once())->method('getDirectoryWrite')->will($this->returnValue($dir));
  54. $this->processor->expects($this->once())->method('reindexAll');
  55. $this->assertEquals(0, $this->entryPoint->launch()->getCode());
  56. }
  57. /**
  58. * @return array
  59. */
  60. public function executeProvider()
  61. {
  62. return [
  63. 'set1' => ['isExist' => true, 'expectsValue' => 1],
  64. 'set1' => ['delete' => false, 'expectsValue' => 0]
  65. ];
  66. }
  67. public function testCatchException()
  68. {
  69. $bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
  70. $this->assertFalse($this->entryPoint->catchException($bootstrap, new \Exception()));
  71. }
  72. }