ReaderTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update\Queue;
  7. class ReaderTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Update\Queue\Reader
  11. */
  12. protected $readerReader;
  13. /**
  14. * @var string
  15. */
  16. protected $validQueueFilePath;
  17. /**
  18. * @var string
  19. */
  20. protected $invalidQueueFilePath;
  21. /**
  22. * @var string
  23. */
  24. protected $tmpQueueFilePath;
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $this->validQueueFilePath = __DIR__ . '/../_files/update_queue_valid.json';
  29. $this->invalidQueueFilePath = __DIR__ . '/../_files/update_queue_invalid.json';
  30. $this->tmpQueueFilePath = TESTS_TEMP_DIR . '/update_queue_valid.json';
  31. /** Prepare temporary queue file which can be modified */
  32. $queueFileContent = file_get_contents($this->validQueueFilePath);
  33. file_put_contents($this->tmpQueueFilePath, $queueFileContent);
  34. /** Make sure it was created */
  35. $this->assertEquals($queueFileContent, file_get_contents($this->tmpQueueFilePath), "Precondition failed.");
  36. }
  37. protected function tearDown()
  38. {
  39. parent::tearDown();
  40. if (file_exists($this->tmpQueueFilePath)) {
  41. unlink($this->tmpQueueFilePath);
  42. }
  43. }
  44. public function testRead()
  45. {
  46. $reader = new \Magento\Update\Queue\Reader($this->validQueueFilePath);
  47. $actualQueueFileContent = $reader->read();
  48. $expectedQueueFileContent = file_get_contents($this->validQueueFilePath);
  49. $this->assertEquals($expectedQueueFileContent, $actualQueueFileContent);
  50. }
  51. public function testReadFileDoesNotExist()
  52. {
  53. $invalidFilePath = 'invalidpath';
  54. $reader = new \Magento\Update\Queue\Reader($invalidFilePath);
  55. $actualQueueFileContent = $reader->read();
  56. $expectedQueueFileContent = '';
  57. $this->assertEquals($expectedQueueFileContent, $actualQueueFileContent);
  58. }
  59. public function testReadInvalidFileFormat()
  60. {
  61. $reader = new \Magento\Update\Queue\Reader($this->invalidQueueFilePath);
  62. $this->expectException('\RuntimeException');
  63. $this->expectExceptionMessage("Content of \"{$this->invalidQueueFilePath}\" must be a valid JSON.");
  64. $reader->read();
  65. }
  66. }