WriterTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 WriterTest 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 $tmpQueueFilePath;
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $this->validQueueFilePath = __DIR__ . '/../_files/update_queue_valid.json';
  25. $this->tmpQueueFilePath = TESTS_TEMP_DIR . '/update_queue_valid.json';
  26. /** Prepare temporary queue file which can be modified */
  27. $queueFileContent = file_get_contents($this->validQueueFilePath);
  28. file_put_contents($this->tmpQueueFilePath, $queueFileContent);
  29. /** Make sure it was created */
  30. $this->assertEquals($queueFileContent, file_get_contents($this->tmpQueueFilePath), "Precondition failed.");
  31. }
  32. protected function tearDown()
  33. {
  34. parent::tearDown();
  35. if (file_exists($this->tmpQueueFilePath)) {
  36. unlink($this->tmpQueueFilePath);
  37. }
  38. }
  39. public function testWrite()
  40. {
  41. $writer = new Writer($this->tmpQueueFilePath);
  42. $writer->write('{"jobs": []}');
  43. $expectedQueueFileContent = file_get_contents($this->tmpQueueFilePath);
  44. $this->assertEquals($expectedQueueFileContent, $writer->read());
  45. }
  46. /**
  47. * @expectedException \RuntimeException
  48. * @expectedExceptionMessage Content to write must be a valid JSON.
  49. */
  50. public function testWriteInvalidJson()
  51. {
  52. $writer = new Writer($this->tmpQueueFilePath);
  53. $writer->write('invalid json string');
  54. }
  55. public function testWriteFileDoesNotExist()
  56. {
  57. $invalidFilePath = 'invalidpath';
  58. $writer = new Writer($invalidFilePath);
  59. $this->assertFalse($writer->write('{jobs: []}'));
  60. }
  61. }