From 1ffdb58fc4950696ed948d6f64fd284875419236 Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Sun, 3 Nov 2024 12:48:53 +0100 Subject: [PATCH] Added possibillity to add more than one message to messagebag in one go --- src/ZugferdMailMessageBag.php | 15 +- src/ZugferdMailReader.php | 2 +- .../ZugferdMailParsesPlaceholders.php | 2 +- .../ZugferdMailSendsMessagesToMessageBag.php | 285 +++++++++++++ .../ZugferdMailConcernMessageBagTest.php | 389 ++++++++++++++++++ 5 files changed, 681 insertions(+), 12 deletions(-) diff --git a/src/ZugferdMailMessageBag.php b/src/ZugferdMailMessageBag.php index ae6f9b3..3e230c3 100644 --- a/src/ZugferdMailMessageBag.php +++ b/src/ZugferdMailMessageBag.php @@ -215,16 +215,11 @@ public function addThrowable(Throwable $throwable, string $source, array $additi */ private function getMessageBagFilteredByType(string $messageType): array { - return array_map( - function ($data) { - return $data["message"]; - }, - array_filter( - $this->messageContainer, - function ($data) use ($messageType) { - return $data['type'] == $messageType; - } - ) + return array_filter( + $this->messageContainer, + function ($data) use ($messageType) { + return $data['type'] == $messageType; + } ); } diff --git a/src/ZugferdMailReader.php b/src/ZugferdMailReader.php index 7f1c5c7..2e04be8 100644 --- a/src/ZugferdMailReader.php +++ b/src/ZugferdMailReader.php @@ -276,7 +276,7 @@ private function validateDocument(ZugferdDocument $document, array $messageAddit { if ($this->config->getSymfonyValidationEnabled()) { $validator = new ZugferdDocumentValidator($document); - $this->raiseRuntimeExceptionIf(count($validator->validateDocument()) != 0, "Validation against Symfony-Validation failed"); + $this->raiseRuntimeExceptionIf($validator->validateDocument()->count() != 0, "Validation against Symfony-Validation failed"); $this->addSuccessMessageToMessageBag('The document was successfully validated against Symfony validator', $messageAdditionalData); } else { $this->addLogSecondaryMessageToMessageBag('The document was not validated against Symfony validator (Disabled)', $messageAdditionalData); diff --git a/src/concerns/ZugferdMailParsesPlaceholders.php b/src/concerns/ZugferdMailParsesPlaceholders.php index a592799..18dbfc8 100644 --- a/src/concerns/ZugferdMailParsesPlaceholders.php +++ b/src/concerns/ZugferdMailParsesPlaceholders.php @@ -40,7 +40,7 @@ protected function parsePlaceholdersByZugferdDocumentReader(ZugferdDocumentReade * Parses a string ($subject) with values from an array ($keyValuePairs) * * @param array $keyValuePairs - * @param string $subject + * @param string $subject * @return string */ protected function parsePlaceholdersByArray(array $keyValuePairs, string $subject): string diff --git a/src/concerns/ZugferdMailSendsMessagesToMessageBag.php b/src/concerns/ZugferdMailSendsMessagesToMessageBag.php index 453e55b..c1ec99b 100644 --- a/src/concerns/ZugferdMailSendsMessagesToMessageBag.php +++ b/src/concerns/ZugferdMailSendsMessagesToMessageBag.php @@ -123,6 +123,291 @@ protected function addThrowableToMessageBag(Throwable $throwable, array $additio return $this; } + /** + * Helper function for adding messages to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param string $type + * @param string $message + * @param array $additionalData + * @return static + */ + protected function addMessageToMessageBagIf(bool $condition, string $type, string $message, array $additionalData = []) + { + return $condition === true ? $this->addMessageToMessageBag($type, $message, $additionalData) : $this; + } + + /** + * Helper function for adding a log entry to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param string $message + * @param array $additionalData + * @return static + */ + protected function addLogMessageToMessageBagIf(bool $condition, string $message, array $additionalData = []) + { + return $condition === true ? $this->addLogMessageToMessageBag($message, $additionalData) : $this; + } + + /** + * Helper function for adding a secondary log entry to message bag if $condition evaluates to true + * + * @param string $message + * @param array $additionalData + * @return static + */ + protected function addLogSecondaryMessageToMessageBagIf(bool $condition, string $message, array $additionalData = []) + { + return $condition === true ? $this->addLogSecondaryMessageToMessageBag($message, $additionalData) : $this; + } + + /** + * Helper function for adding a warning entry to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param string $message + * @param array $additionalData + * @return static + */ + protected function addWarningMessageToMessageBagIf(bool $condition, string $message, array $additionalData = []) + { + return $condition === true ? $this->addWarningMessageToMessageBag($message, $additionalData) : $this; + } + + /** + * Helper function for adding a error entry to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param string $message + * @param array $additionalData + * @return static + */ + protected function addErrorMessageToMessageBagIf(bool $condition, string $message, array $additionalData = []) + { + return $condition === true ? $this->addErrorMessageToMessageBag($message, $additionalData) : $this; + } + + /** + * Helper function for adding a success entry to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param string $message + * @param array $additionalData + * @return static + */ + protected function addSuccessMessageToMessageBagIf(bool $condition, string $message, array $additionalData = []) + { + return $condition === true ? $this->addSuccessMessageToMessageBag($message, $additionalData) : $this; + } + + /** + * Helper function for adding an throwable (Exception) to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param Throwable $throwable + * @param array $additionalData + * @return static + */ + protected function addThrowableToMessageBagIf(bool $condition, Throwable $throwable, array $additionalData = []) + { + return $condition === true ? $this->addThrowableToMessageBag($throwable, $additionalData) : $this; + } + + /** + * Helper function for adding multiple messages to message bag + * + * @param string $type + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleMessagesToMessageBag(string $type, array $messages, array $additionalData = []) + { + array_walk( + $messages, + function ($message) use ($type, $additionalData) { + $this->addMessageToMessageBag($type, $message, $additionalData); + } + ); + + return $this; + } + + /** + * Helper function for adding multiple log entries to message bag + * + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleLogMessagesToMessageBag(array $messages, array $additionalData = []) + { + array_walk( + $messages, + function ($message) use ($additionalData) { + $this->addLogMessageToMessageBag($message, $additionalData); + } + ); + + return $this; + } + + /** + * Helper function for adding multiple secondary log entries to message bag + * + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleLogSecondaryMessagesToMessageBag(array $messages, array $additionalData = []) + { + array_walk( + $messages, + function ($message) use ($additionalData) { + $this->addLogSecondaryMessageToMessageBag($message, $additionalData); + } + ); + + return $this; + } + + /** + * Helper function for adding multiple warning entries to message bag + * + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleWarningMessagesToMessageBag(array $messages, array $additionalData = []) + { + array_walk( + $messages, + function ($message) use ($additionalData) { + $this->addWarningMessageToMessageBag($message, $additionalData); + } + ); + + return $this; + } + + /** + * Helper function for adding multiple error entries to message bag + * + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleErrorMessagesToMessageBag(array $messages, array $additionalData = []) + { + array_walk( + $messages, + function ($message) use ($additionalData) { + $this->addErrorMessageToMessageBag($message, $additionalData); + } + ); + + return $this; + } + + /** + * Helper function for adding multiple success entries to message bag + * + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleSuccessMessagesToMessageBag(array $messages, array $additionalData = []) + { + array_walk( + $messages, + function ($message) use ($additionalData) { + $this->addSuccessMessageToMessageBag($message, $additionalData); + } + ); + + return $this; + } + + /** + * Helper function for adding multiple messages to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param string $type + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleMessagesToMessageBagIf(bool $condition, string $type, array $messages, array $additionalData = []) + { + return $condition === true ? $this->addMultipleMessagesToMessageBag($type, $messages, $additionalData) : $this; + } + + /** + * Helper function for adding multiple log entries to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleLogMessagesToMessageBagIf(bool $condition, array $messages, array $additionalData = []) + { + return $condition === true ? $this->addMultipleLogMessagesToMessageBag($messages, $additionalData) : $this; + } + + /** + * Helper function for adding multiple secondary log entries to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleLogSecondaryMessagesToMessageBagIf(bool $condition, array $messages, array $additionalData = []) + { + return $condition === true ? $this->addMultipleLogSecondaryMessagesToMessageBag($messages, $additionalData) : $this; + } + + /** + * Helper function for adding multiple warning entries to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleWarningMessagesToMessageBagIf(bool $condition, array $messages, array $additionalData = []) + { + return $condition === true ? $this->addMultipleWarningMessagesToMessageBag($messages, $additionalData) : $this; + } + + /** + * Helper function for adding multiple error entries to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleErrorMessagesToMessageBagIf(bool $condition, array $messages, array $additionalData = []) + { + return $condition === true ? $this->addMultipleErrorMessagesToMessageBag($messages, $additionalData) : $this; + } + + /** + * Helper function for adding multiple success entries to message bag if $condition evaluates to true + * + * @param boolean $condition + * @param array $messages + * @param array $additionalData + * @return static + */ + protected function addMultipleSuccessMessagesToMessageBagIf(bool $condition, array $messages, array $additionalData = []) + { + return $condition === true ? $this->addMultipleSuccessMessagesToMessageBag($messages, $additionalData) : $this; + } + /** * Helper function for getting the source from a current classname * diff --git a/tests/testcases/ZugferdMailConcernMessageBagTest.php b/tests/testcases/ZugferdMailConcernMessageBagTest.php index 8882fb1..93f3740 100644 --- a/tests/testcases/ZugferdMailConcernMessageBagTest.php +++ b/tests/testcases/ZugferdMailConcernMessageBagTest.php @@ -480,4 +480,393 @@ public function testMessageBagToString() $this->assertObjectHasProperty('additionalData', $messageBagJson[0]); $this->assertObjectHasProperty('datetime', $messageBagJson[0]); } + + public function testAddMultipleMessagesToMessageBag() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleMessagesToMessageBag(ZugferdMailMessageBagType::MESSAGETYPE_LOG, ["Test 1", "Test 2"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(2, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleLogMessagesToMessageBag() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleLogMessagesToMessageBag(["Test 1", "Test 2", "Test 3"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(3, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleLogSecondaryMessagesToMessageBag() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleLogSecondaryMessagesToMessageBag(["Test 1", "Test 2", "Test 3"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleWarningMessagesToMessageBag() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleWarningMessagesToMessageBag(["Test 1", "Test 2", "Test 3"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleErrorMessagesToMessageBag() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleErrorMessagesToMessageBag(["Test 1", "Test 2", "Test 3"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleSuccessMessagesToMessageBag() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleSuccessMessagesToMessageBag(["Test 1", "Test 2", "Test 3"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(3, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMessageToMessageBagIf(): void + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMessageToMessageBagIf(false, ZugferdMailMessageBagType::MESSAGETYPE_LOG, 'Message 1'); + $this->addMessageToMessageBagIf(true, ZugferdMailMessageBagType::MESSAGETYPE_LOG, 'Message 2'); + $this->addMessageToMessageBagIf(false, ZugferdMailMessageBagType::MESSAGETYPE_LOG, 'Message 3'); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(1, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getAllMessagesFromMessageBag())); + + $this->assertArrayHasKey(0, $this->getLogMessagesFromMessageBag()); + $this->assertArrayNotHasKey(1, $this->getLogMessagesFromMessageBag()); + $this->assertEquals("Message 2", $this->getLogMessagesFromMessageBag()[0]["message"]); + } + + public function testAddLogMessageToMessageBagIf(): void + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addLogMessageToMessageBagIf(false, 'Message 1'); + $this->addLogMessageToMessageBagIf(true, 'Message 2'); + $this->addLogMessageToMessageBagIf(false, 'Message 3'); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(1, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getAllMessagesFromMessageBag())); + + $this->assertArrayHasKey(0, $this->getLogMessagesFromMessageBag()); + $this->assertArrayNotHasKey(1, $this->getLogMessagesFromMessageBag()); + $this->assertEquals("Message 2", $this->getLogMessagesFromMessageBag()[0]["message"]); + } + + public function testAddLogSecondaryMessageToMessageBagIf(): void + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addLogSecondaryMessageToMessageBagIf(false, 'Message 1'); + $this->addLogSecondaryMessageToMessageBagIf(true, 'Message 2'); + $this->addLogSecondaryMessageToMessageBagIf(false, 'Message 3'); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getAllMessagesFromMessageBag())); + + $this->assertArrayHasKey(0, $this->getLogSecondaryMessagesFromMessageBag()); + $this->assertArrayNotHasKey(1, $this->getLogSecondaryMessagesFromMessageBag()); + $this->assertEquals("Message 2", $this->getLogSecondaryMessagesFromMessageBag()[0]["message"]); + } + + public function testAddWarningMessageToMessageBagIf(): void + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addWarningMessageToMessageBagIf(false, 'Message 1'); + $this->addWarningMessageToMessageBagIf(true, 'Message 2'); + $this->addWarningMessageToMessageBagIf(false, 'Message 3'); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getAllMessagesFromMessageBag())); + + $this->assertArrayHasKey(0, $this->getWarningMessagesFromMessageBag()); + $this->assertArrayNotHasKey(1, $this->getWarningMessagesFromMessageBag()); + $this->assertEquals("Message 2", $this->getWarningMessagesFromMessageBag()[0]["message"]); + } + + public function testAddErrorMessageToMessageBagIf(): void + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addErrorMessageToMessageBagIf(false, 'Message 1'); + $this->addErrorMessageToMessageBagIf(true, 'Message 2'); + $this->addErrorMessageToMessageBagIf(false, 'Message 3'); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getAllMessagesFromMessageBag())); + + $this->assertArrayHasKey(0, $this->getErrorMessagesFromMessageBag()); + $this->assertArrayNotHasKey(1, $this->getErrorMessagesFromMessageBag()); + $this->assertEquals("Message 2", $this->getErrorMessagesFromMessageBag()[0]["message"]); + } + + public function testAddSuccessMessageToMessageBagIf(): void + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addSuccessMessageToMessageBagIf(false, 'Message 1'); + $this->addSuccessMessageToMessageBagIf(true, 'Message 2'); + $this->addSuccessMessageToMessageBagIf(false, 'Message 3'); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getAllMessagesFromMessageBag())); + + $this->assertArrayHasKey(0, $this->getSuccessMessagesFromMessageBag()); + $this->assertArrayNotHasKey(1, $this->getSuccessMessagesFromMessageBag()); + $this->assertEquals("Message 2", $this->getSuccessMessagesFromMessageBag()[0]["message"]); + } + + public function testAddThrowableToMessageBagIf(): void + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addThrowableToMessageBagIf(false, new \Exception("Message 1")); + $this->addThrowableToMessageBagIf(true, new \Exception("Message 2")); + $this->addThrowableToMessageBagIf(false, new \Exception("Message 3")); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(1, count($this->getAllMessagesFromMessageBag())); + + $this->assertArrayHasKey(0, $this->getErrorMessagesFromMessageBag()); + $this->assertArrayNotHasKey(1, $this->getErrorMessagesFromMessageBag()); + $this->assertEquals("Message 2", $this->getErrorMessagesFromMessageBag()[0]["message"]); + } + + public function testAddMultipleMessagesToMessageBagIf() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleMessagesToMessageBagIf(false, ZugferdMailMessageBagType::MESSAGETYPE_LOG, ["Test 1", "Test 2"]); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleMessagesToMessageBagIf(true, ZugferdMailMessageBagType::MESSAGETYPE_LOG, ["Test 1", "Test 2"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(2, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleLogMessagesToMessageBagIf() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleLogMessagesToMessageBagIf(false, ["Test 1", "Test 2"]); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleLogMessagesToMessageBagIf(true, ["Test 1", "Test 2"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(2, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleLogSecondaryMessagesToMessageBagIf() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleLogSecondaryMessagesToMessageBagIf(false, ["Test 1", "Test 2"]); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleLogSecondaryMessagesToMessageBagIf(true, ["Test 1", "Test 2"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleWarningMessagesToMessageBagIf() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleWarningMessagesToMessageBagIf(false, ["Test 1", "Test 2"]); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleWarningMessagesToMessageBagIf(true, ["Test 1", "Test 2"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleErrorMessagesToMessageBagIf() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleErrorMessagesToMessageBagIf(false, ["Test 1", "Test 2"]); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleErrorMessagesToMessageBagIf(true, ["Test 1", "Test 2"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getAllMessagesFromMessageBag())); + } + + public function testAddMultipleSuccessMessagesToMessageBagIf() + { + $this->clearMessageBag(); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleSuccessMessagesToMessageBagIf(false, ["Test 1", "Test 2"]); + + $this->assertFalse($this->getHasAnyMessageInMessageBag()); + + $this->addMultipleSuccessMessagesToMessageBagIf(true, ["Test 1", "Test 2"]); + + $this->assertTrue($this->getHasAnyMessageInMessageBag()); + $this->assertEquals(0, count($this->getLogMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getLogSecondaryMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getWarningMessagesFromMessageBag())); + $this->assertEquals(0, count($this->getErrorMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getSuccessMessagesFromMessageBag())); + $this->assertEquals(2, count($this->getAllMessagesFromMessageBag())); + } }