LogTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Tools\Test\Utility;
  3. use Tools\TestSuite\TestCase;
  4. use Tools\Utility\Log;
  5. /**
  6. * LogTest class
  7. */
  8. class LogTest 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. private const TEST_FILENAME_ARRAY2 = 'array_file2';
  31. private const TEST_FILEPATH_ARRAY2 = LOGS . self::TEST_FILENAME_ARRAY2 . '.log';
  32. /**
  33. * setUp method
  34. *
  35. * @return void
  36. */
  37. public function setUp() {
  38. parent::setUp();
  39. }
  40. /**
  41. * testLogsStringData method
  42. *
  43. * @return void
  44. */
  45. public function testLogsStringData() {
  46. if (file_exists(static::TEST_FILEPATH_STRING)) {
  47. unlink(static::TEST_FILEPATH_STRING);
  48. }
  49. $result = Log::write('It works!', static::TEST_FILENAME_STRING);
  50. $this->assertTrue($result);
  51. $this->assertFileExists(static::TEST_FILEPATH_STRING);
  52. $this->assertRegExp(
  53. '/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: It works!/',
  54. file_get_contents(static::TEST_FILEPATH_STRING)
  55. );
  56. unlink(static::TEST_FILENAME_STRING);
  57. }
  58. /**
  59. * testLogsArray method
  60. *
  61. * @return void
  62. */
  63. public function testLogsArray() {
  64. if (file_exists(static::TEST_FILEPATH_ARRAY1)) {
  65. unlink(static::TEST_FILEPATH_ARRAY1);
  66. }
  67. if (file_exists(static::TEST_FILEPATH_ARRAY2)) {
  68. unlink(static::TEST_FILEPATH_ARRAY2);
  69. }
  70. $result1 = Log::write(
  71. [
  72. 'user' => [
  73. 'id' => 1,
  74. 'firstname' => 'John Doe',
  75. 'email' => 'john.doe@example.com',
  76. ],
  77. ],
  78. static::TEST_FILENAME_ARRAY1
  79. );
  80. $result2 = Log::write(
  81. [
  82. 'user' => [
  83. 'id' => 2,
  84. 'firstname' => 'Jane Doe',
  85. 'email' => 'jane.doe@example.com',
  86. ],
  87. ],
  88. static::TEST_FILENAME_ARRAY2
  89. );
  90. // Assert for `TEST_FILENAME_ARRAY1`
  91. $this->assertTrue($result1);
  92. $this->assertFileExists(static::TEST_FILEPATH_ARRAY1);
  93. $fileContents = file_get_contents(static::TEST_FILEPATH_ARRAY1);
  94. $this->assertRegExp(
  95. '/^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/',
  96. $fileContents
  97. );
  98. // Assert for `TEST_FILENAME_ARRAY2`
  99. $this->assertTrue($result2);
  100. $this->assertFileExists(static::TEST_FILEPATH_ARRAY2);
  101. $fileContents = file_get_contents(static::TEST_FILEPATH_ARRAY2);
  102. $this->assertRegExp(
  103. '/^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/',
  104. $fileContents
  105. );
  106. unlink(static::TEST_FILEPATH_ARRAY1);
  107. unlink(static::TEST_FILEPATH_ARRAY2);
  108. }
  109. /**
  110. * testLogsIntoDefaultFile method
  111. *
  112. * @return void
  113. */
  114. public function testLogsIntoDefaultFile() {
  115. if (file_exists(static::TEST_DEFAULT_FILEPATH_STRING)) {
  116. unlink(static::TEST_DEFAULT_FILEPATH_STRING);
  117. }
  118. $result = Log::write('It works with default too!');
  119. $this->assertTrue($result);
  120. $this->assertFileExists(static::TEST_DEFAULT_FILEPATH_STRING);
  121. $this->assertRegExp(
  122. '/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: It works with default too!/',
  123. file_get_contents(static::TEST_DEFAULT_FILEPATH_STRING)
  124. );
  125. unlink(static::TEST_DEFAULT_FILEPATH_STRING);
  126. }
  127. }