FileUtilsTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. require_once 'application/FileUtils.php';
  3. /**
  4. * Class FileUtilsTest
  5. *
  6. * Test file utility class.
  7. */
  8. class FileUtilsTest extends PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var string Test file path.
  12. */
  13. protected static $file = 'sandbox/flat.db';
  14. /**
  15. * Delete test file after every test.
  16. */
  17. public function tearDown()
  18. {
  19. @unlink(self::$file);
  20. }
  21. /**
  22. * Test writeDB, then readDB with different data.
  23. */
  24. public function testSimpleWriteRead()
  25. {
  26. $data = ['blue', 'red'];
  27. $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
  28. $this->assertTrue(startsWith(file_get_contents(self::$file), '<?php /*'));
  29. $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
  30. $data = 0;
  31. $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
  32. $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
  33. $data = null;
  34. $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
  35. $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
  36. $data = false;
  37. $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
  38. $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
  39. }
  40. /**
  41. * File not writable: raise an exception.
  42. *
  43. * @expectedException IOException
  44. * @expectedExceptionMessage Error accessing "sandbox/flat.db"
  45. */
  46. public function testWriteWithoutPermission()
  47. {
  48. touch(self::$file);
  49. chmod(self::$file, 0440);
  50. FileUtils::writeFlatDB(self::$file, null);
  51. }
  52. /**
  53. * Folder non existent: raise an exception.
  54. *
  55. * @expectedException IOException
  56. * @expectedExceptionMessage Error accessing "nopefolder"
  57. */
  58. public function testWriteFolderDoesNotExist()
  59. {
  60. FileUtils::writeFlatDB('nopefolder/file', null);
  61. }
  62. /**
  63. * Folder non writable: raise an exception.
  64. *
  65. * @expectedException IOException
  66. * @expectedExceptionMessage Error accessing "sandbox"
  67. */
  68. public function testWriteFolderPermission()
  69. {
  70. chmod(dirname(self::$file), 0555);
  71. try {
  72. FileUtils::writeFlatDB(self::$file, null);
  73. } catch (Exception $e) {
  74. chmod(dirname(self::$file), 0755);
  75. throw $e;
  76. }
  77. }
  78. /**
  79. * Read non existent file, use default parameter.
  80. */
  81. public function testReadNotExistentFile()
  82. {
  83. $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
  84. $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
  85. }
  86. /**
  87. * Read non readable file, use default parameter.
  88. */
  89. public function testReadNotReadable()
  90. {
  91. touch(self::$file);
  92. chmod(self::$file, 0220);
  93. $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
  94. $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
  95. }
  96. }