From 5ee6223434b3e217a7c81d1dc5079a9af2dc7861 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Fri, 10 Mar 2023 16:04:55 +0100 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20N=C2=B05893=20Add=20te?= =?UTF-8?q?st=20for=20\TriggerOnObject::LogException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unitary-tests/core/TriggerTest.php | 67 +++++++++++++------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/tests/php-unit-tests/unitary-tests/core/TriggerTest.php b/tests/php-unit-tests/unitary-tests/core/TriggerTest.php index 87b41001e..bf91d28b9 100644 --- a/tests/php-unit-tests/unitary-tests/core/TriggerTest.php +++ b/tests/php-unit-tests/unitary-tests/core/TriggerTest.php @@ -4,7 +4,10 @@ namespace Combodo\iTop\Test\UnitTest\Core; use Combodo\iTop\Test\UnitTest\ItopDataTestCase; use ContextTag; +use Exception; +use IssueLog; use MetaModel; +use Person; use TriggerOnObjectCreate; /** @@ -14,7 +17,6 @@ use TriggerOnObjectCreate; * * @runTestsInSeparateProcesses */ - class TriggerTest extends ItopDataTestCase { const USE_TRANSACTION = false; @@ -40,33 +42,29 @@ class TriggerTest extends ItopDataTestCase public function testEnrichRaisedException_Trigger() { $oTrigger = MetaModel::NewObject('TriggerOnObjectCreate'); - try - { - try - { + try { + try { MetaModel::NewObject('Toto'); } - catch (\Exception $e) - { + catch (\Exception $e) { \utils::EnrichRaisedException($oTrigger, $e); } $this->assertTrue(false, "An exception should have been thrown"); } - catch(\CoreException $e1) - { + catch (\CoreException $e1) { $this->assertEquals('CoreException', get_class($e1)); $this->assertEquals('Unknown class \'Toto\' (TriggerOnObjectCreate::-1 ()
)', $e1->getMessage()); $fullStackTraceAsString = $e1->getFullStackTraceAsString(); - $this->assertContains("MetaModel::NewObject", $fullStackTraceAsString,"new enriched exception should contain root cause method: " . $fullStackTraceAsString); + $this->assertContains("MetaModel::NewObject", $fullStackTraceAsString, "new enriched exception should contain root cause method: ".$fullStackTraceAsString); } } public function NoEnrichmentProvider() { return [ - [ null ], - [ new NonCmdbAbstractObject() ], + [null], + [new NonCmdbAbstractObject()], ] ; } @@ -76,23 +74,52 @@ class TriggerTest extends ItopDataTestCase */ public function testEnrichRaisedException_NoEnrichment($oCmdbAbstract) { - try - { - try - { + try { + try { MetaModel::NewObject('CoreException'); } - catch (\Exception $e) - { + catch (\Exception $e) { \utils::EnrichRaisedException($oCmdbAbstract, $e); } $this->assertTrue(false, "An exception should have been thrown"); } - catch(\Exception $e1) - { + catch (\Exception $e1) { $this->assertEquals($e, $e1); } } + + public function testLogException() + { + $sTestLogPath = APPROOT.'log/TriggerTest__testLogException.log'; + IssueLog::Enable($sTestLogPath); + + try { + $oPerson1 = MetaModel::GetObject(Person::class, 1, true); + $sExceptionMessage = 'My test exception message'; + $oException = new Exception($sExceptionMessage); + + /** @var TriggerOnObjectCreate $oTrigger */ + $oTrigger = MetaModel::NewObject(TriggerOnObjectCreate::class, [ + 'description' => 'my trigger description', + ]); + $oTrigger->DBWrite(); + $oTrigger->LogException($oException, $oPerson1); + + $sTestLogFileContent = file_get_contents($sTestLogPath); + + $this->assertContains('A trigger did throw an exception', $sTestLogFileContent); + + $this->assertContains($oPerson1->GetKey(), $sTestLogFileContent); + /** @noinspection GetClassUsageInspection */ + $this->assertContains(get_class($oPerson1), $sTestLogFileContent); + $this->assertContains($oPerson1->GetRawName(), $sTestLogFileContent); + + $this->assertContains($sExceptionMessage, $sTestLogFileContent); + } + finally { + IssueLog::Enable(APPROOT.'log/error.log'); + } + } } class NonCmdbAbstractObject{