From 05982ea671eb4d3c82f0c33995e6ef31ce09edd9 Mon Sep 17 00:00:00 2001 From: "y2ksoft@gmail.com" Date: Mon, 6 Mar 2023 07:44:14 +1300 Subject: [PATCH] Test case upgrade --- src/Model/LogEntry.php | 4 ++-- tests/Admin/LogViewerAdminTest.php | 2 +- .../GridField/GridFieldClearAllButtonTest.php | 8 +++---- tests/Handler/DataObjectHandlerTest.php | 23 ++++++++++--------- tests/Model/LogEntryTest.php | 7 +++--- tests/Task/RemoveOldLogEntriesTaskTest.php | 10 ++++---- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/Model/LogEntry.php b/src/Model/LogEntry.php index c598589..15dc06e 100644 --- a/src/Model/LogEntry.php +++ b/src/Model/LogEntry.php @@ -90,13 +90,13 @@ public function getCMSFields() { $fields = parent::getCMSFields(); - $data = Convert::json2obj($this->getField('Entry')); + $data = json_decode($this->getField('Entry')); $fields->addFieldToTab( 'Root.Main', LiteralField::create( 'Entry', '
'
-                . Convert::raw2json($data, JSON_PRETTY_PRINT)
+                . json_encode($data, JSON_PRETTY_PRINT)
                 . '
' ) ); diff --git a/tests/Admin/LogViewerAdminTest.php b/tests/Admin/LogViewerAdminTest.php index 0c6d371..12e1dc2 100644 --- a/tests/Admin/LogViewerAdminTest.php +++ b/tests/Admin/LogViewerAdminTest.php @@ -31,7 +31,7 @@ class LogViewerAdminTest extends FunctionalTest /** * {@inheritDoc} */ - public function setUp() + public function setUp(): void { parent::setUp(); diff --git a/tests/Forms/GridField/GridFieldClearAllButtonTest.php b/tests/Forms/GridField/GridFieldClearAllButtonTest.php index ddf2421..0fb6f52 100644 --- a/tests/Forms/GridField/GridFieldClearAllButtonTest.php +++ b/tests/Forms/GridField/GridFieldClearAllButtonTest.php @@ -35,7 +35,7 @@ class GridFieldClearAllButtonTest extends SapphireTest /** * {@inheritDoc} */ - public function setUp() + public function setUp(): void { parent::setUp(); @@ -67,9 +67,9 @@ public function testGetHtmlFragments() $fragments = $this->getSubject()->getHTMLFragments($this->gridField); $this->assertArrayHasKey('before', $fragments); - $this->assertContains('Clear all', $fragments['before']); - $this->assertContains('clear-all-logs', $fragments['before']); - $this->assertContains('font-icon-trash-bin action_clear', $fragments['before']); + $this->assertStringContainsString('Clear all', $fragments['before']); + $this->assertStringContainsString('clear-all-logs', $fragments['before']); + $this->assertStringContainsString('font-icon-trash-bin action_clear', $fragments['before']); } /** diff --git a/tests/Handler/DataObjectHandlerTest.php b/tests/Handler/DataObjectHandlerTest.php index d5b4d62..172fd81 100644 --- a/tests/Handler/DataObjectHandlerTest.php +++ b/tests/Handler/DataObjectHandlerTest.php @@ -2,6 +2,7 @@ namespace SilverLeague\LogViewer\Tests\Handler; +use Monolog\Logger; use Psr\Log\LoggerInterface; use SilverLeague\LogViewer\Handler\DataObjectHandler; use SilverLeague\LogViewer\Model\LogEntry; @@ -19,13 +20,13 @@ class DataObjectHandlerTest extends SapphireTest * A Logger instance * @var Monolog\Logger */ - protected $logger; + protected Logger $logger; /** * The original logger handlers * @var Monolog\LoggerInterface[] */ - protected $originalHandlers = []; + protected $originalHandlers = []; /** * {@inheritDoc} @@ -37,7 +38,7 @@ class DataObjectHandlerTest extends SapphireTest * * {@inheritDoc} */ - public function setUp() + public function setUp(): void { parent::setUp(); @@ -54,10 +55,10 @@ public function setUp() public function testWriteToDefaultLogger() { $this->logger->pushHandler(new DataObjectHandler); - $this->logger->addError('Hello world'); + $this->logger->error('Hello world'); $logEntry = LogEntry::get()->first(); - $this->assertContains('Hello world', $logEntry->Entry); + $this->assertStringContainsString('Hello world', $logEntry->Entry); $this->assertSame('ERROR', $logEntry->Level); } @@ -70,13 +71,13 @@ public function testDontLogMessagesLowerThanMinimumLever() LogEntry::get()->removeAll(); $this->logger->pushHandler(new DataObjectHandler); - $this->logger->addDebug('Debug'); + $this->logger->debug('Debug'); $this->assertSame(0, LogEntry::get()->count()); - $this->logger->addWarning('Warning'); + $this->logger->warning('Warning'); $this->assertGreaterThan(0, LogEntry::get()->filter('Level', 'WARNING')->count()); - $this->logger->addAlert('Alert'); + $this->logger->alert('Alert'); $this->assertGreaterThan(0, LogEntry::get()->filter('Level', 'ALERT')->count()); } @@ -85,8 +86,8 @@ public function testDontLogMessagesLowerThanMinimumLever() */ public function testGetMinimumLogLevelFromConfiguration() { - Config::modify()->set(LogEntry::class, 'minimum_log_level', 123); - $this->assertSame(123, (new DataObjectHandler)->getMinimumLogLevel()); + Config::modify()->set(LogEntry::class, 'minimum_log_level', 300); + $this->assertSame(300, (new DataObjectHandler)->getMinimumLogLevel()); } /** @@ -94,7 +95,7 @@ public function testGetMinimumLogLevelFromConfiguration() * * {@inheritDoc} */ - public function tearDown() + public function tearDown(): void { $this->logger->setHandlers($this->originalHandlers); diff --git a/tests/Model/LogEntryTest.php b/tests/Model/LogEntryTest.php index a5051c9..4f27a63 100644 --- a/tests/Model/LogEntryTest.php +++ b/tests/Model/LogEntryTest.php @@ -85,13 +85,14 @@ public function testLogEntriesAreFormattedAsJson() ]; $entry = LogEntry::create(); - $entry->Entry = Convert::raw2json($data); + $entry->Entry = json_encode($data); + $fields = $entry->getCMSFields(); $field = $fields->fieldByName('Root.Main.Entry'); $this->assertInstanceOf(LiteralField::class, $field); - $this->assertContains(Convert::raw2json($data, JSON_PRETTY_PRINT), $field->getContent()); - $this->assertContains('
', $field->getContent());
+        $this->assertStringContainsString(json_encode($data, JSON_PRETTY_PRINT), $field->getContent());
+        $this->assertStringContainsString('
', $field->getContent());
     }
 }
diff --git a/tests/Task/RemoveOldLogEntriesTaskTest.php b/tests/Task/RemoveOldLogEntriesTaskTest.php
index fc88385..c303295 100644
--- a/tests/Task/RemoveOldLogEntriesTaskTest.php
+++ b/tests/Task/RemoveOldLogEntriesTaskTest.php
@@ -31,8 +31,8 @@ class RemoveOldLogEntriesTaskTest extends SapphireTest
     public function testClassProperties()
     {
         $task = new RemoveOldLogEntriesTask;
-        $this->assertContains('Remove LogEntry', $task->getTitle());
-        $this->assertContains('that are older than', $task->getDescription());
+        $this->assertStringContainsString('Remove LogEntry', $task->getTitle());
+        $this->assertStringContainsString('that are older than', $task->getDescription());
         $this->assertSame('RemoveOldLogEntriesTask', Config::inst()->get(RemoveOldLogEntriesTask::class, 'segment'));
     }
 
@@ -99,10 +99,10 @@ public function testRemoveOldLogEntries()
         $buffer = ob_get_clean();
 
         $this->assertTrue($result);
-        $this->assertContains('Removed 6 log(s)', $buffer);
+        $this->assertStringContainsString('Removed 6 log(s)', $buffer);
         // Nothing to do the second time
         $this->assertFalse($second);
-        $this->assertContains('Removed 0 log(s)', $buffer);
-        $this->assertContains('older than 1 day(s)', $buffer);
+        $this->assertStringContainsString('Removed 0 log(s)', $buffer);
+        $this->assertStringContainsString('older than 1 day(s)', $buffer);
     }
 }