diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b84886..d8ae66a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ +### [0.6.0] - 2016-10-02 + + * added basic meta model + ### [0.5.3] - 2016-10-02 * fixed subscription monitor integration test diff --git a/composer.json b/composer.json index 967fd92..d919508 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,10 @@ "codacy/coverage": "dev-master" }, "autoload": { - "psr-0": { - "Tidal\\WampWatch": "src/" + "psr-4": { + "Tidal\\WampWatch\\Test\\Unit\\": "tests/unit", + "Tidal\\WampWatch\\Test\\Integration\\": "tests/integration", + "Tidal\\WampWatch\\": "src/Tidal/WampWatch" } }, "scripts": { diff --git a/composer.lock b/composer.lock index 8a3f3d0..8201259 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "39b42b2be9636aa5ef36eb5ca45c9099", + "hash": "5d633d50f9f98709f14e9d8d708a93ce", "content-hash": "38f9d91c99d6746de41dd08108bf3389", "packages": [ { diff --git a/src/Tidal/WampWatch/Behavior/Event/ObservableTrait.php b/src/Tidal/WampWatch/Behavior/Event/ObservableTrait.php new file mode 100644 index 0000000..02bec7c --- /dev/null +++ b/src/Tidal/WampWatch/Behavior/Event/ObservableTrait.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Behavior\Event; + +trait ObservableTrait +{ +} diff --git a/src/Tidal/WampWatch/Contract/Async/PromiseInterface.php b/src/Tidal/WampWatch/Contract/Async/PromiseInterface.php new file mode 100644 index 0000000..5c45237 --- /dev/null +++ b/src/Tidal/WampWatch/Contract/Async/PromiseInterface.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Contract\Async; + +interface PromiseInterface +{ + /** + * @return PromiseInterface + */ + public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null); + + public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null); + + /** + * @return PromiseInterface + */ + public function otherwise(callable $onRejected); + + /** + * @return PromiseInterface + */ + public function always(callable $onFulfilledOrRejected); + + /** + * @return PromiseInterface + */ + public function progress(callable $onProgress); +} diff --git a/src/Tidal/WampWatch/Contract/Event/EventEmitterInterface.php b/src/Tidal/WampWatch/Contract/Event/EventEmitterInterface.php new file mode 100644 index 0000000..6124d1e --- /dev/null +++ b/src/Tidal/WampWatch/Contract/Event/EventEmitterInterface.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Contract\Event; + +interface EventEmitterInterface +{ + /** + * Registers a subscriber to be notified on given event. + * + * @param string $eventName the event to subscribe to + * @param callable $callback the callback to notify the subscriber + * + * @return mixed + */ + public function on($eventName, callable $callback); +} diff --git a/src/Tidal/WampWatch/Model/Behavior/Property/HasCollectionsTrait.php b/src/Tidal/WampWatch/Model/Behavior/Property/HasCollectionsTrait.php new file mode 100644 index 0000000..76e6f15 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Behavior/Property/HasCollectionsTrait.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Behavior\Property; + +use InvalidArgumentException; +use Tidal\WampWatch\Model\Contract\Property\CollectionInterface; + +trait HasCollectionsTrait +{ + private function initCollection($name, CollectionInterface $collection) + { + if (property_exists($this, $name)) { + $this->{$name} = $collection; + } + } + + private function appendTo($name, $value) + { + $this->getCollection($name)->append($value); + } + + /** + * @param string $name the name of the collection + * + * @throws InvalidArgumentException when the collection has not been initialized before + * + * @return CollectionInterface the collection object + */ + private function getCollection($name) + { + if (!$this->hasCollection($name)) { + throw new InvalidArgumentException("No Collection with name '$name' registered."); + } + + return $this->{$name}; + } + + private function hasCollection($name) + { + return property_exists($this, $name) && is_a($this->{$name}, CollectionInterface::class); + } +} diff --git a/src/Tidal/WampWatch/Model/Connection.php b/src/Tidal/WampWatch/Model/Connection.php new file mode 100644 index 0000000..dc287ca --- /dev/null +++ b/src/Tidal/WampWatch/Model/Connection.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model; + +use Tidal\WampWatch\Model\Property\Object\HasRouterTrait; +use Tidal\WampWatch\Model\Property\Object\HasRealmTrait; +use Tidal\WampWatch\Model\Property\Object\HasSessionTrait; + +class Connection implements Contract\ConnectionInterface +{ + use HasRouterTrait; + use HasRealmTrait; + use HasSessionTrait; + + public function __construct(Contract\RouterInterface $router, Contract\RealmInterface $realm, Contract\SessionInterface $session = null) + { + $this->setRouter($router); + $this->setRealm($realm); + if ($session !== null) { + $this->confirm($session); + } + } + + public function confirm(Contract\SessionInterface $session) + { + $this->setSession($session); + } +} diff --git a/src/Tidal/WampWatch/Model/Contract/ConnectionInterface.php b/src/Tidal/WampWatch/Model/Contract/ConnectionInterface.php new file mode 100644 index 0000000..854b9bc --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/ConnectionInterface.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +interface ConnectionInterface +{ + /** + * @return RouterInterface + */ + public function getRouter(); + + /** + * @return RealmInterface + */ + public function getRealm(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/ProcedureInterface.php b/src/Tidal/WampWatch/Model/Contract/ProcedureInterface.php new file mode 100644 index 0000000..66e3c1c --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/ProcedureInterface.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +use Tidal\WampWatch\Model\Contract\Property\Scalar\HasUriInterface; + +interface ProcedureInterface extends HasUriInterface +{ +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/Collection/HasProceduresInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/Collection/HasProceduresInterface.php new file mode 100644 index 0000000..584b618 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/Collection/HasProceduresInterface.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property\Collection; + +use Tidal\WampWatch\Model\Contract; + +interface HasProceduresInterface +{ + public function addProcedure(Contract\ProcedureInterface $procedure); + + /** + * @param string $uri + * + * @return mixed + */ + public function hasProcedure($uri); + + /** + * @param string $uri + * + * @return Contract\ProcedureInterface + */ + public function getProcedure($uri); + + /** + * @return \Generator + */ + public function listProcedures(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/Collection/HasTopicsInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/Collection/HasTopicsInterface.php new file mode 100644 index 0000000..10d17c1 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/Collection/HasTopicsInterface.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property\Collection; + +use Tidal\WampWatch\Model\Contract; + +interface HasTopicsInterface +{ + public function addTopic(Contract\TopicInterface $topic); + + public function hasTopic($uri); + + /** + * @param string $uri + * + * @return Contract\TopicInterface + */ + public function getTopic($uri); + + /** + * @return \Generator + */ + public function listTopics(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/CollectionInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/CollectionInterface.php new file mode 100644 index 0000000..e38f767 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/CollectionInterface.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property; + +use IteratorAggregate; +use ArrayAccess; +use Serializable; +use Countable; + +interface CollectionInterface extends IteratorAggregate, ArrayAccess, Serializable, Countable +{ + /** + * Sets a property type constrain for collection items. + * + * @param $type + */ + public function setItemType($type); + + /** + * Registers a callback function to validate new Collection entries. + * The callback function must return a boolean value. + * + * + * @param callable $callback + */ + public function setValidationCallback(callable $callback); + + /** + * If the collection has an item with the given key. + * + * @param string $key the name of the key + * + * @return bool + */ + public function has($key); + + /** + * set an item with the given key. + * + * @param string $key the name of the key + * @param mixed $value the item to add + * + * @return mixed + */ + public function set($key, $value); + + /** + * Retrieves an item with the given key. + * + * @param string $key the name of the key + * + * @return mixed + */ + public function get($key); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/Object/HasProcedureInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/Object/HasProcedureInterface.php new file mode 100644 index 0000000..6a33de5 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/Object/HasProcedureInterface.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property\Object; + +use Tidal\WampWatch\Model\Contract; + +interface HasProcedureInterface +{ + /** + * @return Contract\ProcedureInterface + */ + public function getProcedure(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/Object/HasSessionInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/Object/HasSessionInterface.php new file mode 100644 index 0000000..81052f9 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/Object/HasSessionInterface.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property\Object; + +use Tidal\WampWatch\Model\Contract; + +interface HasSessionInterface +{ + /** + * @return Contract\SessionInterface + */ + public function getSession(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/Object/HasTopicInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/Object/HasTopicInterface.php new file mode 100644 index 0000000..3a25351 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/Object/HasTopicInterface.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property\Object; + +use Tidal\WampWatch\Model\Contract; + +interface HasTopicInterface +{ + /** + * @return Contract\TopicInterface + */ + public function getTopic(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/ObjectCollectionInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/ObjectCollectionInterface.php new file mode 100644 index 0000000..d5dcf24 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/ObjectCollectionInterface.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property; + +interface ObjectCollectionInterface extends CollectionInterface +{ + /** + * Set a constrain on a class or interface name the collection's items must be an instance of. + * Paramater 1 $name expects a fully qualified class name. + * + * @param string $cls A Fully qualified class name + */ + public function setObjectConstrain($cls); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasIdInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasIdInterface.php new file mode 100644 index 0000000..a08ac49 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasIdInterface.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property\Scalar; + +interface HasIdInterface +{ + public function getId(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasNameInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasNameInterface.php new file mode 100644 index 0000000..e0998e5 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasNameInterface.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +/** + * Created by PhpStorm. + * User: Timo + * Date: 25.07.2016 + * Time: 22:34. + */ +namespace Tidal\WampWatch\Model\Contract\Property\Scalar; + +interface HasNameInterface +{ + /** + * @return string + */ + public function getName(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasUriInterface.php b/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasUriInterface.php new file mode 100644 index 0000000..dc83c13 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/Property/Scalar/HasUriInterface.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract\Property\Scalar; + +interface HasUriInterface +{ + /** + * @return string + */ + public function getUri(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/RealmInterface.php b/src/Tidal/WampWatch/Model/Contract/RealmInterface.php new file mode 100644 index 0000000..ee576af --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/RealmInterface.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +use Tidal\WampWatch\Model\Contract\Property\Scalar\HasNameInterface; +use Tidal\WampWatch\Model\Contract\Property\Collection\HasTopicsInterface; + +interface RealmInterface extends HasNameInterface, HasTopicsInterface +{ + public function getProcedure($uri); +} diff --git a/src/Tidal/WampWatch/Model/Contract/RegistrationInterface.php b/src/Tidal/WampWatch/Model/Contract/RegistrationInterface.php new file mode 100644 index 0000000..0cd74bd --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/RegistrationInterface.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +use Tidal\WampWatch\Model\Contract\Property\Scalar\HasIdInterface; +use Tidal\WampWatch\Model\Contract\Property\Object\HasSessionInterface; +use Tidal\WampWatch\Model\Contract\Property\Object\HasProcedureInterface; + +interface RegistrationInterface extends + HasIdInterface, + HasSessionInterface, + HasProcedureInterface +{ +} diff --git a/src/Tidal/WampWatch/Model/Contract/RemoteActionInterface.php b/src/Tidal/WampWatch/Model/Contract/RemoteActionInterface.php new file mode 100644 index 0000000..8bb5da9 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/RemoteActionInterface.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +interface RemoteActionInterface +{ + public function getUriString(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/RouterInterface.php b/src/Tidal/WampWatch/Model/Contract/RouterInterface.php new file mode 100644 index 0000000..e0b4ccb --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/RouterInterface.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +use Tidal\WampWatch\Model\Contract; + +interface RouterInterface +{ + /** + * @return string; + */ + public function getUri(); + + /** + * @param RealmInterface $realm + */ + public function addRealm(Contract\RealmInterface $realm); + + /** + * @return \Generator + */ + public function listRealms(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/SessionInterface.php b/src/Tidal/WampWatch/Model/Contract/SessionInterface.php new file mode 100644 index 0000000..6039538 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/SessionInterface.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +interface SessionInterface +{ + /** + * @return int + */ + public function getId(); +} diff --git a/src/Tidal/WampWatch/Model/Contract/SubscriptionInterface.php b/src/Tidal/WampWatch/Model/Contract/SubscriptionInterface.php new file mode 100644 index 0000000..4b44991 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/SubscriptionInterface.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +use Tidal\WampWatch\Model\Contract\Property\Scalar\HasIdInterface; +use Tidal\WampWatch\Model\Contract\Property\Object\HasSessionInterface; +use Tidal\WampWatch\Model\Contract\Property\Object\HasTopicInterface; + +interface SubscriptionInterface extends + HasIdInterface, + HasSessionInterface, + HasTopicInterface +{ +} diff --git a/src/Tidal/WampWatch/Model/Contract/TopicInterface.php b/src/Tidal/WampWatch/Model/Contract/TopicInterface.php new file mode 100644 index 0000000..f33176a --- /dev/null +++ b/src/Tidal/WampWatch/Model/Contract/TopicInterface.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Contract; + +use Tidal\WampWatch\Model\Contract\Property\Scalar\HasUriInterface; + +interface TopicInterface extends HasUriInterface +{ +} diff --git a/src/Tidal/WampWatch/Model/Factory/ConnectionFactory.php b/src/Tidal/WampWatch/Model/Factory/ConnectionFactory.php new file mode 100644 index 0000000..f9c2f71 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Factory/ConnectionFactory.php @@ -0,0 +1,76 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Factory; + +use Guzzle\Common\Exception\RuntimeException; +use Tidal\WampWatch\Model\Contract\RouterInterface; +use Tidal\WampWatch\Model\Contract\RealmInterface; +use Tidal\WampWatch\Model\Contract\SessionInterface; +use Tidal\WampWatch\Model\Connection; + +class ConnectionFactory +{ + private $router; + + private $realm; + + private $session; + + private function __construct(RouterInterface $router, RealmInterface $realm = null, SessionInterface $session = null) + { + $this->router = $router; + $this->realm = $realm; + $this->session = $session; + } + + /** + * @param RouterInterface $router + * + * @return ConnectionFactory + */ + public static function get(RouterInterface $router) + { + return new self($router); + } + + /** + * @param RealmInterface $realm + * + * @return ConnectionFactory + */ + public function select(RealmInterface $realm) + { + return new self($this->router, $realm); + } + + /** + * @param SessionInterface $session + * + * @return ConnectionFactory + */ + public function establish(SessionInterface $session) + { + return new self($this->router, $this->realm, $session); + } + + /** + * @return Connection + */ + public function create() + { + if (!isset($this->realm)) { + throw new RuntimeException('No realm set.'); + } + + return new Connection($this->router, $this->realm, $this->session); + } +} diff --git a/src/Tidal/WampWatch/Model/Message.php b/src/Tidal/WampWatch/Model/Message.php new file mode 100644 index 0000000..4b3c652 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Message.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model; + +class Message +{ +} diff --git a/src/Tidal/WampWatch/Model/Procedure.php b/src/Tidal/WampWatch/Model/Procedure.php new file mode 100644 index 0000000..ee0dc72 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Procedure.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model; + +use Tidal\WampWatch\Model\Property\Scalar\HasUriTrait; + +class Procedure implements Contract\ProcedureInterface +{ + use HasUriTrait; + + public function __construct($uri) + { + $this->setUri($uri); + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Collection.php b/src/Tidal/WampWatch/Model/Property/Collection.php new file mode 100644 index 0000000..f0f87fe --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Collection.php @@ -0,0 +1,106 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property; + +use ArrayObject; +use InvalidArgumentException; +use Generator; +use Tidal\WampWatch\Model\Contract\Property\CollectionInterface; + +class Collection extends ArrayObject implements CollectionInterface +{ + private $itemType; + + private $validationCallback; + + public function append($value) + { + $this->validate($value); + + parent::append($value); + } + + private function validate($item) + { + $this->validateItemType($item); + $this->validateCallback($item); + } + + private function validateItemType($item) + { + if (isset($this->itemType) && $type = gettype($item) !== $this->itemType) { + throw new InvalidArgumentException("Expected item of type '{$this->itemType}'. -> '$type' given."); + } + } + + private function validateCallback($item) + { + if (!isset($this->validationCallback)) { + return; + } + + $callback = $this->validationCallback; + + if (!$callback($item)) { + throw new InvalidArgumentException('Item failed validation.'); + } + } + + public function setItemType($type) + { + $this->itemType = (string) $type; + } + + public function has($key) + { + return $this->offsetExists($key); + } + + public function set($key, $value) + { + $this->offsetSet($key, $value); + } + + public function offsetSet($index, $newValue) + { + $this->validate($newValue); + + parent::offsetSet($index, $newValue); + } + + public function get($key) + { + return $this->offsetGet($key); + } + + /** + * @return Generator + */ + public function getGenerator() + { + foreach ($this as $key => $value) { + yield $key => $value; + } + } + + /** + * Registers a callback function to validate new Collection entries. + * The callback function must return a boolean value. + * When the callback returns false the new item will not be added to the collection. + * + * @param callable $callback + */ + public function setValidationCallback(callable $callback) + { + $this->validationCallback = $callback; + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Collection/HasProceduresTrait.php b/src/Tidal/WampWatch/Model/Property/Collection/HasProceduresTrait.php new file mode 100644 index 0000000..0e4a876 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Collection/HasProceduresTrait.php @@ -0,0 +1,87 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Collection; + +use Tidal\WampWatch\Model\Contract; +use Tidal\WampWatch\Model\Contract\Property\ObjectCollectionInterface; + +/** + * Class HasProceduresTrait. + * + * Important! Classes using this trait have to also use trait + * Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait + * for this trait to work; + */ +trait HasProceduresTrait +{ + protected static $proceduresPropertyName = 'procedures'; + + /** + * @var ObjectCollectionInterface + */ + private $procedures; + + public function addProcedure(Contract\ProcedureInterface $procedure) + { + $this->getProcedures()->set($procedure->getUri(), $procedure); + } + + /** + * @return ObjectCollectionInterface + */ + private function getProcedures() + { + return $this->getCollection(static::$proceduresPropertyName); + } + + public function hasProcedure($uri) + { + return $this->getProcedures()->has($uri); + } + + /** + * @param string $uri + * + * @return Contract\ProcedureInterface + */ + public function getProcedure($uri) + { + return $this->getProcedures()->get($uri); + } + + /** + * @param string $uri + */ + public function removeProcedure($uri) + { + $this->getProcedures()->offsetUnset($uri); + } + + /** + * @return \Generator + */ + public function listProcedures() + { + foreach ($this->getProcedures()->getIterator() as $uri => $procedure) { + yield $uri => $procedure; + } + } + + /** + * @param ObjectCollectionInterface $procedures + */ + private function setProcedures(ObjectCollectionInterface $procedures) + { + $this->initCollection(static::$proceduresPropertyName, $procedures); + $procedures->setObjectConstrain(Contract\ProcedureInterface::class); + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Collection/HasRealmsTrait.php b/src/Tidal/WampWatch/Model/Property/Collection/HasRealmsTrait.php new file mode 100644 index 0000000..5825878 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Collection/HasRealmsTrait.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Collection; + +use Tidal\WampWatch\Model\Contract; +use Tidal\WampWatch\Model\Contract\Property\ObjectCollectionInterface; +use Tidal\WampWatch\Model\Contract\RealmInterface; + +/** + * Class HasRealmsTrait. + * + * Important! Classes using this trait have to also use trait + * Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait + * for this trait to work; + */ +trait HasRealmsTrait +{ + protected $realmsPropertyName = 'realms'; + + /** + * @var ObjectCollectionInterface + */ + private $realms; + + public function addRealm(Contract\RealmInterface $realm) + { + $this->getRealms()->set($realm->getName(), $realm); + } + + /** + * @return ObjectCollectionInterface + */ + private function getRealms() + { + return $this->getCollection($this->realmsPropertyName); + } + + public function hasRealm($name) + { + return $this->getRealms()->has($name); + } + + /** + * @param $name + * + * @return Contract\RealmInterface + */ + public function getRealm($name) + { + return $this->getRealms()->get($name); + } + + /** + * @param $name + */ + public function removeRealm($name) + { + $this->getRealms()->offsetUnset($name); + } + + /** + * @return \Generator + */ + public function listRealms() + { + foreach ($this->getRealms()->getIterator() as $name => $realm) { + yield $name => $realm; + } + } + + /** + * @param ObjectCollectionInterface $realms + */ + private function setRealms(ObjectCollectionInterface $realms) + { + $this->initCollection($this->realmsPropertyName, $realms); + $realms->setObjectConstrain(RealmInterface::class); + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Collection/HasTopicsTrait.php b/src/Tidal/WampWatch/Model/Property/Collection/HasTopicsTrait.php new file mode 100644 index 0000000..85d5223 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Collection/HasTopicsTrait.php @@ -0,0 +1,87 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Collection; + +use Tidal\WampWatch\Model\Contract; +use Tidal\WampWatch\Model\Contract\Property\ObjectCollectionInterface; + +/** + * Class HasTopicsTrait. + * + * Important! Classes using this trait have to also use trait + * Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait + * for this trait to work; + */ +trait HasTopicsTrait +{ + protected $topicsPropertyName = 'topics'; + + /** + * @var ObjectCollectionInterface + */ + private $topics; + + public function addTopic(Contract\TopicInterface $topic) + { + $this->getTopics()->set($topic->getUri(), $topic); + } + + /** + * @return ObjectCollectionInterface + */ + private function getTopics() + { + return $this->getCollection($this->topicsPropertyName); + } + + public function hasTopic($uri) + { + return $this->getTopics()->has($uri); + } + + /** + * @param string $uri + * + * @return Contract\TopicInterface + */ + public function getTopic($uri) + { + return $this->getTopics()->get($uri); + } + + /** + * @param string $uri + */ + public function removeTopic($uri) + { + $this->getTopics()->offsetUnset($uri); + } + + /** + * @return \Generator + */ + public function listTopics() + { + foreach ($this->getTopics()->getIterator() as $uri => $topic) { + yield $uri => $topic; + } + } + + /** + * @param ObjectCollectionInterface $topics + */ + private function setTopics(ObjectCollectionInterface $topics) + { + $this->initCollection($this->topicsPropertyName, $topics); + $topics->setObjectConstrain(Contract\TopicInterface::class); + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Object/HasProcedureTrait.php b/src/Tidal/WampWatch/Model/Property/Object/HasProcedureTrait.php new file mode 100644 index 0000000..decabc4 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Object/HasProcedureTrait.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Object; + +use Tidal\WampWatch\Model\Contract; + +trait HasProcedureTrait +{ + /** + * @var Contract\ProcedureInterface + */ + private $procedure; + + /** + * @return Contract\ProcedureInterface + */ + public function getProcedure() + { + return $this->procedure; + } + + /** + * @param Contract\ProcedureInterface $procedure + */ + private function setProcedure(Contract\ProcedureInterface $procedure) + { + $this->procedure = $procedure; + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Object/HasRealmTrait.php b/src/Tidal/WampWatch/Model/Property/Object/HasRealmTrait.php new file mode 100644 index 0000000..f162df0 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Object/HasRealmTrait.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Object; + +use Tidal\WampWatch\Model\Contract; + +trait HasRealmTrait +{ + /** + * @var Contract\RealmInterface + */ + private $realm; + + /** + * @return Contract\RealmInterface + */ + public function getRealm() + { + return $this->realm; + } + + /** + * @param Contract\RealmInterface $realm + */ + private function setRealm(Contract\RealmInterface $realm) + { + $this->realm = $realm; + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Object/HasRouterTrait.php b/src/Tidal/WampWatch/Model/Property/Object/HasRouterTrait.php new file mode 100644 index 0000000..a052f31 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Object/HasRouterTrait.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Object; + +use Tidal\WampWatch\Model\Contract; + +trait HasRouterTrait +{ + /** + * @var Contract\RouterInterface + */ + private $router; + + /** + * @return Contract\RouterInterface + */ + public function getRouter() + { + return $this->router; + } + + /** + * @param Contract\RouterInterface $router + */ + private function setRouter(Contract\RouterInterface $router) + { + $this->router = $router; + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Object/HasSessionTrait.php b/src/Tidal/WampWatch/Model/Property/Object/HasSessionTrait.php new file mode 100644 index 0000000..7f8439e --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Object/HasSessionTrait.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Object; + +use Tidal\WampWatch\Model\Contract; + +trait HasSessionTrait +{ + /** + * @var Contract\SessionInterface + */ + private $session; + + /** + * @return Contract\SessionInterface + */ + public function getSession() + { + return $this->session; + } + + /** + * @param Contract\SessionInterface $session + */ + private function setSession(Contract\SessionInterface $session) + { + $this->session = $session; + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Object/HasTopicTrait.php b/src/Tidal/WampWatch/Model/Property/Object/HasTopicTrait.php new file mode 100644 index 0000000..6116ccf --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Object/HasTopicTrait.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Object; + +use Tidal\WampWatch\Model\Contract; + +trait HasTopicTrait +{ + /** + * @var Contract\TopicInterface + */ + private $topic; + + /** + * @return Contract\TopicInterface + */ + public function getTopic() + { + return $this->topic; + } + + /** + * @param Contract\TopicInterface $topic + */ + private function setTopic(Contract\TopicInterface $topic) + { + $this->topic = $topic; + } +} diff --git a/src/Tidal/WampWatch/Model/Property/ObjectCollection.php b/src/Tidal/WampWatch/Model/Property/ObjectCollection.php new file mode 100644 index 0000000..1e3339a --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/ObjectCollection.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property; + +use InvalidArgumentException; +use Tidal\WampWatch\Model\Contract\Property\ObjectCollectionInterface; + +class ObjectCollection extends Collection implements ObjectCollectionInterface +{ + private $objectConstrain; + + /** + * Set a constrain on a class or interface name the collection's items must be an instance of. + * Paramater 1 $cls expects a fully qualified class name. + * + * @param string $cls A Fully qualified class name + */ + public function setObjectConstrain($cls) + { + if (!class_exists($cls) && !interface_exists($cls)) { + throw new InvalidArgumentException("Class or Interface '$cls' not found."); + } + + $this->objectConstrain = $cls; + } + + public function append($value) + { + $this->validateObjectConstrain($value); + + parent::append($value); + } + + private function validateObjectConstrain($item) + { + if (!isset($this->objectConstrain)) { + return; + } + + if (!is_a($item, $this->objectConstrain)) { + throw new InvalidArgumentException("Item must be instance of '{$this->objectConstrain}'"); + } + } + + public function offsetSet($index, $newValue) + { + $this->validateObjectConstrain($newValue); + + parent::offsetSet($index, $newValue); + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Scalar/HasIdTrait.php b/src/Tidal/WampWatch/Model/Property/Scalar/HasIdTrait.php new file mode 100644 index 0000000..9e4fbb1 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Scalar/HasIdTrait.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Scalar; + +trait HasIdTrait +{ + private $id; + + public function getId() + { + return $this->id; + } + + private function setId($id) + { + $this->id = $id; + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Scalar/HasNameTrait.php b/src/Tidal/WampWatch/Model/Property/Scalar/HasNameTrait.php new file mode 100644 index 0000000..adb50a5 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Scalar/HasNameTrait.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model\Property\Scalar; + +trait HasNameTrait +{ + private $name; + + public function getName() + { + return $this->name; + } + + /** + * @param string $name + */ + private function setName($name) + { + $this->name = $name; + } +} diff --git a/src/Tidal/WampWatch/Model/Property/Scalar/HasUriTrait.php b/src/Tidal/WampWatch/Model/Property/Scalar/HasUriTrait.php new file mode 100644 index 0000000..01fcba4 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Property/Scalar/HasUriTrait.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +/** + * Created by PhpStorm. + * User: Timo + * Date: 25.07.2016 + * Time: 20:07. + */ +namespace Tidal\WampWatch\Model\Property\Scalar; + +trait HasUriTrait +{ + private $uri; + + /** + * @return string + */ + public function getUri() + { + return $this->uri; + } + + private function setUri($uri) + { + $this->uri = (string) $uri; + } +} diff --git a/src/Tidal/WampWatch/Model/Realm.php b/src/Tidal/WampWatch/Model/Realm.php new file mode 100644 index 0000000..25bda84 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Realm.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model; + +use Tidal\WampWatch\Model\Property\Scalar\HasNameTrait; +use Tidal\WampWatch\Model\Property\Collection\HasTopicsTrait; +use Tidal\WampWatch\Model\Property\Collection\HasProceduresTrait; +use Tidal\WampWatch\Model\Property\Object\HasRouterTrait; +use Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait; +use Tidal\WampWatch\Model\Property\ObjectCollection; + +class Realm implements Contract\RealmInterface +{ + use HasCollectionsTrait; + use HasNameTrait; + use HasTopicsTrait; + use HasProceduresTrait; + use HasRouterTrait; + + public function __construct($name, Contract\RouterInterface $router) + { + $this->setName($name); + $this->setRouter($router); + $this->setProcedures(new ObjectCollection()); + $this->setTopics(new ObjectCollection()); + } +} diff --git a/src/Tidal/WampWatch/Model/Registration.php b/src/Tidal/WampWatch/Model/Registration.php new file mode 100644 index 0000000..2dc1b2e --- /dev/null +++ b/src/Tidal/WampWatch/Model/Registration.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + + +namespace Tidal\WampWatch\Model; + +use Tidal\WampWatch\Model\Contract\RegistrationInterface; +use Tidal\WampWatch\Model\Contract\SessionInterface; +use Tidal\WampWatch\Model\Contract\ProcedureInterface; +use Tidal\WampWatch\Model\Property\Scalar\HasIdTrait; +use Tidal\WampWatch\Model\Property\Object\HasSessionTrait; +use Tidal\WampWatch\Model\Property\Object\HasProcedureTrait; + +class Registration implements RegistrationInterface +{ + use HasIdTrait; + use HasSessionTrait; + use HasProcedureTrait; + + public function __construct($id, SessionInterface $session, ProcedureInterface $procedure) + { + $this->setId($id); + $this->setSession($session); + $this->setProcedure($procedure); + } +} diff --git a/src/Tidal/WampWatch/Model/Router.php b/src/Tidal/WampWatch/Model/Router.php new file mode 100644 index 0000000..85df6ac --- /dev/null +++ b/src/Tidal/WampWatch/Model/Router.php @@ -0,0 +1,71 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model; + +use Guzzle\Common\Exception\RuntimeException; +use Tidal\WampWatch\Model\Property\Collection\HasRealmsTrait; +use Tidal\WampWatch\Model\Property\Scalar\HasUriTrait; +use Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait; +use Tidal\WampWatch\Model\Property\ObjectCollection; +use Exception; + +class Router implements Contract\RouterInterface +{ + use HasCollectionsTrait; + use HasRealmsTrait; + use HasUriTrait; + + const DEFAULT_CONNECTION_CLS = '\Tidal\WampWatch\Model\Connection'; + + private static $connectionFactory; + + public function __construct($uri) + { + $this->setUri((string) $uri); + $this->setRealms(new ObjectCollection()); + } + + /** + * @param callable $factory + */ + public static function setConnectionFactory(callable $factory) + { + self::$connectionFactory = $factory; + } + + /** + * @param \Tidal\WampWatch\Model\Contract\RealmInterface $realm + * + * @return mixed + * + * @throws \Exception + */ + public function connect(Contract\RealmInterface $realm) + { + try { + $factory = self::$connectionFactory; + /** @var Contract\ConnectionInterface $connection */ + $connection = $factory($this, $realm); + if (!is_a($connection, Contract\ConnectionInterface::class)) { + throw new RuntimeException('Callable registered as factory for Connection did not return Connection instance'); + } + } catch (Exception $e) { + throw $e; + } + + return $connection; + } +} + +Router::setConnectionFactory(function (Contract\RouterInterface $router, Contract\RealmInterface $realm) { + return new Connection($router, $realm); +}); diff --git a/src/Tidal/WampWatch/Model/Session.php b/src/Tidal/WampWatch/Model/Session.php new file mode 100644 index 0000000..38f609c --- /dev/null +++ b/src/Tidal/WampWatch/Model/Session.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + + +namespace Tidal\WampWatch\Model; + +use Tidal\WampWatch\Model\Property\Scalar\HasIdTrait; + +class Session implements Contract\SessionInterface +{ + use HasIdTrait; + + public function __construct($id) + { + $this->setId($id); + } +} diff --git a/src/Tidal/WampWatch/Model/Subscription.php b/src/Tidal/WampWatch/Model/Subscription.php new file mode 100644 index 0000000..2324851 --- /dev/null +++ b/src/Tidal/WampWatch/Model/Subscription.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + + +namespace Tidal\WampWatch\Model; + +use Tidal\WampWatch\Model\Contract\SessionInterface; +use Tidal\WampWatch\Model\Contract\TopicInterface; +use Tidal\WampWatch\Model\Property\Scalar\HasIdTrait; +use Tidal\WampWatch\Model\Property\Object\HasSessionTrait; +use Tidal\WampWatch\Model\Property\Object\HasTopicTrait; + +class Subscription implements Contract\SubscriptionInterface +{ + use HasIdTrait; + use HasSessionTrait; + use HasTopicTrait; + + public function __construct($id, SessionInterface $session, TopicInterface $topic) + { + $this->setId($id); + $this->setSession($session); + $this->setTopic($topic); + } +} diff --git a/src/Tidal/WampWatch/Model/Topic.php b/src/Tidal/WampWatch/Model/Topic.php new file mode 100644 index 0000000..b14851e --- /dev/null +++ b/src/Tidal/WampWatch/Model/Topic.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Model; + +use Tidal\WampWatch\Model\Property\Scalar\HasUriTrait; + +class Topic implements Contract\TopicInterface +{ + use HasUriTrait; + + public function __construct($uri) + { + $this->setUri($uri); + } +} diff --git a/src/Tidal/WampWatch/Subscription/Collection.php b/src/Tidal/WampWatch/Subscription/Collection.php index ce53b7d..2ae76f8 100644 --- a/src/Tidal/WampWatch/Subscription/Collection.php +++ b/src/Tidal/WampWatch/Subscription/Collection.php @@ -96,9 +96,6 @@ public function subscribe() return $this->subscriptionPromise->promise(); } - /** - * - */ protected function doSubscribe() { \React\Promise\all($this->getSubscriptionPromises())->done(function () { diff --git a/tests/unit/Adapter/React/DeferredAdapterTest.php b/tests/unit/Adapter/React/DeferredAdapterTest.php index d0b04f4..742fcc5 100644 --- a/tests/unit/Adapter/React/DeferredAdapterTest.php +++ b/tests/unit/Adapter/React/DeferredAdapterTest.php @@ -1,21 +1,24 @@ * - * * This file is part of the Tidal/WampWatch package. - * * (c) 2016 Timo Michna - * * - * * For the full copyright and license information, please view the LICENSE - * * file that was distributed with this source code. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. * */ +namespace Tidal\WampWatch\Test\Unit\Adapter\React; + require_once __DIR__ . '/../../Stub/PromiseStub.php'; use Tidal\WampWatch\Adapter\React\DeferredAdapter; +use Tidal\WampWatch\Test\Unit\Stub\PromiseStub; use React\Promise\Deferred; use React\Promise\Promise; +use PHPUnit_Framework_TestCase; -class DeferredAdapterTest extends \PHPUnit_Framework_TestCase +class DeferredAdapterTest extends PHPUnit_Framework_TestCase { /** diff --git a/tests/unit/Adapter/React/PromiseAdapterTest.php b/tests/unit/Adapter/React/PromiseAdapterTest.php index e37d391..8e29bf8 100644 --- a/tests/unit/Adapter/React/PromiseAdapterTest.php +++ b/tests/unit/Adapter/React/PromiseAdapterTest.php @@ -1,16 +1,18 @@ * - * * This file is part of the Tidal/WampWatch package. - * * (c) 2016 Timo Michna - * * - * * For the full copyright and license information, please view the LICENSE - * * file that was distributed with this source code. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. * */ +namespace Tidal\WampWatch\Test\Unit\Adapter\React; + use Tidal\WampWatch\Adapter\React\PromiseAdapter; use React\Promise\Promise; +use PHPUnit_Framework_TestCase; class PromiseAdapterTest extends PHPUnit_Framework_TestCase { diff --git a/tests/unit/Adapter/ThruwaySessionTest.php b/tests/unit/Adapter/ThruwaySessionTest.php index fdfc91c..db2e882 100644 --- a/tests/unit/Adapter/ThruwaySessionTest.php +++ b/tests/unit/Adapter/ThruwaySessionTest.php @@ -1,6 +1,6 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ -require_once __DIR__ . '/../bootstrap.php'; +namespace Tidal\WampWatch\Test\Unit; +require_once __DIR__ . '/../bootstrap.php'; use Tidal\WampWatch\Stub\ClientSessionStub; use Tidal\WampWatch\Exception\UnknownProcedureException; use Tidal\WampWatch\Exception\UnknownTopicException; +use Thruway\Message\ErrorMessage as ThruwayErrorMessage; +use stdClass; -class ClientSessionStubTest extends PHPUnit_Framework_TestCase +class ClientSessionStubTest extends \PHPUnit_Framework_TestCase { const PROMISE_CLS = 'React\Promise\Promise'; @@ -523,7 +534,7 @@ private function getEmptyFunc() private function getErrorMessage() { - return new Thruway\Message\ErrorMessage( + return new ThruwayErrorMessage( "wamp.error.not_authorized", 321, new stdClass(), diff --git a/tests/unit/Exception/NoSuchProcedureExceptionTest.php b/tests/unit/Exception/NoSuchProcedureExceptionTest.php index 5d82a31..3346492 100644 --- a/tests/unit/Exception/NoSuchProcedureExceptionTest.php +++ b/tests/unit/Exception/NoSuchProcedureExceptionTest.php @@ -1,19 +1,21 @@ * - * * This file is part of the Tidal/WampWatch package. - * * (c) 2016 Timo Michna - * * - * * For the full copyright and license information, please view the LICENSE - * * file that was distributed with this source code. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. * */ +namespace Tidal\WampWatch\Test\Unit\Exception; + require_once __DIR__ . '/../../bootstrap.php'; use Tidal\WampWatch\Exception\NoSuchProcedureException; +use PHPUnit_Framework_TestCase; -class NoSuchProcedureExceptionTest extends \PHPUnit_Framework_TestCase +class NoSuchProcedureExceptionTest extends PHPUnit_Framework_TestCase { public function test_topic_name_can_be_retrieved() diff --git a/tests/unit/Exception/NotAuthorizedExceptionTest.php b/tests/unit/Exception/NotAuthorizedExceptionTest.php index 2f261a7..c28f0f3 100644 --- a/tests/unit/Exception/NotAuthorizedExceptionTest.php +++ b/tests/unit/Exception/NotAuthorizedExceptionTest.php @@ -1,15 +1,17 @@ * - * * This file is part of the Tidal/WampWatch package. - * * (c) 2016 Timo Michna - * * - * * For the full copyright and license information, please view the LICENSE - * * file that was distributed with this source code. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. * */ +namespace Tidal\WampWatch\Test\Unit\Exception; + use Tidal\WampWatch\Exception\NotAuthorizedException; +use PHPUnit_Framework_TestCase; class NotAuthorizedExceptionTest extends PHPUnit_Framework_TestCase { diff --git a/tests/unit/Exception/UnknownProcedureExceptionTest.php b/tests/unit/Exception/UnknownProcedureExceptionTest.php index 5c1c6ef..ce02fb2 100644 --- a/tests/unit/Exception/UnknownProcedureExceptionTest.php +++ b/tests/unit/Exception/UnknownProcedureExceptionTest.php @@ -1,17 +1,19 @@ * - * * This file is part of the Tidal/WampWatch package. - * * (c) 2016 Timo Michna - * * - * * For the full copyright and license information, please view the LICENSE - * * file that was distributed with this source code. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. * */ +namespace Tidal\WampWatch\Test\Unit\Exception; + require_once __DIR__ . '/../../bootstrap.php'; use Tidal\WampWatch\Exception\UnknownProcedureException; +use PHPUnit_Framework_TestCase; class UnknownProcedureExceptionTest extends PHPUnit_Framework_TestCase { diff --git a/tests/unit/Exception/UnknownTopicExceptionTest.php b/tests/unit/Exception/UnknownTopicExceptionTest.php index b47d120..45fe297 100644 --- a/tests/unit/Exception/UnknownTopicExceptionTest.php +++ b/tests/unit/Exception/UnknownTopicExceptionTest.php @@ -1,21 +1,22 @@ * - * * This file is part of the Tidal/WampWatch package. - * * (c) 2016 Timo Michna - * * - * * For the full copyright and license information, please view the LICENSE - * * file that was distributed with this source code. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. * */ +namespace Tidal\WampWatch\Test\Unit\Exception; + require_once __DIR__ . '/../../bootstrap.php'; use Tidal\WampWatch\Exception\UnknownTopicException; +use PHPUnit_Framework_TestCase; class UnknownUnknownTopicExceptionTest extends PHPUnit_Framework_TestCase { - public function test_topic_name_can_be_retrieved() { $e = new UnknownTopicException('foo'); @@ -23,5 +24,4 @@ public function test_topic_name_can_be_retrieved() $this->assertSame('foo', $e->getTopicName()); } - } diff --git a/tests/unit/Model/Behavior/Property/HasCollectionsTest.php b/tests/unit/Model/Behavior/Property/HasCollectionsTest.php new file mode 100644 index 0000000..44e382a --- /dev/null +++ b/tests/unit/Model/Behavior/Property/HasCollectionsTest.php @@ -0,0 +1,80 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Model\Behavior\Property; + +use Tidal\WampWatch\Model\Property\ObjectCollection; +use Tidal\WampWatch\Test\Unit\Stub\HasCollectionsImplementation; +use PHPUnit_Framework_TestCase; +use InvalidArgumentException; + +class HasCollectionsTest extends PHPUnit_Framework_TestCase +{ + /** + * @var HasCollectionsImplementation + */ + private $mock; + + + public function setUp() + { + $this->mock = new HasCollectionsImplementation(); + } + + public function test_can_initialize_collection() + { + $collection = $this->getMockBuilder(ObjectCollection::class) + ->getMock(); + + $this->mock->init( + 'foo', + $collection + ); + + $this->assertTrue($this->mock->has('foo')); + } + + public function test_can_append_value_to_collection() + { + $collection = $this->getMockBuilder(ObjectCollection::class) + ->getMock(); + + $collection + ->expects($this->once()) + ->method('append') + ->with('bar'); + + $this->mock->init( + 'foo', + $collection + ); + $this->mock->append('foo', 'bar'); + } + + public function test_can_retrieve_collection() + { + $collection = $this->getMockBuilder(ObjectCollection::class) + ->getMock(); + + $this->mock->init( + 'foo', + $collection + ); + + $this->assertEquals($collection, $this->mock->get('foo')); + } + + public function test_retrieve_throws_exception_on_unknown_collection() + { + $this->setExpectedException(InvalidArgumentException::class); + + $this->mock->get('foo'); + } + +} diff --git a/tests/unit/Model/Factory/ConnectionFactoryTest.php b/tests/unit/Model/Factory/ConnectionFactoryTest.php new file mode 100644 index 0000000..242aa6e --- /dev/null +++ b/tests/unit/Model/Factory/ConnectionFactoryTest.php @@ -0,0 +1,120 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model\Factory; + +use Tidal\WampWatch\Model\Factory\ConnectionFactory; +use Tidal\WampWatch\Test\Unit\Model\HasSessionTrait; +use Tidal\WampWatch\Test\Unit\Model\HasRouterTrait; +use Tidal\WampWatch\Test\Unit\Model\HasRealmTrait; +use Tidal\WampWatch\Model\Connection; +use PHPUnit_Framework_TestCase; +use RuntimeException; + +class ConnectionFactoryTest extends PHPUnit_Framework_TestCase +{ + use HasSessionTrait; + use HasRouterTrait; + use HasRealmTrait; + + /** + * @var ConnectionFactory + */ + private $factory; + + public function setUp() + { + $this->factory = ConnectionFactory::get( + $this->getRouterMock() + ); + } + + public function test_can_get_instance_for_router() + { + $this->assertInstanceOf(ConnectionFactory::class, $this->factory); + } + + public function test_can_select_realm() + { + $factory = $this->factory->select( + $this->getRealmMock() + ); + $this->assertInstanceOf(ConnectionFactory::class, $factory); + } + + public function test_select_returns_new_instance() + { + $factory = $this->factory->select( + $this->getRealmMock() + ); + + $this->assertNotEquals($this->factory, $factory); + } + + public function test_can_establish_session() + { + $factory = $this->factory->establish( + $this->getSessionMock() + ); + $this->assertInstanceOf(ConnectionFactory::class, $factory); + } + + public function test_select_establish_new_instance() + { + $factory = $this->factory->establish( + $this->getSessionMock() + ); + + $this->assertNotEquals($this->factory, $factory); + } + + public function test_can_create() + { + $connection = $this->factory->select( + $this->getRealmMock() + )->establish( + $this->getSessionMock() + )->create(); + + $this->assertInstanceOf(Connection::class, $connection); + } + + public function test_uses_given_dependencies() + { + $router = $this->getRouterMock(); + $realm = $this->getRealmMock(); + $session = $this->getSessionMock(); + + $connection = ConnectionFactory::get( + $router + )->select( + $realm + )->establish( + $session + )->create(); + + $this->assertEquals($router, $connection->getRouter()); + $this->assertEquals($realm, $connection->getRealm()); + $this->assertEquals($session, $connection->getSession()); + } + + public function test_create_throws_exception_without_realm() + { + $this->setExpectedException( + RuntimeException::class + ); + + $this->factory->establish( + $this->getSessionMock() + )->create(); + } + +} diff --git a/tests/unit/Model/HasProcedureTrait.php b/tests/unit/Model/HasProcedureTrait.php new file mode 100644 index 0000000..5bb644f --- /dev/null +++ b/tests/unit/Model/HasProcedureTrait.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Procedure; +use Tidal\WampWatch\Test\Unit\Stub\MockHelper; +use PHPUnit_Framework_TestCase as TestCase; + +trait HasProcedureTrait +{ + /** + * @param string $uri + * + * @return \PHPUnit_Framework_MockObject_MockObject|Procedure + */ + private function getProcedureMock($uri = 'faa') + { + /** @var TestCase $self */ + $self = $this; + $mock = MockHelper::getNoConstructorMock($self, Procedure::class); + $mock->expects($this->any()) + ->method('getUri') + ->willReturn($uri); + + return $mock; + } +} \ No newline at end of file diff --git a/tests/unit/Model/HasRealmTrait.php b/tests/unit/Model/HasRealmTrait.php new file mode 100644 index 0000000..7f2deca --- /dev/null +++ b/tests/unit/Model/HasRealmTrait.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Realm; +use Tidal\WampWatch\Test\Unit\Stub\MockHelper; +use PHPUnit_Framework_TestCase as TestCase; + +trait HasRealmTrait +{ + /** + * @param string $name + * + * @return \PHPUnit_Framework_MockObject_MockObject|Realm + */ + private function getRealmMock($name = 'faz') + { + /** @var TestCase $self */ + $self = $this; + $mock = MockHelper::getNoConstructorMock($self, Realm::class); + $mock->expects($this->any()) + ->method('getName') + ->willReturn($name); + + return $mock; + } +} \ No newline at end of file diff --git a/tests/unit/Model/HasRouterTrait.php b/tests/unit/Model/HasRouterTrait.php new file mode 100644 index 0000000..2b203dc --- /dev/null +++ b/tests/unit/Model/HasRouterTrait.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Router; +use Tidal\WampWatch\Test\Unit\Stub\MockHelper; +use PHPUnit_Framework_TestCase as TestCase; + +trait HasRouterTrait +{ + /** + * @param string $uri + * + * @return \PHPUnit_Framework_MockObject_MockObject|Router + */ + private function getRouterMock($uri = 'faz') + { + /** @var TestCase $self */ + $self = $this; + $mock = MockHelper::getNoConstructorMock($self, Router::class); + $mock->expects($this->any()) + ->method('getUri') + ->willReturn($uri); + + return $mock; + } +} \ No newline at end of file diff --git a/tests/unit/Model/HasSessionTrait.php b/tests/unit/Model/HasSessionTrait.php new file mode 100644 index 0000000..b045e35 --- /dev/null +++ b/tests/unit/Model/HasSessionTrait.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Session; +use Tidal\WampWatch\Test\Unit\Stub\MockHelper; +use PHPUnit_Framework_TestCase as TestCase; + +trait HasSessionTrait +{ + /** + * @param string $id + * + * @return \PHPUnit_Framework_MockObject_MockObject|Session + */ + private function getSessionMock($id = 'baz') + { + /** @var TestCase $self */ + $self = $this; + $mock = MockHelper::getNoConstructorMock($self, Session::class); + $mock->expects($this->any()) + ->method('getId') + ->willReturn($id); + + return $mock; + } +} \ No newline at end of file diff --git a/tests/unit/Model/HasTopicTrait.php b/tests/unit/Model/HasTopicTrait.php new file mode 100644 index 0000000..d453b3b --- /dev/null +++ b/tests/unit/Model/HasTopicTrait.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Topic; +use Tidal\WampWatch\Test\Unit\Stub\MockHelper; +use PHPUnit_Framework_TestCase as TestCase; + +trait HasTopicTrait +{ + /** + * @param string $uri + * + * @return \PHPUnit_Framework_MockObject_MockObject|Topic + */ + private function getTopicMock($uri = 'foo') + { + /** @var TestCase $self */ + $self = $this; + $mock = MockHelper::getNoConstructorMock($self, Topic::class); + $mock->expects($this->any()) + ->method('getUri') + ->willReturn($uri); + + return $mock; + } +} \ No newline at end of file diff --git a/tests/unit/Model/ProcedureTest.php b/tests/unit/Model/ProcedureTest.php new file mode 100644 index 0000000..4dc478e --- /dev/null +++ b/tests/unit/Model/ProcedureTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Procedure; + + +class ProcedureTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Procedure + */ + private $procedure; + + public function setUp() + { + $this->procedure = new Procedure('foo'); + } + + public function test_can_retrieve_uri() + { + $this->assertSame('foo', $this->procedure->getUri()); + } +} diff --git a/tests/unit/Model/Property/CollectionTest.php b/tests/unit/Model/Property/CollectionTest.php new file mode 100644 index 0000000..beef625 --- /dev/null +++ b/tests/unit/Model/Property/CollectionTest.php @@ -0,0 +1,176 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Model\Property; + +use Tidal\WampWatch\Model\Property\Collection; +use PHPUnit_Framework_TestCase; +use InvalidArgumentException; + +class CollectionTest extends PHPUnit_Framework_TestCase +{ + + /** + * @var Collection + */ + private $collection; + + public function setUp() + { + $this->collection = new Collection(); + } + + public function test_can_append() + { + $this->collection->append('foo'); + + $this->assertEquals('foo', $this->collection[0]); + } + + public function test_can_get_generator() + { + $values = ['foo', 'bar', 'baz']; + + foreach ($values as $value) { + $this->collection->append($value); + } + + foreach ($this->collection->getGenerator() as $key => $value) { + $this->assertEquals($values[$key], $value); + } + } + + /** + * + */ + public function test_can_set_valid_callback() + { + $res = null; + + $this->collection->setValidationCallback(function ($value) use (&$res) { + $res = $value; + + return true; + }); + + $this->collection->append('foo'); + + $this->assertEquals('foo', $res); + } + + /** + * + */ + public function test_failed_validation_callback_throws_exception() + { + $this->setExpectedException(InvalidArgumentException::class); + + $this->collection->setValidationCallback(function () { + return false; + }); + + $this->collection->append('foo'); + } + + /** + * + */ + public function test_can_access_values() + { + $this->assertFalse($this->collection->has('foo')); + $this->collection->set('foo', 'bar'); + $this->assertTrue($this->collection->has('foo')); + $this->assertEquals('bar', $this->collection->get('foo')); + } + + /** + * @param string $type + * @param mixed $value + * + * @dataProvider retrieveValidItemTypes + */ + public function test_can_set_valid_item_types($type, $value) + { + $this->collection->setItemType($type); + + $this->collection->append($value); + + $this->assertEquals($value, $this->collection[0]); + } + + /** + * @return array + */ + public function retrieveValidItemTypes() + { + return [ + ['string', 'foo'], + ['integer', 321], + ['double', 3.21], + ['boolean', true], + ['array', []], + ['object', new \stdClass()] + ]; + } + + /** + * @param string $type + * @param mixed $value + * + * @dataProvider retrieveInvalidItemTypes + */ + public function test_cannot_set_invalid_item_types($type, $value) + { + $this->setExpectedException(InvalidArgumentException::class); + + $this->collection->setItemType($type); + + $this->collection->append($value); + } + + /** + * @return array + */ + public function retrieveInvalidItemTypes() + { + return [ + ['string', 321], + ['string', 3.21], + ['string', true], + ['string', []], + ['string', new \stdClass()], + ['integer', '321'], + ['integer', 3.21], + ['integer', true], + ['integer', []], + ['integer', new \stdClass()], + ['double', '321'], + ['double', 321], + ['double', true], + ['double', []], + ['boolean', '321'], + ['boolean', 321], + ['boolean', 3.21], + ['boolean', []], + ['boolean', new \stdClass()], + ['array', 321], + ['array', 3.21], + ['array', '321'], + ['array', true], + ['array', new \stdClass()], + ['object', 321], + ['object', 3.21], + ['object', true], + ['object', '321'], + ['object', []] + ]; + } + + +} diff --git a/tests/unit/Model/Property/ObjectCollectionTest.php b/tests/unit/Model/Property/ObjectCollectionTest.php new file mode 100644 index 0000000..323c02b --- /dev/null +++ b/tests/unit/Model/Property/ObjectCollectionTest.php @@ -0,0 +1,91 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Model\Property; + +use Tidal\WampWatch\Model\Property\ObjectCollection; +use PHPUnit_Framework_TestCase; +use InvalidArgumentException; + +class ObjectCollectionTest extends PHPUnit_Framework_TestCase +{ + /** + * @var ObjectCollection + */ + private $collection; + + public function setUp() + { + $this->collection = new ObjectCollection(); + } + + /** + * @param mixed $value + * + * @dataProvider retrieveTypes + */ + public function test_can_append_all_values_without_constrain($value) + { + $this->collection->append($value); + + $this->assertEquals($value, $this->collection[0]); + } + + /** + * @return array + */ + public function retrieveTypes() + { + return [ + ['foo'], + [321], + [3.21], + [true], + [[]], + [new \stdClass()] + ]; + } + + /** + */ + public function test_invalid_class_constrain_throws_exception() + { + $this->setExpectedException( + InvalidArgumentException::class + ); + + $this->collection->setObjectConstrain('Foo'); + } + + /** + */ + public function test_invalid_class_value_throws_exception() + { + $this->setExpectedException( + InvalidArgumentException::class + ); + + $this->collection->setObjectConstrain(\stdClass::class); + + $this->collection->append('foo'); + } + + /** + */ + public function test_given_class_is_valid() + { + $this->collection->setObjectConstrain(\stdClass::class); + + $value = new \stdClass(); + $this->collection->append($value); + + $this->assertEquals($value, $this->collection[0]); + } + +} diff --git a/tests/unit/Model/RealmTest.php b/tests/unit/Model/RealmTest.php new file mode 100644 index 0000000..853f3d1 --- /dev/null +++ b/tests/unit/Model/RealmTest.php @@ -0,0 +1,149 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Realm; +use Tidal\WampWatch\Model\Router; + +class RealmTest extends \PHPUnit_Framework_TestCase +{ + use HasRouterTrait; + use HasProcedureTrait; + use HasTopicTrait; + + const REALM_NAME = 'foo-realm'; + + /** + * @var Realm + */ + private $realm; + + /** + * @var Router + */ + private $router; + + public function setUp() + { + $this->router = $this->getRouterMock(); + + $this->realm = new Realm( + self::REALM_NAME, + $this->router + ); + } + + public function test_can_retrieve_name() + { + $this->assertSame(self::REALM_NAME, $this->realm->getName()); + } + + public function test_can_retrieve_router() + { + $this->assertSame($this->router, $this->realm->getRouter()); + } + + public function test_can_add_procedure() + { + $procedure = $this->getProcedureMock(); + $uri = $procedure->getUri(); + $this->realm->addProcedure($procedure); + + $this->assertTrue($this->realm->hasProcedure($uri)); + } + + public function test_can_remove_procedure() + { + $procedure = $this->getProcedureMock(); + $uri = $procedure->getUri(); + $this->realm->addProcedure($procedure); + $this->realm->removeProcedure($uri); + + $this->assertFalse($this->realm->hasProcedure($uri)); + } + + public function test_can_retrieve_procedure() + { + $procedure = $this->getProcedureMock(); + $uri = $procedure->getUri(); + $this->realm->addProcedure($procedure); + + $this->assertEquals($procedure, $this->realm->getProcedure($uri)); + } + + public function test_can_list_procedures() + { + $procedures = [ + $this->getProcedureMock('foo'), + $this->getProcedureMock('bar'), + $this->getProcedureMock('baz') + ]; + + foreach ($procedures as $procedure) { + $this->realm->addProcedure($procedure); + } + + $x = 0; + foreach ($this->realm->listProcedures() as $uri => $procedure) { + $this->assertEquals($procedures[$x], $procedure); + $this->assertEquals($procedures[$x], $this->realm->getProcedure($uri)); + $x++; + } + } + + public function test_can_add_topic() + { + $topic = $this->getTopicMock(); + $uri = $topic->getUri(); + $this->realm->addTopic($topic); + + $this->assertTrue($this->realm->hasTopic($uri)); + } + + public function test_can_remove_topic() + { + $topic = $this->getTopicMock(); + $uri = $topic->getUri(); + $this->realm->addTopic($topic); + $this->realm->removeTopic($uri); + + $this->assertFalse($this->realm->hasTopic($uri)); + } + + public function test_can_retrieve_topic() + { + $topic = $this->getTopicMock(); + $uri = $topic->getUri(); + $this->realm->addTopic($topic); + + $this->assertEquals($topic, $this->realm->getTopic($uri)); + } + + public function test_can_list_topics() + { + $topics = [ + $this->getTopicMock('foo'), + $this->getTopicMock('bar'), + $this->getTopicMock('baz') + ]; + + foreach ($topics as $topic) { + $this->realm->addTopic($topic); + } + + $x = 0; + foreach ($this->realm->listTopics() as $uri => $topic) { + $this->assertEquals($topics[$x], $topic); + $this->assertEquals($topics[$x], $this->realm->getTopic($uri)); + $x++; + } + } + +} diff --git a/tests/unit/Model/RegistrationTest.php b/tests/unit/Model/RegistrationTest.php new file mode 100644 index 0000000..5cb0ba7 --- /dev/null +++ b/tests/unit/Model/RegistrationTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Registration; + +class RegistrationTest extends \PHPUnit_Framework_TestCase +{ + use HasSessionTrait; + use HasProcedureTrait; + + /** + * @var Registration + */ + private $registration; + + public function setUp() + { + $this->registration = new Registration( + 321, + $this->getSessionMock('baz'), + $this->getProcedureMock('foo') + ); + } + + public function test_can_retrieve_id() + { + $this->assertSame(321, $this->registration->getId()); + } + + public function test_can_retrieve_session() + { + $this->assertSame('baz', $this->registration->getSession()->getId()); + } + + public function test_can_retrieve_topic() + { + $this->assertSame('foo', $this->registration->getProcedure()->getUri()); + } +} diff --git a/tests/unit/Model/RouterTest.php b/tests/unit/Model/RouterTest.php new file mode 100644 index 0000000..bdb96b2 --- /dev/null +++ b/tests/unit/Model/RouterTest.php @@ -0,0 +1,129 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Connection; +use Tidal\WampWatch\Model\Router; +use Tidal\WampWatch\Model\Contract\ConnectionInterface; +use RuntimeException; + +class RouterTest extends \PHPUnit_Framework_TestCase +{ + use HasRealmTrait; + + /** + * @var Router + */ + private $router; + + public function setUp() + { + $this->router = new Router('foo://bar'); + } + + public function test_can_retrieve_uri() + { + $this->assertSame('foo://bar', $this->router->getUri()); + } + + public function test_can_connect() + { + $connection = $this->router->connect( + $this->getRealmMock() + ); + + $this->assertInstanceOf(ConnectionInterface::class, $connection); + } + + public function test_can_set_connection_factory() + { + $connectionMock = $this->getMockBuilder(Connection::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->router->setConnectionFactory(function () use ($connectionMock) { + return $connectionMock; + }); + + $connection = $this->router->connect( + $this->getRealmMock() + ); + + $this->assertSame($connectionMock, $connection); + } + + public function test_incorrect_connection_factory_throws_exception() + { + $this->setExpectedException( + RuntimeException::class + ); + + $connectionMock = $this->getMockBuilder(Connection::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->router->setConnectionFactory(function () use ($connectionMock) { + return new \stdClass(); + }); + + $this->router->connect( + $this->getRealmMock() + ); + } + + public function test_can_add_realm() + { + $realm = $this->getRealmMock(); + $name = $realm->getName(); + $this->router->addRealm($realm); + + $this->assertTrue($this->router->hasRealm($name)); + } + + public function test_can_remove_realm() + { + $realm = $this->getRealmMock(); + $name = $realm->getName(); + $this->router->addRealm($realm); + $this->router->removeRealm($name); + + $this->assertFalse($this->router->hasRealm($name)); + } + + public function test_can_retrieve_realm() + { + $realm = $this->getRealmMock(); + $name = $realm->getName(); + $this->router->addRealm($realm); + + $this->assertEquals($realm, $this->router->getRealm($name)); + } + + public function test_can_list_realms() + { + $realms = [ + $this->getRealmMock('foo'), + $this->getRealmMock('bar'), + $this->getRealmMock('baz') + ]; + + foreach ($realms as $realm) { + $this->router->addRealm($realm); + } + + $x = 0; + foreach ($this->router->listRealms() as $name => $realm) { + $this->assertEquals($realms[$x], $realm); + $this->assertEquals($realms[$x], $this->router->getRealm($name)); + $x++; + } + } + +} diff --git a/tests/unit/Model/SessionTest.php b/tests/unit/Model/SessionTest.php new file mode 100644 index 0000000..bd5b6d7 --- /dev/null +++ b/tests/unit/Model/SessionTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Session; + +class SessionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Session + */ + private $session; + + public function setUp() + { + $this->session = new Session('foo'); + } + + public function test_can_retrieve_id() + { + $this->assertSame('foo', $this->session->getId()); + } +} diff --git a/tests/unit/Model/SubscriptionTest.php b/tests/unit/Model/SubscriptionTest.php new file mode 100644 index 0000000..ca85591 --- /dev/null +++ b/tests/unit/Model/SubscriptionTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Subscription; + +class SubscriptionTest extends \PHPUnit_Framework_TestCase +{ + use HasSessionTrait; + use HasTopicTrait; + + /** + * @var Subscription + */ + private $subscription; + + public function setUp() + { + $this->subscription = new Subscription( + 321, + $this->getSessionMock(), + $this->getTopicMock() + ); + } + + public function test_can_retrieve_id() + { + $this->assertSame(321, $this->subscription->getId()); + } + + public function test_can_retrieve_session() + { + $this->assertSame('baz', $this->subscription->getSession()->getId()); + } + + public function test_can_retrieve_topic() + { + $this->assertSame('foo', $this->subscription->getTopic()->getUri()); + } +} diff --git a/tests/unit/Model/TopicTest.php b/tests/unit/Model/TopicTest.php new file mode 100644 index 0000000..b4eae69 --- /dev/null +++ b/tests/unit/Model/TopicTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit\Model; + +use Tidal\WampWatch\Model\Topic; + +class TopicTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Topic + */ + private $topic; + + public function setUp() + { + $this->topic = new Topic('foo'); + } + + public function test_can_retrieve_uri() + { + $this->assertSame('foo', $this->topic->getUri()); + } + +} diff --git a/tests/unit/MonitorTraitTest.php b/tests/unit/MonitorTraitTest.php index a4f731e..76b4d1d 100644 --- a/tests/unit/MonitorTraitTest.php +++ b/tests/unit/MonitorTraitTest.php @@ -1,11 +1,23 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit; require_once __DIR__.'/../bootstrap.php'; require_once __DIR__ . '/Stub/MonitorTraitImplementation.php'; use Tidal\WampWatch\Stub\ClientSessionStub; +use Tidal\WampWatch\Test\Unit\Stub\MonitorTraitImplementation; use Tidal\WampWatch\Subscription\Collection; use Mockery as M; +use PHPUnit_Framework_TestCase; /** * @author Timo Michna diff --git a/tests/unit/SessionMonitorTest.php b/tests/unit/SessionMonitorTest.php index a65815a..2b963dc 100644 --- a/tests/unit/SessionMonitorTest.php +++ b/tests/unit/SessionMonitorTest.php @@ -1,11 +1,22 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ -require_once __DIR__.'/../bootstrap.php'; +namespace Tidal\WampWatch\Test\Unit; +require_once __DIR__.'/../bootstrap.php'; use Mockery as M; use Tidal\WampWatch\SessionMonitor; use Tidal\WampWatch\Stub\ClientSessionStub; +use PHPUnit_Framework_TestCase; +use stdClass; /** * @author Timo Michna diff --git a/tests/unit/Stub/HasCollectionsImplementation.php b/tests/unit/Stub/HasCollectionsImplementation.php new file mode 100644 index 0000000..bcf544e --- /dev/null +++ b/tests/unit/Stub/HasCollectionsImplementation.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Stub; + +use Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait; +use Tidal\WampWatch\Model\Contract\Property\CollectionInterface; + +class HasCollectionsImplementation +{ + use HasCollectionsTrait; + + private $foo; + + private $bar; + + private $baz; + + public function init($name, CollectionInterface $collection) + { + $this->initCollection($name, $collection); + } + + public function get($name) + { + return $this->getCollection($name); + } + + public function append($name, $value) + { + $this->appendTo($name, $value); + } + + public function has($name) + { + return $this->hasCollection($name); + } + + +} \ No newline at end of file diff --git a/tests/unit/Stub/MockHelper.php b/tests/unit/Stub/MockHelper.php new file mode 100644 index 0000000..1c064ee --- /dev/null +++ b/tests/unit/Stub/MockHelper.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tidal\WampWatch\Test\Unit\Stub; + +use PHPUnit_Framework_TestCase as TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; + +class MockHelper +{ + /** + * @param TestCase $test + * @param string $class + * + * @return MockObject + */ + public static function getNoConstructorMock(TestCase $test, $class) + { + return $test->getMockBuilder($class) + ->disableOriginalConstructor() + ->getMock(); + } + +} \ No newline at end of file diff --git a/tests/unit/Stub/MonitorTraitImplementation.php b/tests/unit/Stub/MonitorTraitImplementation.php index ff3f33f..244fed1 100644 --- a/tests/unit/Stub/MonitorTraitImplementation.php +++ b/tests/unit/Stub/MonitorTraitImplementation.php @@ -1,6 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ -require_once __DIR__ . '/../../bootstrap.php'; +namespace Tidal\WampWatch\Test\Unit\Stub; use \Tidal\WampWatch\MonitorTrait; use Tidal\WampWatch\ClientSessionInterface as ClientSession; diff --git a/tests/unit/Stub/PromiseStub.php b/tests/unit/Stub/PromiseStub.php index 18823ca..186033e 100644 --- a/tests/unit/Stub/PromiseStub.php +++ b/tests/unit/Stub/PromiseStub.php @@ -1,14 +1,15 @@ * - * * This file is part of the Tidal/WampWatch package. - * * (c) 2016 Timo Michna - * * - * * For the full copyright and license information, please view the LICENSE - * * file that was distributed with this source code. + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. * */ +namespace Tidal\WampWatch\Test\Unit\Stub; + use Tidal\WampWatch\Async\PromiseInterface; class PromiseStub implements PromiseInterface diff --git a/tests/unit/SubscriptionCollectionTest.php b/tests/unit/SubscriptionCollectionTest.php index 3725bc6..a6c5ce0 100644 --- a/tests/unit/SubscriptionCollectionTest.php +++ b/tests/unit/SubscriptionCollectionTest.php @@ -8,8 +8,7 @@ * */ -namespace Tidal\WampWatch\tests\unit; - +namespace Tidal\WampWatch\Test\Unit; use React\Promise\Promise; use Tidal\WampWatch\Stub\ClientSessionStub; diff --git a/tests/unit/SubscriptionMonitorTest.php b/tests/unit/SubscriptionMonitorTest.php index b370b95..0b5fd24 100644 --- a/tests/unit/SubscriptionMonitorTest.php +++ b/tests/unit/SubscriptionMonitorTest.php @@ -1,6 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ + +namespace Tidal\WampWatch\Test\Unit; require_once __DIR__ . '/../bootstrap.php';