FileLogTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Tools\Test\TestCase\Utility;
  3. use Shim\TestSuite\TestCase;
  4. use Tools\Utility\FileLog;
  5. /**
  6. * FileLogTest class
  7. */
  8. class FileLogTest extends TestCase {
  9. /**
  10. * Default filename with path to use in test case.
  11. *
  12. * @var string
  13. */
  14. private const TEST_DEFAULT_FILENAME_STRING = 'custom_log';
  15. private const TEST_DEFAULT_FILEPATH_STRING = LOGS . self::TEST_DEFAULT_FILENAME_STRING . '.log';
  16. /**
  17. * Filename with path to use in string test case.
  18. *
  19. * @var string
  20. */
  21. private const TEST_FILENAME_STRING = 'my_file';
  22. private const TEST_FILEPATH_STRING = LOGS . self::TEST_FILENAME_STRING . '.log';
  23. /**
  24. * Filename with path to use in array test case.
  25. *
  26. * @var string
  27. */
  28. private const TEST_FILENAME_ARRAY1 = 'array_file1';
  29. private const TEST_FILEPATH_ARRAY1 = LOGS . self::TEST_FILENAME_ARRAY1 . '.log';
  30. /**
  31. * @var string
  32. */
  33. private const TEST_FILENAME_ARRAY2 = 'array_file2';
  34. private const TEST_FILEPATH_ARRAY2 = LOGS . self::TEST_FILENAME_ARRAY2 . '.log';
  35. /**
  36. * Filename with path to use in object test case.
  37. *
  38. * @var string
  39. */
  40. private const TEST_FILENAME_OBJECT = 'object';
  41. private const TEST_FILEPATH_OBJECT = LOGS . self::TEST_FILENAME_OBJECT . '.log';
  42. /**
  43. * testLogsStringData method
  44. *
  45. * @return void
  46. */
  47. public function testLogsStringData(): void {
  48. if (file_exists(static::TEST_FILEPATH_STRING)) {
  49. unlink(static::TEST_FILEPATH_STRING);
  50. }
  51. $result = FileLog::write('It works!', static::TEST_FILENAME_STRING);
  52. $this->assertTrue($result);
  53. $this->assertFileExists(static::TEST_FILEPATH_STRING);
  54. $this->assertMatchesRegularExpression(
  55. '/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: It works!/i',
  56. file_get_contents(static::TEST_FILEPATH_STRING),
  57. );
  58. unlink(static::TEST_FILEPATH_STRING);
  59. }
  60. /**
  61. * testLogsArray method
  62. *
  63. * @return void
  64. */
  65. public function testLogsArray(): void {
  66. if (file_exists(static::TEST_FILEPATH_ARRAY1)) {
  67. unlink(static::TEST_FILEPATH_ARRAY1);
  68. }
  69. if (file_exists(static::TEST_FILEPATH_ARRAY2)) {
  70. unlink(static::TEST_FILEPATH_ARRAY2);
  71. }
  72. $result1 = FileLog::write(
  73. [
  74. 'user' => [
  75. 'id' => 1,
  76. 'firstname' => 'John Doe',
  77. 'email' => 'john.doe@example.com',
  78. ],
  79. ],
  80. static::TEST_FILENAME_ARRAY1,
  81. );
  82. $result2 = FileLog::write(
  83. [
  84. 'user' => [
  85. 'id' => 2,
  86. 'firstname' => 'Jane Doe',
  87. 'email' => 'jane.doe@example.com',
  88. ],
  89. ],
  90. static::TEST_FILENAME_ARRAY2,
  91. );
  92. // Assert for `TEST_FILENAME_ARRAY1`
  93. $this->assertTrue($result1);
  94. $this->assertFileExists(static::TEST_FILEPATH_ARRAY1);
  95. $fileContents = file_get_contents(static::TEST_FILEPATH_ARRAY1);
  96. $this->assertMatchesRegularExpression(
  97. '/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Array([\s\S]*)\(([\s\S]*)[user]([\s\S]*)\[id\] => 1/i',
  98. $fileContents,
  99. );
  100. // Assert for `TEST_FILENAME_ARRAY2`
  101. $this->assertTrue($result2);
  102. $this->assertFileExists(static::TEST_FILEPATH_ARRAY2);
  103. $fileContents = file_get_contents(static::TEST_FILEPATH_ARRAY2);
  104. $this->assertMatchesRegularExpression(
  105. '/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Array([\s\S]*)\(([\s\S]*)[user]([\s\S]*)\[id\] => 2/i',
  106. $fileContents,
  107. );
  108. unlink(static::TEST_FILEPATH_ARRAY1);
  109. unlink(static::TEST_FILEPATH_ARRAY2);
  110. }
  111. /**
  112. * testLogsObject method
  113. *
  114. * @return void
  115. */
  116. public function testLogsObject(): void {
  117. if (file_exists(static::TEST_FILEPATH_OBJECT)) {
  118. unlink(static::TEST_FILEPATH_OBJECT);
  119. }
  120. $result = FileLog::write(
  121. $this->getTableLocator()->get('Posts'),
  122. static::TEST_FILENAME_OBJECT,
  123. );
  124. $this->assertTrue($result);
  125. $this->assertFileExists(static::TEST_FILEPATH_OBJECT);
  126. $this->assertMatchesRegularExpression(
  127. '/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: TestApp.Model.Table.PostsTable Object/i',
  128. file_get_contents(static::TEST_FILEPATH_OBJECT),
  129. );
  130. unlink(static::TEST_FILEPATH_OBJECT);
  131. }
  132. /**
  133. * testLogsIntoDefaultFile method
  134. *
  135. * @return void
  136. */
  137. public function testLogsIntoDefaultFile(): void {
  138. if (file_exists(static::TEST_DEFAULT_FILEPATH_STRING)) {
  139. unlink(static::TEST_DEFAULT_FILEPATH_STRING);
  140. }
  141. $result = FileLog::write('It works with default too!');
  142. $this->assertTrue($result);
  143. $this->assertFileExists(static::TEST_DEFAULT_FILEPATH_STRING);
  144. $this->assertMatchesRegularExpression(
  145. '/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: It works with default too!/i',
  146. file_get_contents(static::TEST_DEFAULT_FILEPATH_STRING),
  147. );
  148. unlink(static::TEST_DEFAULT_FILEPATH_STRING);
  149. }
  150. }