From fcd3bbfee031dddf739dbf65d21bb70886f324be Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 13 May 2016 01:46:48 +0200 Subject: [PATCH] MDL-54205 tests: verify destroy() and close() behavior --- backup/util/loggers/tests/logger_test.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/backup/util/loggers/tests/logger_test.php b/backup/util/loggers/tests/logger_test.php index 67f8e7472c6..70a65f9b8b3 100644 --- a/backup/util/loggers/tests/logger_test.php +++ b/backup/util/loggers/tests/logger_test.php @@ -108,6 +108,16 @@ class backup_logger_testcase extends basic_testcase { $this->assertEquals($lo1->get_levelstr(backup::LOG_WARNING), 'warn'); $this->assertEquals($lo1->get_levelstr(backup::LOG_INFO), 'info'); $this->assertEquals($lo1->get_levelstr(backup::LOG_DEBUG), 'debug'); + + // Test destroy. + $lo1 = new mock_base_logger1(backup::LOG_ERROR); + $lo2 = new mock_base_logger2(backup::LOG_ERROR); + $lo1->set_next($lo2); + $this->assertInstanceOf('base_logger', $lo1->get_next()); + $this->assertNull($lo2->get_next()); + $lo1->destroy(); + $this->assertNull($lo1->get_next()); + $this->assertNull($lo2->get_next()); } /** @@ -249,9 +259,9 @@ class backup_logger_testcase extends basic_testcase { $result = $lo2->process($message2, backup::LOG_WARNING, $options); $this->assertTrue($result); - // Destruct loggers - $lo1 = null; - $lo2 = null; + // Destroy loggers. + $lo1->destroy(); + $lo2->destroy(); // Load file results to analyze them $fcontents = file_get_contents($file); @@ -275,6 +285,7 @@ class backup_logger_testcase extends basic_testcase { $this->assertTrue(file_exists($file)); $message = 'testing file_logger'; $result = $lo->process($message, backup::LOG_ERROR, $options); + $lo->close(); // Closes logger. // Get file contents and inspect them $fcontents = file_get_contents($file); $this->assertTrue($result); @@ -282,7 +293,6 @@ class backup_logger_testcase extends basic_testcase { $this->assertTrue(strpos($fcontents, '[error]') !== false); $this->assertTrue(strpos($fcontents, '  ') !== false); $this->assertTrue(substr_count($fcontents , '] ') >= 2); - $lo->__destruct(); // closes file handle unlink($file); // delete file // Instantiate, write something, force deletion, try to write again @@ -292,7 +302,8 @@ class backup_logger_testcase extends basic_testcase { $this->assertTrue(file_exists($file)); $message = 'testing file_logger'; $result = $lo->process($message, backup::LOG_ERROR); - fclose($lo->get_fhandle()); // close file + $lo->close(); + $this->assertNull($lo->get_fhandle()); try { $result = @$lo->process($message, backup::LOG_ERROR); // Try to write again $this->assertTrue(false, 'base_logger_exception expected'); @@ -326,6 +337,7 @@ class backup_logger_testcase extends basic_testcase { $lo = new file_logger(backup::LOG_NONE, true, true, $file); $this->assertTrue($lo instanceof file_logger); $this->assertFalse(file_exists($file)); + $lo->close(); // Remove the test dir and any content @remove_dir(dirname($file));