StateTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Test\Unit\SampleData;
  7. /**
  8. * Class StateTest
  9. */
  10. class StateTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\Setup\SampleData\State|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $state;
  16. /**
  17. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $filesystem;
  20. /**
  21. * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $writeInterface;
  24. /**
  25. * @var string
  26. */
  27. protected $absolutePath;
  28. protected function setUp()
  29. {
  30. $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  31. ->setMethods(['getDirectoryWrite'])
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->writeInterface = $this->getMockForAbstractClass(
  35. \Magento\Framework\Filesystem\Directory\WriteInterface::class,
  36. [],
  37. '',
  38. false,
  39. true,
  40. true,
  41. ['write', 'close']
  42. );
  43. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  44. $this->state = $objectManager->getObject(
  45. \Magento\Framework\Setup\SampleData\State::class,
  46. ['filesystem' => $this->filesystem]
  47. );
  48. }
  49. public function testClearState()
  50. {
  51. $this->filesystem->expects($this->any())->method('getDirectoryWrite')->willReturn($this->writeInterface);
  52. $this->writeInterface->expects($this->any())->method('openFile')->willReturnSelf();
  53. $this->state->clearState();
  54. }
  55. /**
  56. * @covers \Magento\Framework\Setup\SampleData\State::setError
  57. */
  58. public function testHasError()
  59. {
  60. $this->filesystem->expects($this->any())->method('getDirectoryWrite')->willReturn($this->writeInterface);
  61. $this->writeInterface->expects($this->any())->method('openFile')->willReturnSelf();
  62. $this->writeInterface->expects($this->any())->method('write')->willReturnSelf();
  63. $this->writeInterface->expects($this->any())->method('close');
  64. $this->writeInterface->expects($this->any())->method('isExist')->willReturn(true);
  65. $this->writeInterface->expects($this->any())->method('read')
  66. ->willReturn(\Magento\Framework\Setup\SampleData\State::ERROR);
  67. $this->state->setError();
  68. $this->assertTrue($this->state->hasError());
  69. }
  70. /**
  71. * Clear state file
  72. */
  73. protected function tearDown()
  74. {
  75. $this->filesystem->expects($this->any())->method('getDirectoryWrite')->willReturn($this->writeInterface);
  76. $this->writeInterface->expects($this->any())->method('openFile')->willReturnSelf($this->absolutePath);
  77. $this->state->clearState();
  78. }
  79. }