From d2389b79093e21eba9a8805df474102c2bd736cc Mon Sep 17 00:00:00 2001 From: neneaX Date: Sun, 31 Jan 2016 16:43:50 +0200 Subject: [PATCH] / Redesigned the Notification class / Redesigned the NotificationCollection class + Added Classes for Notification attributes + Added support for PSR-3 list of logger interface levels + Created a class that extends Notification class, for each logger level: * Emergency * Critical * Alert * Error * Warning * Notice * Info * Debug + Created a Notification factory + Created a Notification Factory facade: Notif + Created a Presentable Interface for toString, toArray, toOutput, toJson methods + Created a Presentable Trait for basic implementations of toOutput and toJson methods --- composer.json | 1 + composer.lock | 56 ++++- src/Alert.php | 25 +++ src/Critical.php | 25 +++ src/Debug.php | 25 +++ src/Emergency.php | 25 +++ src/Error.php | 25 +++ src/Exceptions/Codes.php | 4 + .../InvalidNotificationTypeException.php | 35 +++ .../NotificationTypeDomainException.php | 35 +++ src/Info.php | 25 +++ src/Label.php | 106 +++++++++ src/Message.php | 106 +++++++++ src/Notice.php | 25 +++ src/Notif.php | 139 ++++++++++++ src/Notification.php | 171 +++++++++----- src/NotificationCollection.php | 80 +++++-- src/NotificationFactory.php | 209 ++++++++++++++++++ src/NotificationInterface.php | 11 +- src/Presentable.php | 56 +++++ src/PresentableTrait.php | 59 +++++ src/Type.php | 108 +++++++++ src/TypeList.php | 20 ++ src/Warning.php | 25 +++ tests/Alert/ConstructTest.php | 101 +++++++++ tests/Critical/ConstructTest.php | 101 +++++++++ tests/Debug/ConstructTest.php | 101 +++++++++ tests/Emergency/ConstructTest.php | 101 +++++++++ tests/Error/ConstructTest.php | 101 +++++++++ tests/Info/ConstructTest.php | 101 +++++++++ tests/Label/ConstructTest.php | 75 +++++++ tests/Label/EqualsTest.php | 60 +++++ tests/Label/GetValueTest.php | 22 ++ tests/Label/ToArrayTest.php | 72 ++++++ tests/Label/ToJsonTest.php | 88 ++++++++ tests/Label/ToOutputTest.php | 120 ++++++++++ tests/Label/ToStringTest.php | 26 +++ tests/Label/__ToStringTest.php | 22 ++ tests/Message/ConstructTest.php | 75 +++++++ tests/Message/EqualsTest.php | 60 +++++ tests/Message/GetValueTest.php | 22 ++ tests/Message/ToArrayTest.php | 72 ++++++ tests/Message/ToJsonTest.php | 88 ++++++++ tests/Message/ToOutputTest.php | 120 ++++++++++ tests/Message/ToStringTest.php | 26 +++ tests/Message/__ToStringTest.php | 22 ++ tests/Notice/ConstructTest.php | 101 +++++++++ tests/Notification/ConstructTest.php | 97 +++++--- tests/Type/ConstructTest.php | 75 +++++++ tests/Type/EqualsTest.php | 60 +++++ tests/Type/GetValueTest.php | 22 ++ tests/Type/ToArrayTest.php | 72 ++++++ tests/Type/ToJsonTest.php | 88 ++++++++ tests/Type/ToOutputTest.php | 120 ++++++++++ tests/Type/ToStringTest.php | 26 +++ tests/Type/__ToStringTest.php | 22 ++ tests/Warning/ConstructTest.php | 101 +++++++++ 57 files changed, 3637 insertions(+), 119 deletions(-) create mode 100644 src/Alert.php create mode 100644 src/Critical.php create mode 100644 src/Debug.php create mode 100644 src/Emergency.php create mode 100644 src/Error.php create mode 100644 src/Exceptions/InvalidNotificationTypeException.php create mode 100644 src/Exceptions/NotificationTypeDomainException.php create mode 100644 src/Info.php create mode 100644 src/Label.php create mode 100644 src/Message.php create mode 100644 src/Notice.php create mode 100644 src/Notif.php create mode 100644 src/NotificationFactory.php create mode 100644 src/Presentable.php create mode 100644 src/PresentableTrait.php create mode 100644 src/Type.php create mode 100644 src/TypeList.php create mode 100644 src/Warning.php create mode 100644 tests/Alert/ConstructTest.php create mode 100644 tests/Critical/ConstructTest.php create mode 100644 tests/Debug/ConstructTest.php create mode 100644 tests/Emergency/ConstructTest.php create mode 100644 tests/Error/ConstructTest.php create mode 100644 tests/Info/ConstructTest.php create mode 100644 tests/Label/ConstructTest.php create mode 100644 tests/Label/EqualsTest.php create mode 100644 tests/Label/GetValueTest.php create mode 100644 tests/Label/ToArrayTest.php create mode 100644 tests/Label/ToJsonTest.php create mode 100644 tests/Label/ToOutputTest.php create mode 100644 tests/Label/ToStringTest.php create mode 100644 tests/Label/__ToStringTest.php create mode 100644 tests/Message/ConstructTest.php create mode 100644 tests/Message/EqualsTest.php create mode 100644 tests/Message/GetValueTest.php create mode 100644 tests/Message/ToArrayTest.php create mode 100644 tests/Message/ToJsonTest.php create mode 100644 tests/Message/ToOutputTest.php create mode 100644 tests/Message/ToStringTest.php create mode 100644 tests/Message/__ToStringTest.php create mode 100644 tests/Notice/ConstructTest.php create mode 100644 tests/Type/ConstructTest.php create mode 100644 tests/Type/EqualsTest.php create mode 100644 tests/Type/GetValueTest.php create mode 100644 tests/Type/ToArrayTest.php create mode 100644 tests/Type/ToJsonTest.php create mode 100644 tests/Type/ToOutputTest.php create mode 100644 tests/Type/ToStringTest.php create mode 100644 tests/Type/__ToStringTest.php create mode 100644 tests/Warning/ConstructTest.php diff --git a/composer.json b/composer.json index 9744e40..9a05caf 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ ], "require": { "php": ">=5.4.0", + "psr/log": "1.*", "xqddd/exceptions": "0.*" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 1c144aa..1f5eb07 100644 --- a/composer.lock +++ b/composer.lock @@ -4,25 +4,63 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "dc583bae49d1a39188324a1b0ae29af6", - "content-hash": "8940e2a748fb6f4f3bfb836876679219", + "hash": "123910c3d5097f442d165795066a3f6e", + "content-hash": "b472b97b8a2a087e3b36bf2a2fb6595d", "packages": [ + { + "name": "psr/log", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "fe0936ee26643249e916849d48e3a51d5f5e278b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/fe0936ee26643249e916849d48e3a51d5f5e278b", + "reference": "fe0936ee26643249e916849d48e3a51d5f5e278b", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-0": { + "Psr\\Log\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2012-12-21 11:40:51" + }, { "name": "xqddd/exceptions", - "version": "0.2.0", + "version": "0.2.1", "source": { "type": "git", "url": "https://github.com/xqddd/Exceptions.git", - "reference": "88f6f915f4c9667385ab7cc8da71a9120908d1fc" + "reference": "2ffdaa8e62d2ade8e599ed0cf01e5b1e2771fa7b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/xqddd/Exceptions/zipball/88f6f915f4c9667385ab7cc8da71a9120908d1fc", - "reference": "88f6f915f4c9667385ab7cc8da71a9120908d1fc", + "url": "https://api.github.com/repos/xqddd/Exceptions/zipball/2ffdaa8e62d2ade8e599ed0cf01e5b1e2771fa7b", + "reference": "2ffdaa8e62d2ade8e599ed0cf01e5b1e2771fa7b", "shasum": "" }, "require": { - "php": ">=5.3.0" + "php": ">=5.4.0" }, "require-dev": { "mockery/mockery": "0.9.*" @@ -45,7 +83,7 @@ } ], "description": "A small library with classes extending the PHP exceptions by adding additional parameters for custom use.", - "time": "2016-01-09 15:37:14" + "time": "2016-01-29 22:11:45" } ], "packages-dev": [ @@ -166,7 +204,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=5.3.0" + "php": ">=5.4.0" }, "platform-dev": [] } diff --git a/src/Alert.php b/src/Alert.php new file mode 100644 index 0000000..9590ca2 --- /dev/null +++ b/src/Alert.php @@ -0,0 +1,25 @@ + + * @package Xqddd\Notifications + */ +class Alert extends Notification +{ + + /** + * Alert constructor. + * + * @param Label $label + * @param Message $message + */ + public function __construct(Label $label, Message $message) + { + $type = new Type(TypeList::ALERT); + + parent::__construct($label, $message, $type); + } +} diff --git a/src/Critical.php b/src/Critical.php new file mode 100644 index 0000000..d46375d --- /dev/null +++ b/src/Critical.php @@ -0,0 +1,25 @@ + + * @package Xqddd\Notifications + */ +class Critical extends Notification +{ + + /** + * Critical constructor. + * + * @param Label $label + * @param Message $message + */ + public function __construct(Label $label, Message $message) + { + $type = new Type(TypeList::CRITICAL); + + parent::__construct($label, $message, $type); + } +} diff --git a/src/Debug.php b/src/Debug.php new file mode 100644 index 0000000..4806590 --- /dev/null +++ b/src/Debug.php @@ -0,0 +1,25 @@ + + * @package Xqddd\Notifications + */ +class Debug extends Notification +{ + + /** + * Debug constructor. + * + * @param Label $label + * @param Message $message + */ + public function __construct(Label $label, Message $message) + { + $type = new Type(TypeList::DEBUG); + + parent::__construct($label, $message, $type); + } +} diff --git a/src/Emergency.php b/src/Emergency.php new file mode 100644 index 0000000..1125566 --- /dev/null +++ b/src/Emergency.php @@ -0,0 +1,25 @@ + + * @package Xqddd\Notifications + */ +class Emergency extends Notification +{ + + /** + * Emergency constructor. + * + * @param Label $label + * @param Message $message + */ + public function __construct(Label $label, Message $message) + { + $type = new Type(TypeList::EMERGENCY); + + parent::__construct($label, $message, $type); + } +} diff --git a/src/Error.php b/src/Error.php new file mode 100644 index 0000000..5374339 --- /dev/null +++ b/src/Error.php @@ -0,0 +1,25 @@ + + * @package Xqddd\Notifications + */ +class Error extends Notification +{ + + /** + * Error constructor. + * + * @param Label $label + * @param Message $message + */ + public function __construct(Label $label, Message $message) + { + $type = new Type(TypeList::ERROR); + + parent::__construct($label, $message, $type); + } +} diff --git a/src/Exceptions/Codes.php b/src/Exceptions/Codes.php index cdcacd8..aa77501 100644 --- a/src/Exceptions/Codes.php +++ b/src/Exceptions/Codes.php @@ -15,4 +15,8 @@ class Codes extends BaseCodes const INVALID_NOTIFICATION_LABEL = 102; const INVALID_NOTIFICATION_MESSAGE = 202; + + const NOTIFICATION_TYPE_DOMAIN = 301; + + const INVALID_NOTIFICATION_TYPE = 302; } diff --git a/src/Exceptions/InvalidNotificationTypeException.php b/src/Exceptions/InvalidNotificationTypeException.php new file mode 100644 index 0000000..5cc5181 --- /dev/null +++ b/src/Exceptions/InvalidNotificationTypeException.php @@ -0,0 +1,35 @@ + + * @package Xqddd\Notifications\Exceptions + */ +class InvalidNotificationTypeException extends InvalidArgumentException +{ + + /** + * The exception message + * + * @var string + */ + protected $message = 'Invalid [%s] notification type: [%s] expected - [%s] given.'; + + /** + * The exception code + * + * @var int + */ + protected $code = Codes::INVALID_NOTIFICATION_TYPE; + + /** + * The exception string code + * + * @var string + */ + protected $stringCode = 'INVALID_NOTIFICATION_TYPE'; +} diff --git a/src/Exceptions/NotificationTypeDomainException.php b/src/Exceptions/NotificationTypeDomainException.php new file mode 100644 index 0000000..857e1c0 --- /dev/null +++ b/src/Exceptions/NotificationTypeDomainException.php @@ -0,0 +1,35 @@ + + * @package Xqddd\Notifications\Exceptions + */ +class NotificationTypeDomainException extends DomainException +{ + + /** + * The exception message + * + * @var string + */ + protected $message = 'The provided notification type [%s] does not exist or does not belong to the current domain [%s].'; + + /** + * The exception code + * + * @var int + */ + protected $code = Codes::NOTIFICATION_TYPE_DOMAIN; + + /** + * The exception string code + * + * @var string + */ + protected $stringCode = 'NOTIFICATION_TYPE_DOMAIN'; +} diff --git a/src/Info.php b/src/Info.php new file mode 100644 index 0000000..27128dd --- /dev/null +++ b/src/Info.php @@ -0,0 +1,25 @@ + + * @package Xqddd\Notifications + */ +class Info extends Notification +{ + + /** + * Info constructor. + * + * @param Label $label + * @param Message $message + */ + public function __construct(Label $label, Message $message) + { + $type = new Type(TypeList::INFO); + + parent::__construct($label, $message, $type); + } +} diff --git a/src/Label.php b/src/Label.php new file mode 100644 index 0000000..4867018 --- /dev/null +++ b/src/Label.php @@ -0,0 +1,106 @@ + + * @package Xqddd\Notifications + */ +class Label implements Presentable +{ + use PresentableTrait; + + /** + * The label string value + * + * @var string + */ + protected $value; + + /** + * Label constructor. + * + * @param string $value + */ + public function __construct($value) + { + $this->validateValue($value); + $this->setValue($value); + } + + /** + * Check if a given value passes validations + * + * @param mixed $value + * @throws InvalidNotificationLabelException + */ + protected function validateValue($value) + { + if (!is_string($value)) { + throw new InvalidNotificationLabelException( + 'label', + 'string', + gettype($value) + ); + } + } + + /** + * Get the label string value + * + * @return string + */ + public function getValue() + { + return $this->value; + } + + /** + * Set the label string value + * + * @param string $value + */ + protected function setValue($value) + { + $this->value = $value; + } + + /** + * @param Label $label + * @return bool + */ + public function equals(Label $label) + { + return $this->value === $label->getValue(); + } + + /** + * Get the string representation of the label + * + * @return string + */ + public function toString() + { + return $this->getValue(); + } + + /** + * Get the array representation of the label + * + * @param bool $assoc + * @return array + */ + public function toArray($assoc = true) + { + return (true === $assoc) + ? [ + 'label' => $this->getValue() + ] + : [ + $this->getValue() + ]; + } +} diff --git a/src/Message.php b/src/Message.php new file mode 100644 index 0000000..b112899 --- /dev/null +++ b/src/Message.php @@ -0,0 +1,106 @@ + + * @package Xqddd\Notifications + */ +class Message implements Presentable +{ + use PresentableTrait; + + /** + * The message string value + * + * @var string + */ + protected $value; + + /** + * Message constructor. + * + * @param string $value + */ + public function __construct($value) + { + $this->validateValue($value); + $this->setValue($value); + } + + /** + * Check if a given value passes validations + * + * @param mixed $value + * @throws InvalidNotificationMessageException + */ + protected function validateValue($value) + { + if (!is_string($value)) { + throw new InvalidNotificationMessageException( + 'message', + 'string', + gettype($value) + ); + } + } + + /** + * Get the message string value + * + * @return string + */ + public function getValue() + { + return $this->value; + } + + /** + * Set the message string value + * + * @param string $value + */ + protected function setValue($value) + { + $this->value = $value; + } + + /** + * @param Message $message + * @return bool + */ + public function equals(Message $message) + { + return $this->value === $message->getValue(); + } + + /** + * Get the string representation of the message + * + * @return string + */ + public function toString() + { + return $this->getValue(); + } + + /** + * Get the array representation of the message + * + * @param bool $assoc + * @return array + */ + public function toArray($assoc = true) + { + return (true === $assoc) + ? [ + 'message' => $this->getValue() + ] + : [ + $this->getValue() + ]; + } +} diff --git a/src/Notice.php b/src/Notice.php new file mode 100644 index 0000000..2285a8f --- /dev/null +++ b/src/Notice.php @@ -0,0 +1,25 @@ + + * @package Xqddd\Notifications + */ +class Notice extends Notification +{ + + /** + * Notice constructor. + * + * @param Label $label + * @param Message $message + */ + public function __construct(Label $label, Message $message) + { + $type = new Type(TypeList::NOTICE); + + parent::__construct($label, $message, $type); + } +} diff --git a/src/Notif.php b/src/Notif.php new file mode 100644 index 0000000..8946383 --- /dev/null +++ b/src/Notif.php @@ -0,0 +1,139 @@ + + * @package Xqddd\Notifications + */ +class Notif +{ + + /** + * @var NotificationFactory + */ + protected static $notificationFactory; + + /** + * @return NotificationFactory + */ + protected static function getNotificationFactory() + { + if (! static::$notificationFactory instanceof NotificationFactory) { + static::$notificationFactory = new NotificationFactory(); + } + return static::$notificationFactory; + } + + /** + * Build a Notification + * + * @param $labelValue + * @param $typeValue + * @param $messageValue + * @return Notification + */ + public static function build($labelValue, $typeValue, $messageValue) + { + return static::getNotificationFactory()->build($labelValue, $typeValue, $messageValue); + } + + + /** + * Build a debug notification + * + * @param $labelValue + * @param $messageValue + * @return Debug + */ + public function buildDebug($labelValue, $messageValue) + { + return static::getNotificationFactory()->buildDebug($labelValue, $messageValue); + } + + + /** + * Build an informative notification + * + * @param $labelValue + * @param $messageValue + * @return Info + */ + public function buildInfo($labelValue, $messageValue) + { + return static::getNotificationFactory()->buildInfo($labelValue, $messageValue); + } + + /** + * Build a notice + * + * @param $labelValue + * @param $messageValue + * @return Notice + */ + public function buildNotice($labelValue, $messageValue) + { + return static::getNotificationFactory()->buildNotice($labelValue, $messageValue); + } + + /** + * Build a warning + * + * @param $labelValue + * @param $messageValue + * @return Warning + */ + public function buildWarning($labelValue, $messageValue) + { + return static::getNotificationFactory()->buildWarning($labelValue, $messageValue); + } + + /** + * Build an error + * + * @param $labelValue + * @param $messageValue + * @return Error + */ + public function buildError($labelValue, $messageValue) + { + return static::getNotificationFactory()->buildError($labelValue, $messageValue); + } + + /** + * Build a critical + * + * @param $labelValue + * @param $messageValue + * @return Critical + */ + public function buildCritical($labelValue, $messageValue) + { + return static::getNotificationFactory()->buildCritical($labelValue, $messageValue); + } + + /** + * Build an alert + * + * @param $labelValue + * @param $messageValue + * @return Alert + */ + public function buildAlert($labelValue, $messageValue) + { + return static::getNotificationFactory()->buildAlert($labelValue, $messageValue); + } + + /** + * Build an emergency + * + * @param $labelValue + * @param $messageValue + * @return Emergency + */ + public function buildEmergency($labelValue, $messageValue) + { + return static::getNotificationFactory()->buildEmergency($labelValue, $messageValue); + } +} diff --git a/src/Notification.php b/src/Notification.php index 3035dbc..25c8b38 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -1,50 +1,54 @@ * @package Xqddd\Notifications */ -class Notification implements NotificationInterface +class Notification implements NotificationInterface, Presentable { /** * The notification label * - * @var string + * @var Label */ protected $label; /** * The notification message * - * @var string + * @var Message */ protected $message; + /** + * The notification type + * + * @var Type + */ + protected $type; + /** * Notification constructor. * - * @param $message - * @param null $label + * @param Label $label + * @param Type $type + * @param Message $message */ - public function __construct($message, $label = null) + public function __construct(Label $label, Message $message, Type $type) { - if (!is_null($label)) { - $this->setLabel($label); - } + $this->setLabel($label); $this->setMessage($message); + $this->setType($type); } /** * Get the notification label * - * @return string + * @return Label */ public function getLabel() { @@ -54,26 +58,18 @@ public function getLabel() /** * Set the notification label * - * @param string $label - * @throws InvalidNotificationLabelException + * @param Label $label */ - protected function setLabel($label) + protected function setLabel(Label $label) { - if (!is_string($label)) { - throw new InvalidNotificationLabelException( - 'label', - 'string', - gettype($label) - ); - } - $this->label = $label; } + /** * Get the notification message * - * @return string + * @return Message */ public function getMessage() { @@ -83,60 +79,125 @@ public function getMessage() /** * Set the notification message * - * @param string $message - * @throws InvalidNotificationMessageException + * @param Message $message */ - protected function setMessage($message) + protected function setMessage(Message $message) { - if (!is_string($message)) { - throw new InvalidNotificationMessageException( - 'message', - 'string', - gettype($message) - ); - } - $this->message = $message; } /** - * Get the notification message formatted to string + * Get the notification type + * + * @return Type + */ + public function getType() + { + return $this->type; + } + + /** + * Set the notification type + * + * @param Type $type + */ + protected function setType(Type $type) + { + $this->type = $type; + } + + /** + * Get the notification formatted as a string * * @return string */ public function toString() { - return $this->getMessage(); + return sprintf( + '[%s]: [[%s]] - [%s]', + [ + ucfirst($this->getType()->toString()), + $this->getLabel()->toString(), + $this->getMessage()->toString() + ] + ); } /** + * Get the notification formatted as an array + * + * @param bool $assoc * @return array */ - public function toArray() + public function toArray($assoc = true) { - $attribute = [ - 'label' => $this->getLabel(), - 'message' => $this->getMessage() - ]; - - return $attribute; + return array_merge( + $this->getLabel()->toArray($assoc), + $this->getMessage()->toArray($assoc), + $this->getType()->toArray($assoc) + ); } - /** - * @param bool $fullStructure - * @return string - */ - public function toJson($fullStructure = false) + /** + * Get the notification formatted as a short array + * + * If associative: [$label => $message] + * Else: [$message] + * + * @param bool $assoc + * @return array + */ + public function toShortArray($assoc = true) { - if (true === $fullStructure) { - $structure = $this->toArray(); + if (true === $assoc) { + return [ + $this->getLabel()->toString() => $this->getMessage()->toString() + ]; } else { - $structure = $this->toString(); + return [ + $this->getMessage()->toString() + ]; } + } - $jsonStructure = json_encode($structure); + /** + * Get the notification formatted as a custom data structure + * + * @param string $structure + * @param bool $assoc + * @return mixed + */ + public function toOutput($structure = 'shortArray', $assoc = true) + { + switch ($structure) { + case 'string': + $output = $this->toString(); + break; + case 'array': + $output = $this->toArray($assoc); + break; + case 'shortArray': + default: + $output = $this->toShortArray($assoc); + break; + } + return $output; + } - return $jsonStructure; + /** + * Get the notification formatted as a JSON string, by using a custom data structure + * + * @param string $structure + * @param bool $assoc + * @param int $options + * @return string + */ + public function toJson($options = 0, $structure = 'shortArray', $assoc = true) + { + return json_encode( + $this->toOutput($structure, $assoc), + $options + ); } public function __toString() diff --git a/src/NotificationCollection.php b/src/NotificationCollection.php index b7f30c5..3e6d20c 100644 --- a/src/NotificationCollection.php +++ b/src/NotificationCollection.php @@ -9,7 +9,7 @@ * @author Andrei Pirjoleanu * @package Xqddd\Notifications */ -class NotificationCollection extends \ArrayObject +class NotificationCollection extends \ArrayObject implements Presentable { /** @@ -61,49 +61,99 @@ public function hasNotifications() } /** + * Get the notifications formatted as a string * - * @return array + * @return string */ public function toString() { - $collection = []; + $collectionString = ''; - foreach ($this->getArrayCopy() as $item) { - $collection[] = $item->toString(); + /** @var Notification $item */ + foreach ($this->getArrayCopy() as $label => $item) { + $collectionString .= $item->toString(); + $collectionString .= PHP_EOL; } - return $collection; + return $collectionString; } /** + * Get the notifications formatted as an array * + * @param bool $assoc * @return array */ - public function toArray() + public function toArray($assoc = true) { $collection = []; - foreach ($this->getArrayCopy() as $item) { - $collection[] = $item->toArray(); + /** @var Notification $item */ + foreach ($this->getArrayCopy() as $label => $item) { + $collection[$label] = $item->toArray($assoc); } return $collection; } /** + * Get the notifications formatted as a short array * - * @param boolean $fullStructure - * @return string + * @param bool $assoc + * @return array */ - public function toJson($fullStructure = true) + public function toShortArray($assoc = true) { $collection = []; - foreach ($this->getArrayCopy() as $item) { - $collection[] = json_decode($item->toJson($fullStructure)); + /** @var Notification $item */ + foreach ($this->getArrayCopy() as $label => $item) { + $collection[$label] = $item->toShortArray($assoc); } - return json_encode($collection); + return $collection; } + /** + * Get the notifications formatted as a custom data structure + * + * @param string $structure + * @param bool $assoc + * @return mixed + */ + public function toOutput($structure = 'shortArray', $assoc = true) + { + switch ($structure) { + case 'string': + $output = $this->toString(); + break; + case 'array': + $output = $this->toArray($assoc); + break; + case 'shortArray': + default: + $output = $this->toShortArray($assoc); + break; + } + return $output; + } + + /** + * @param int $options + * @param string $structure + * @param bool $assoc + * @return string + */ + public function toJson($options = 0, $structure = 'shortArray', $assoc = true) + { + return json_encode( + $this->toOutput($structure, $assoc), + $options + ); + } + + public function __toString() + { + return $this->toString(); + } } diff --git a/src/NotificationFactory.php b/src/NotificationFactory.php new file mode 100644 index 0000000..0a71fa3 --- /dev/null +++ b/src/NotificationFactory.php @@ -0,0 +1,209 @@ + + * @package Xqddd\Notifications + */ +class NotificationFactory +{ + + /** + * Build a notification + * + * @param string $labelValue + * @param string $messageValue + * @param string $typeValue + * @return Notification + */ + public function build($labelValue, $messageValue, $typeValue = 'notification') + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + $type = $this->buildType($typeValue); + + return new Notification($label, $message, $type); + } + + /** + * Build a debug notification + * + * @param $labelValue + * @param $messageValue + * @return Debug + */ + public function buildDebug($labelValue, $messageValue) + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + + return new Debug($label, $message); + } + + + /** + * Build an informative notification + * + * @param $labelValue + * @param $messageValue + * @return Info + */ + public function buildInfo($labelValue, $messageValue) + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + + return new Info($label, $message); + } + + /** + * Build a notice + * + * @param $labelValue + * @param $messageValue + * @return Notice + */ + public function buildNotice($labelValue, $messageValue) + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + + return new Notice($label, $message); + } + + /** + * Build a warning + * + * @param $labelValue + * @param $messageValue + * @return Warning + */ + public function buildWarning($labelValue, $messageValue) + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + + return new Warning($label, $message); + } + + /** + * Build an error + * + * @param $labelValue + * @param $messageValue + * @return Error + */ + public function buildError($labelValue, $messageValue) + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + + return new Error($label, $message); + } + + /** + * Build a critical + * + * @param $labelValue + * @param $messageValue + * @return Critical + */ + public function buildCritical($labelValue, $messageValue) + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + + return new Critical($label, $message); + } + + /** + * Build an alert + * + * @param $labelValue + * @param $messageValue + * @return Alert + */ + public function buildAlert($labelValue, $messageValue) + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + + return new Alert($label, $message); + } + + /** + * Build an emergency + * + * @param $labelValue + * @param $messageValue + * @return Emergency + */ + public function buildEmergency($labelValue, $messageValue) + { + $label = $this->buildLabel($labelValue); + $message = $this->buildMessage($messageValue); + + return new Emergency($label, $message); + } + + /** + * Build a notification label + * + * @param mixed $labelValue + * @return Label + */ + protected function buildLabel($labelValue) + { + try { + $label = new Label($labelValue); + } catch (InvalidNotificationLabelException $e) { + /* + * @todo handle this case + */ + } + return $label; + } + + /** + * Build a notification type + * + * @param mixed $typeValue + * @return Type + */ + protected function buildType($typeValue) + { + try { + $type = new Type($typeValue); + } catch (InvalidNotificationTypeException $e) { + /* + * @todo handle this case + */ + } + return $type; + } + + /** + * Build a notification message + * + * @param mixed $messageValue + * @return Message + */ + protected function buildMessage($messageValue) + { + try { + $message = new Message($messageValue); + } catch (InvalidNotificationMessageException $e) { + /* + * @todo handle this case + */ + } + return $message; + } + +} diff --git a/src/NotificationInterface.php b/src/NotificationInterface.php index 2bfe762..f2f7ab5 100644 --- a/src/NotificationInterface.php +++ b/src/NotificationInterface.php @@ -18,19 +18,16 @@ interface NotificationInterface public function getLabel(); /** - * Get the notification message + * Get the notification type * * @return string */ - public function getMessage(); + public function getType(); /** - * Get the notification message formatted to string + * Get the notification message * * @return string */ - public function toString(); - - public function __toString(); - + public function getMessage(); } diff --git a/src/Presentable.php b/src/Presentable.php new file mode 100644 index 0000000..44c4224 --- /dev/null +++ b/src/Presentable.php @@ -0,0 +1,56 @@ + + * @package Xqddd\Notifications + */ +interface Presentable +{ + + /** + * Get the string representation of the object + * + * @return string + */ + public function toString(); + + /** + * Get the array representation of the object + * + * @param bool $assoc Whether the array returned should be associative or not + * @return array + */ + public function toArray($assoc = true); + + /** + * Get the public representation of the object + * + * Depending of its implementation, this can be any type and it represents the + * object's public data available to any external usage + * + * Any complex representation logic that does not fit with the more common "toString" and "toArray" methods + * should be implemented here + * + * @param string $structure The type of structure to be returned (e.g.: the logic may be implemented with a switch) + * @param bool $assoc Whether the structure returned should be associative or not + * @return mixed + */ + public function toOutput($structure, $assoc = true); + + /** + * Get the JSON representation of the object + * + * This should usually be used as a JSON wrapper for the public representation of the object ("toOutput" method) + * + * @param int $options + * @param string $structure The type of structure to be returned (e.g.: the logic may be implemented with a switch) + * @param bool $assoc Whether the JSON structure returned should be associative or not + * @return string + */ + public function toJson($options = 0, $structure, $assoc = true); + + public function __toString(); +} diff --git a/src/PresentableTrait.php b/src/PresentableTrait.php new file mode 100644 index 0000000..0c185a7 --- /dev/null +++ b/src/PresentableTrait.php @@ -0,0 +1,59 @@ + + * @package Xqddd\Notifications + */ +trait PresentableTrait +{ + + public abstract function toString(); + + public abstract function toArray($assoc = true); + + /** + * Get the public representation of the object + * + * @param string $structure + * @param bool $assoc + * @return array|string + */ + public function toOutput($structure, $assoc = true) + { + switch ($structure) { + case 'string': + $output = $this->toString(); + break; + case 'array': + default: + $output = $this->toArray($assoc); + break; + } + return $output; + } + + /** + * Get the JSON representation of the object + * + * @param int $options + * @param string $structure + * @param bool $assoc + * @return string + */ + public function toJson($options = 0, $structure, $assoc = true) + { + return json_encode( + $this->toOutput($structure, $assoc), + $options + ); + } + + public function __toString() + { + return $this->toString(); + } + +} diff --git a/src/Type.php b/src/Type.php new file mode 100644 index 0000000..93860c7 --- /dev/null +++ b/src/Type.php @@ -0,0 +1,108 @@ + + * @package Xqddd\Notifications + */ +class Type implements Presentable +{ + use PresentableTrait; + + /** + * The type string value + * + * @var string + */ + protected $value; + + /** + * Type constructor. + * + * @param string $value + */ + public function __construct($value) + { + $this->validateValue($value); + $this->setValue($value); + } + + /** + * Check if a given value passes validations + * + * @param mixed $value + * @throws InvalidNotificationTypeException + */ + protected function validateValue($value) + { + if (!is_string($value)) { + throw new InvalidNotificationTypeException( + 'type', + 'string', + gettype($value) + ); + } + } + + /** + * Get the type string value + * + * @return string + */ + public function getValue() + { + return $this->value; + } + + /** + * Set the type string value + * + * @param string $value + */ + protected function setValue($value) + { + $this->value = $value; + } + + /** + * @param Type $type + * @return bool + */ + public function equals(Type $type) + { + return $this->value === $type->getValue(); + } + + /** + * Get the string representation of the type + * + * @return string + */ + public function toString() + { + return $this->getValue(); + } + + /** + * Get the array representation of the type + * + * @param bool $assoc + * @return array + */ + public function toArray($assoc = true) + { + return (true === $assoc) + ? [ + 'type' => $this->getValue() + ] + : [ + $this->getValue() + ]; + } +} diff --git a/src/TypeList.php b/src/TypeList.php new file mode 100644 index 0000000..3f7efa1 --- /dev/null +++ b/src/TypeList.php @@ -0,0 +1,20 @@ + + * @package Xqddd\Notifications + */ +class TypeList extends LogLevel +{ + +} diff --git a/src/Warning.php b/src/Warning.php new file mode 100644 index 0000000..091f779 --- /dev/null +++ b/src/Warning.php @@ -0,0 +1,25 @@ + + * @package Xqddd\Notifications + */ +class Warning extends Notification +{ + + /** + * Warning constructor. + * + * @param Label $label + * @param Message $message + */ + public function __construct(Label $label, Message $message) + { + $type = new Type(TypeList::WARNING); + + parent::__construct($label, $message, $type); + } +} diff --git a/tests/Alert/ConstructTest.php b/tests/Alert/ConstructTest.php new file mode 100644 index 0000000..4cbe32c --- /dev/null +++ b/tests/Alert/ConstructTest.php @@ -0,0 +1,101 @@ +getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Alert = new Alert($label, $messageValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Alert::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } + + public function nonStringValues() + { + /** + * An array of integers + * @var array + */ + $intReturn = [123, 456, 789]; + + $object1 = new \stdClass(); + $object1->test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + $intReturn, + $objectReturn, + $mixedReturn + ]; + } + +} diff --git a/tests/Critical/ConstructTest.php b/tests/Critical/ConstructTest.php new file mode 100644 index 0000000..e11581f --- /dev/null +++ b/tests/Critical/ConstructTest.php @@ -0,0 +1,101 @@ +getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Critical = new Critical($label, $messageValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Critical::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } + + public function nonStringValues() + { + /** + * An array of integers + * @var array + */ + $intReturn = [123, 456, 789]; + + $object1 = new \stdClass(); + $object1->test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + $intReturn, + $objectReturn, + $mixedReturn + ]; + } + +} diff --git a/tests/Debug/ConstructTest.php b/tests/Debug/ConstructTest.php new file mode 100644 index 0000000..df6bdd9 --- /dev/null +++ b/tests/Debug/ConstructTest.php @@ -0,0 +1,101 @@ +getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Debug = new Debug($label, $messageValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Debug::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } + + public function nonStringValues() + { + /** + * An array of integers + * @var array + */ + $intReturn = [123, 456, 789]; + + $object1 = new \stdClass(); + $object1->test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + $intReturn, + $objectReturn, + $mixedReturn + ]; + } + +} diff --git a/tests/Emergency/ConstructTest.php b/tests/Emergency/ConstructTest.php new file mode 100644 index 0000000..feba6b0 --- /dev/null +++ b/tests/Emergency/ConstructTest.php @@ -0,0 +1,101 @@ +getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Emergency = new Emergency($label, $messageValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Emergency::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } + + public function nonStringValues() + { + /** + * An array of integers + * @var array + */ + $intReturn = [123, 456, 789]; + + $object1 = new \stdClass(); + $object1->test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + $intReturn, + $objectReturn, + $mixedReturn + ]; + } + +} diff --git a/tests/Error/ConstructTest.php b/tests/Error/ConstructTest.php new file mode 100644 index 0000000..f038f90 --- /dev/null +++ b/tests/Error/ConstructTest.php @@ -0,0 +1,101 @@ +getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Error = new Error($label, $messageValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Error::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } + + public function nonStringValues() + { + /** + * An array of integers + * @var array + */ + $intReturn = [123, 456, 789]; + + $object1 = new \stdClass(); + $object1->test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + $intReturn, + $objectReturn, + $mixedReturn + ]; + } + +} diff --git a/tests/Info/ConstructTest.php b/tests/Info/ConstructTest.php new file mode 100644 index 0000000..f5bfd9f --- /dev/null +++ b/tests/Info/ConstructTest.php @@ -0,0 +1,101 @@ +getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Info = new Info($label, $messageValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Info::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } + + public function nonStringValues() + { + /** + * An array of integers + * @var array + */ + $intReturn = [123, 456, 789]; + + $object1 = new \stdClass(); + $object1->test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + $intReturn, + $objectReturn, + $mixedReturn + ]; + } + +} diff --git a/tests/Label/ConstructTest.php b/tests/Label/ConstructTest.php new file mode 100644 index 0000000..19d2d0d --- /dev/null +++ b/tests/Label/ConstructTest.php @@ -0,0 +1,75 @@ +test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + array_merge( + $intReturn, + $objectReturn, + $mixedReturn + ) + ]; + } + +} diff --git a/tests/Label/EqualsTest.php b/tests/Label/EqualsTest.php new file mode 100644 index 0000000..556b78c --- /dev/null +++ b/tests/Label/EqualsTest.php @@ -0,0 +1,60 @@ +equals($labelTwo) + ); + } + + /** + * When called with a different object with same class and different value will return false + */ + public function testWhenCalledWithADifferentObjectWithSameClassAndDifferentValueWillReturnFalse() + { + $labelValue = 'testing'; + $labelValueTwo = 'notTesting'; + $label = new Label($labelValue); + $labelTwo = new Label($labelValueTwo); + + static::assertFalse( + $label->equals($labelTwo) + ); + } + + /** + * When called with a different object with different class will throw an exception + */ + public function testWhenCalledWithADifferentObjectWithDifferentClassWillThrowAnException() + { + $labelValue = 'testing'; + $labelValueTwo = 'notTesting'; + $label = new Label($labelValue); + $labelTwo = new \stdClass(); + $labelTwo->value = $labelValueTwo; + + + try { + $label->equals($labelTwo); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 1 passed to Xqddd\Notifications\Label::equals() must be an instance of Xqddd\Notifications\Label,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } +} diff --git a/tests/Label/GetValueTest.php b/tests/Label/GetValueTest.php new file mode 100644 index 0000000..d94edb1 --- /dev/null +++ b/tests/Label/GetValueTest.php @@ -0,0 +1,22 @@ +getValue() + ); + } +} diff --git a/tests/Label/ToArrayTest.php b/tests/Label/ToArrayTest.php new file mode 100644 index 0000000..2910332 --- /dev/null +++ b/tests/Label/ToArrayTest.php @@ -0,0 +1,72 @@ +toArray() + ); + + return [ + $labelValue, + $label + ]; + } + + /** + * When called with true or without parameter will return the associative array representation + * + * @depends testWhenCalledWillReturnTheArrayRepresentation + * + * @param array $params + */ + public function testWhenCalledWithTrueOrWithoutParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Label $label */ + list($labelValue, $label) = $params; + + static::assertEquals( + [ + 'label' => $labelValue + ], + $label->toArray(true) + ); + static::assertEquals( + [ + 'label' => $labelValue + ], + $label->toArray() + ); + } + + /** + * When called with false will return the non associative array representation + * + * @depends testWhenCalledWillReturnTheArrayRepresentation + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Label $label */ + list($labelValue, $label) = $params; + + static::assertEquals( + [$labelValue], + $label->toArray(false) + ); + } + +} diff --git a/tests/Label/ToJsonTest.php b/tests/Label/ToJsonTest.php new file mode 100644 index 0000000..75a5e4a --- /dev/null +++ b/tests/Label/ToJsonTest.php @@ -0,0 +1,88 @@ +toJson($options, 'string') + ); + static::assertEquals( + json_encode($labelValue, $options), + $label->toJson($options, 'string') + ); + + return [ + $labelValue, + $label, + $options + ]; + } + + /** + * When called with options, 'array' and true or without the third parameter will return the associative array representation in a JSON string + * + * @depends testWhenCalledWithOptionsAndStringWillReturnTheStringRepresentationInAJSONString + * + * @param array $params + */ + public function testWhenCalledWithOptionsArrayAndTrueOrWithoutTheThirdParameterWillReturnTheAssociativeArrayRepresentationInAJSONString($params) + { + /** @var Label $label */ + list($labelValue, $label, $options) = $params; + + static::assertEquals( + json_encode( + [ + 'label' => $labelValue + ], + $options + ), + $label->toJson($options, 'array', true) + ); + static::assertEquals( + json_encode( + [ + 'label' => $labelValue + ], + $options + ), + $label->toJson($options, 'array') + ); + } + + /** + * When called with 'array' and false will return the non associative array representation + * + * @depends testWhenCalledWithOptionsAndStringWillReturnTheStringRepresentationInAJSONString + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Label $label */ + list($labelValue, $label, $options) = $params; + + static::assertEquals( + json_encode( + [$labelValue], + $options + ), + $label->toJson($options, 'array', false) + ); + } + +} diff --git a/tests/Label/ToOutputTest.php b/tests/Label/ToOutputTest.php new file mode 100644 index 0000000..248a2cc --- /dev/null +++ b/tests/Label/ToOutputTest.php @@ -0,0 +1,120 @@ +toOutput('string') + ); + static::assertEquals( + $labelValue, + $label->toOutput('string') + ); + + return [ + $labelValue, + $label + ]; + } + + /** + * When called with 'array' and true or without the second parameter will return the associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithArrayAndTrueOrWithoutTheSecondParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Label $label */ + list($labelValue, $label) = $params; + + static::assertEquals( + [ + 'label' => $labelValue + ], + $label->toOutput('array', true) + ); + static::assertEquals( + [ + 'label' => $labelValue + ], + $label->toOutput('array') + ); + } + + /** + * When called with 'array' and false will return the non associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Label $label */ + list($labelValue, $label) = $params; + + static::assertEquals( + [$labelValue], + $label->toOutput('array', false) + ); + } + + + /** + * When called with random string and true or without the second parameter will return the associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithRandomStringAndTrueOrWithoutTheSecondParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Label $label */ + list($labelValue, $label) = $params; + + static::assertEquals( + [ + 'label' => $labelValue + ], + $label->toOutput('r4nd0m$tr1ng', true) + ); + static::assertEquals( + [ + 'label' => $labelValue + ], + $label->toOutput('r4nd0m$tr1ng') + ); + } + + /** + * When called with random string and false will return the non associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithRandomStringAndFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Label $label */ + list($labelValue, $label) = $params; + + static::assertEquals( + [$labelValue], + $label->toOutput('r4nd0m$tr1ng2', false) + ); + } +} diff --git a/tests/Label/ToStringTest.php b/tests/Label/ToStringTest.php new file mode 100644 index 0000000..7cd21e4 --- /dev/null +++ b/tests/Label/ToStringTest.php @@ -0,0 +1,26 @@ +toString() + ); + static::assertEquals( + $labelValue, + $label->toString() + ); + } +} diff --git a/tests/Label/__ToStringTest.php b/tests/Label/__ToStringTest.php new file mode 100644 index 0000000..b94e31a --- /dev/null +++ b/tests/Label/__ToStringTest.php @@ -0,0 +1,22 @@ +test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + array_merge( + $intReturn, + $objectReturn, + $mixedReturn + ) + ]; + } + +} diff --git a/tests/Message/EqualsTest.php b/tests/Message/EqualsTest.php new file mode 100644 index 0000000..94b9a1d --- /dev/null +++ b/tests/Message/EqualsTest.php @@ -0,0 +1,60 @@ +equals($messageTwo) + ); + } + + /** + * When called with a different object with same class and different value will return false + */ + public function testWhenCalledWithADifferentObjectWithSameClassAndDifferentValueWillReturnFalse() + { + $messageValue = 'testing'; + $messageValueTwo = 'notTesting'; + $message = new Message($messageValue); + $messageTwo = new Message($messageValueTwo); + + static::assertFalse( + $message->equals($messageTwo) + ); + } + + /** + * When called with a different object with different class will throw an exception + */ + public function testWhenCalledWithADifferentObjectWithDifferentClassWillThrowAnException() + { + $messageValue = 'testing'; + $messageValueTwo = 'notTesting'; + $message = new Message($messageValue); + $messageTwo = new \stdClass(); + $messageTwo->value = $messageValueTwo; + + + try { + $message->equals($messageTwo); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 1 passed to Xqddd\Notifications\Message::equals() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } +} diff --git a/tests/Message/GetValueTest.php b/tests/Message/GetValueTest.php new file mode 100644 index 0000000..34bf48f --- /dev/null +++ b/tests/Message/GetValueTest.php @@ -0,0 +1,22 @@ +getValue() + ); + } +} diff --git a/tests/Message/ToArrayTest.php b/tests/Message/ToArrayTest.php new file mode 100644 index 0000000..09c3f78 --- /dev/null +++ b/tests/Message/ToArrayTest.php @@ -0,0 +1,72 @@ +toArray() + ); + + return [ + $messageValue, + $message + ]; + } + + /** + * When called with true or without parameter will return the associative array representation + * + * @depends testWhenCalledWillReturnTheArrayRepresentation + * + * @param array $params + */ + public function testWhenCalledWithTrueOrWithoutParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Message $message */ + list($messageValue, $message) = $params; + + static::assertEquals( + [ + 'message' => $messageValue + ], + $message->toArray(true) + ); + static::assertEquals( + [ + 'message' => $messageValue + ], + $message->toArray() + ); + } + + /** + * When called with false will return the non associative array representation + * + * @depends testWhenCalledWillReturnTheArrayRepresentation + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Message $message */ + list($messageValue, $message) = $params; + + static::assertEquals( + [$messageValue], + $message->toArray(false) + ); + } + +} diff --git a/tests/Message/ToJsonTest.php b/tests/Message/ToJsonTest.php new file mode 100644 index 0000000..d96fb6f --- /dev/null +++ b/tests/Message/ToJsonTest.php @@ -0,0 +1,88 @@ +toJson($options, 'string') + ); + static::assertEquals( + json_encode($messageValue, $options), + $message->toJson($options, 'string') + ); + + return [ + $messageValue, + $message, + $options + ]; + } + + /** + * When called with options, 'array' and true or without the third parameter will return the associative array representation in a JSON string + * + * @depends testWhenCalledWithOptionsAndStringWillReturnTheStringRepresentationInAJSONString + * + * @param array $params + */ + public function testWhenCalledWithOptionsArrayAndTrueOrWithoutTheThirdParameterWillReturnTheAssociativeArrayRepresentationInAJSONString($params) + { + /** @var Message $message */ + list($messageValue, $message, $options) = $params; + + static::assertEquals( + json_encode( + [ + 'message' => $messageValue + ], + $options + ), + $message->toJson($options, 'array', true) + ); + static::assertEquals( + json_encode( + [ + 'message' => $messageValue + ], + $options + ), + $message->toJson($options, 'array') + ); + } + + /** + * When called with 'array' and false will return the non associative array representation + * + * @depends testWhenCalledWithOptionsAndStringWillReturnTheStringRepresentationInAJSONString + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Message $message */ + list($messageValue, $message, $options) = $params; + + static::assertEquals( + json_encode( + [$messageValue], + $options + ), + $message->toJson($options, 'array', false) + ); + } + +} diff --git a/tests/Message/ToOutputTest.php b/tests/Message/ToOutputTest.php new file mode 100644 index 0000000..2db1227 --- /dev/null +++ b/tests/Message/ToOutputTest.php @@ -0,0 +1,120 @@ +toOutput('string') + ); + static::assertEquals( + $messageValue, + $message->toOutput('string') + ); + + return [ + $messageValue, + $message + ]; + } + + /** + * When called with 'array' and true or without the second parameter will return the associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithArrayAndTrueOrWithoutTheSecondParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Message $message */ + list($messageValue, $message) = $params; + + static::assertEquals( + [ + 'message' => $messageValue + ], + $message->toOutput('array', true) + ); + static::assertEquals( + [ + 'message' => $messageValue + ], + $message->toOutput('array') + ); + } + + /** + * When called with 'array' and false will return the non associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Message $message */ + list($messageValue, $message) = $params; + + static::assertEquals( + [$messageValue], + $message->toOutput('array', false) + ); + } + + + /** + * When called with random string and true or without the second parameter will return the associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithRandomStringAndTrueOrWithoutTheSecondParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Message $message */ + list($messageValue, $message) = $params; + + static::assertEquals( + [ + 'message' => $messageValue + ], + $message->toOutput('r4nd0m$tr1ng', true) + ); + static::assertEquals( + [ + 'message' => $messageValue + ], + $message->toOutput('r4nd0m$tr1ng') + ); + } + + /** + * When called with random string and false will return the non associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithRandomStringAndFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Message $message */ + list($messageValue, $message) = $params; + + static::assertEquals( + [$messageValue], + $message->toOutput('r4nd0m$tr1ng2', false) + ); + } +} diff --git a/tests/Message/ToStringTest.php b/tests/Message/ToStringTest.php new file mode 100644 index 0000000..4b4f760 --- /dev/null +++ b/tests/Message/ToStringTest.php @@ -0,0 +1,26 @@ +toString() + ); + static::assertEquals( + $messageValue, + $message->toString() + ); + } +} diff --git a/tests/Message/__ToStringTest.php b/tests/Message/__ToStringTest.php new file mode 100644 index 0000000..e242a44 --- /dev/null +++ b/tests/Message/__ToStringTest.php @@ -0,0 +1,22 @@ +getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Notice = new Notice($label, $messageValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Notice::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } + + public function nonStringValues() + { + /** + * An array of integers + * @var array + */ + $intReturn = [123, 456, 789]; + + $object1 = new \stdClass(); + $object1->test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + $intReturn, + $objectReturn, + $mixedReturn + ]; + } + +} diff --git a/tests/Notification/ConstructTest.php b/tests/Notification/ConstructTest.php index 6ed44f9..3092fb5 100644 --- a/tests/Notification/ConstructTest.php +++ b/tests/Notification/ConstructTest.php @@ -1,82 +1,113 @@ getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Notification = new Notification($label, $messageValue, $type); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Notification::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Notification = new Notification($label, $message, $typeValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 3 passed to Xqddd\Notifications\Notification::__construct() must be an instance of Xqddd\Notifications\Type,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } public function nonStringValues() { /** - * An array of ints + * An array of integers * @var array */ - $intReturn = [123, 456]; + $intReturn = [123, 456, 789]; $object1 = new \stdClass(); $object1->test = 123; $object2 = new \stdClass(); $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + /** * An array of objects * @var array */ - $objectReturn = [$object1, $object2]; + $objectReturn = [$object1, $object2, $object3]; /** * An array of mixed values * @var array */ - $mixedReturn = [123, $object2]; + $mixedReturn = [123, $object2, $intReturn]; return [ $intReturn, diff --git a/tests/Type/ConstructTest.php b/tests/Type/ConstructTest.php new file mode 100644 index 0000000..f362535 --- /dev/null +++ b/tests/Type/ConstructTest.php @@ -0,0 +1,75 @@ +test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + array_merge( + $intReturn, + $objectReturn, + $mixedReturn + ) + ]; + } + +} diff --git a/tests/Type/EqualsTest.php b/tests/Type/EqualsTest.php new file mode 100644 index 0000000..6b8ce68 --- /dev/null +++ b/tests/Type/EqualsTest.php @@ -0,0 +1,60 @@ +equals($typeTwo) + ); + } + + /** + * When called with a different object with same class and different value will return false + */ + public function testWhenCalledWithADifferentObjectWithSameClassAndDifferentValueWillReturnFalse() + { + $typeValue = 'testing'; + $typeValueTwo = 'notTesting'; + $type = new Type($typeValue); + $typeTwo = new Type($typeValueTwo); + + static::assertFalse( + $type->equals($typeTwo) + ); + } + + /** + * When called with a different object with different class will throw an exception + */ + public function testWhenCalledWithADifferentObjectWithDifferentClassWillThrowAnException() + { + $typeValue = 'testing'; + $typeValueTwo = 'notTesting'; + $type = new Type($typeValue); + $typeTwo = new \stdClass(); + $typeTwo->value = $typeValueTwo; + + + try { + $type->equals($typeTwo); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 1 passed to Xqddd\Notifications\Type::equals() must be an instance of Xqddd\Notifications\Type,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } +} diff --git a/tests/Type/GetValueTest.php b/tests/Type/GetValueTest.php new file mode 100644 index 0000000..cfd5da1 --- /dev/null +++ b/tests/Type/GetValueTest.php @@ -0,0 +1,22 @@ +getValue() + ); + } +} diff --git a/tests/Type/ToArrayTest.php b/tests/Type/ToArrayTest.php new file mode 100644 index 0000000..d5a50d9 --- /dev/null +++ b/tests/Type/ToArrayTest.php @@ -0,0 +1,72 @@ +toArray() + ); + + return [ + $typeValue, + $type + ]; + } + + /** + * When called with true or without parameter will return the associative array representation + * + * @depends testWhenCalledWillReturnTheArrayRepresentation + * + * @param array $params + */ + public function testWhenCalledWithTrueOrWithoutParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Type $type */ + list($typeValue, $type) = $params; + + static::assertEquals( + [ + 'type' => $typeValue + ], + $type->toArray(true) + ); + static::assertEquals( + [ + 'type' => $typeValue + ], + $type->toArray() + ); + } + + /** + * When called with false will return the non associative array representation + * + * @depends testWhenCalledWillReturnTheArrayRepresentation + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Type $type */ + list($typeValue, $type) = $params; + + static::assertEquals( + [$typeValue], + $type->toArray(false) + ); + } + +} diff --git a/tests/Type/ToJsonTest.php b/tests/Type/ToJsonTest.php new file mode 100644 index 0000000..b5f69c0 --- /dev/null +++ b/tests/Type/ToJsonTest.php @@ -0,0 +1,88 @@ +toJson($options, 'string') + ); + static::assertEquals( + json_encode($typeValue, $options), + $type->toJson($options, 'string') + ); + + return [ + $typeValue, + $type, + $options + ]; + } + + /** + * When called with options, 'array' and true or without the third parameter will return the associative array representation in a JSON string + * + * @depends testWhenCalledWithOptionsAndStringWillReturnTheStringRepresentationInAJSONString + * + * @param array $params + */ + public function testWhenCalledWithOptionsArrayAndTrueOrWithoutTheThirdParameterWillReturnTheAssociativeArrayRepresentationInAJSONString($params) + { + /** @var Type $type */ + list($typeValue, $type, $options) = $params; + + static::assertEquals( + json_encode( + [ + 'type' => $typeValue + ], + $options + ), + $type->toJson($options, 'array', true) + ); + static::assertEquals( + json_encode( + [ + 'type' => $typeValue + ], + $options + ), + $type->toJson($options, 'array') + ); + } + + /** + * When called with 'array' and false will return the non associative array representation + * + * @depends testWhenCalledWithOptionsAndStringWillReturnTheStringRepresentationInAJSONString + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Type $type */ + list($typeValue, $type, $options) = $params; + + static::assertEquals( + json_encode( + [$typeValue], + $options + ), + $type->toJson($options, 'array', false) + ); + } + +} diff --git a/tests/Type/ToOutputTest.php b/tests/Type/ToOutputTest.php new file mode 100644 index 0000000..bab751a --- /dev/null +++ b/tests/Type/ToOutputTest.php @@ -0,0 +1,120 @@ +toOutput('string') + ); + static::assertEquals( + $typeValue, + $type->toOutput('string') + ); + + return [ + $typeValue, + $type + ]; + } + + /** + * When called with 'array' and true or without the second parameter will return the associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithArrayAndTrueOrWithoutTheSecondParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Type $type */ + list($typeValue, $type) = $params; + + static::assertEquals( + [ + 'type' => $typeValue + ], + $type->toOutput('array', true) + ); + static::assertEquals( + [ + 'type' => $typeValue + ], + $type->toOutput('array') + ); + } + + /** + * When called with 'array' and false will return the non associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Type $type */ + list($typeValue, $type) = $params; + + static::assertEquals( + [$typeValue], + $type->toOutput('array', false) + ); + } + + + /** + * When called with random string and true or without the second parameter will return the associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithRandomStringAndTrueOrWithoutTheSecondParameterWillReturnTheAssociativeArrayRepresentation($params) + { + /** @var Type $type */ + list($typeValue, $type) = $params; + + static::assertEquals( + [ + 'type' => $typeValue + ], + $type->toOutput('r4nd0m$tr1ng', true) + ); + static::assertEquals( + [ + 'type' => $typeValue + ], + $type->toOutput('r4nd0m$tr1ng') + ); + } + + /** + * When called with random string and false will return the non associative array representation + * + * @depends testWhenCalledWithStringWillReturnTheStringRepresentation + * + * @param array $params + */ + public function testWhenCalledWithRandomStringAndFalseWillReturnTheNonAssociativeArrayRepresentation($params) + { + /** @var Type $type */ + list($typeValue, $type) = $params; + + static::assertEquals( + [$typeValue], + $type->toOutput('r4nd0m$tr1ng2', false) + ); + } +} diff --git a/tests/Type/ToStringTest.php b/tests/Type/ToStringTest.php new file mode 100644 index 0000000..8646d3c --- /dev/null +++ b/tests/Type/ToStringTest.php @@ -0,0 +1,26 @@ +toString() + ); + static::assertEquals( + $typeValue, + $type->toString() + ); + } +} diff --git a/tests/Type/__ToStringTest.php b/tests/Type/__ToStringTest.php new file mode 100644 index 0000000..f6444d1 --- /dev/null +++ b/tests/Type/__ToStringTest.php @@ -0,0 +1,22 @@ +getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + + try { + $Warning = new Warning($label, $messageValue); + } catch (\PHPUnit_Framework_Error $e) { + static::assertStringStartsWith( + 'Argument 2 passed to Xqddd\Notifications\Warning::__construct() must be an instance of Xqddd\Notifications\Message,', + $e->getMessage() + ); + static::assertEquals(4096, $e->getCode()); + } + } + + public function nonStringValues() + { + /** + * An array of integers + * @var array + */ + $intReturn = [123, 456, 789]; + + $object1 = new \stdClass(); + $object1->test = 123; + $object2 = new \stdClass(); + $object2->wow = 456; + $object3 = new \stdClass(); + $object3->great = 789; + + /** + * An array of objects + * @var array + */ + $objectReturn = [$object1, $object2, $object3]; + + /** + * An array of mixed values + * @var array + */ + $mixedReturn = [123, $object2, $intReturn]; + + return [ + $intReturn, + $objectReturn, + $mixedReturn + ]; + } + +}