From 498f4965e601a5db81981224b3603b394851e7f0 Mon Sep 17 00:00:00 2001 From: Vassyli Date: Thu, 2 Mar 2017 20:04:27 +0100 Subject: [PATCH] Adds tests and support for Unidirectional connections. Also fixes travis config. --- .travis.yml | 2 +- composer.json | 4 +- composer.lock | 117 ++++++++++++++-------------- src/ActionGroup.php | 4 + src/Game.php | 33 ++++---- src/Models/Scene.php | 6 +- src/Models/SceneConnection.php | 14 ++++ src/Models/SceneConnectionGroup.php | 9 +++ tests/GameTest.php | 59 ++++++++++++++ tests/datasets/game.yml | 45 +++++++++++ 10 files changed, 213 insertions(+), 80 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0687176..3465b0d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: false language: php php: - - '7.0' + - '7.1' install: - composer install script: diff --git a/composer.json b/composer.json index b4f4bfa..66d9963 100644 --- a/composer.json +++ b/composer.json @@ -29,8 +29,8 @@ ], "require-dev": { - "phpunit/phpunit": "*", - "phpunit/dbunit": "*", + "phpunit/phpunit": "^5.0", + "phpunit/dbunit": "^2.0", "block8/php-docblock-checker": "2.0.0" } } diff --git a/composer.lock b/composer.lock index e5f72c9..ce37b74 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" ], - "content-hash": "ce7af760f4497e0fc967f624e5e6cfcf", + "content-hash": "d2c6e897f43e09ee36ace2b4205b56e2", "packages": [ { "name": "behat/transliterator", @@ -2035,29 +2035,32 @@ }, { "name": "phpunit/dbunit", - "version": "3.0.0", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/dbunit.git", - "reference": "f2f8bec1d6ad7ad0bcdb47c1ed56d9d42d3e39ab" + "reference": "5c35d74549c21ba55d0ea74ba89d191a51f8cf25" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/dbunit/zipball/f2f8bec1d6ad7ad0bcdb47c1ed56d9d42d3e39ab", - "reference": "f2f8bec1d6ad7ad0bcdb47c1ed56d9d42d3e39ab", + "url": "https://api.github.com/repos/sebastianbergmann/dbunit/zipball/5c35d74549c21ba55d0ea74ba89d191a51f8cf25", + "reference": "5c35d74549c21ba55d0ea74ba89d191a51f8cf25", "shasum": "" }, "require": { "ext-pdo": "*", "ext-simplexml": "*", - "php": "^7.0", - "phpunit/phpunit": "^6.0", - "symfony/yaml": "^3.0" + "php": "^5.4 || ^7.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0", + "symfony/yaml": "^2.1 || ^3.0" }, + "bin": [ + "dbunit" + ], "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -2072,47 +2075,47 @@ "authors": [ { "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", + "email": "sb@sebastian-bergmann.de", "role": "lead" } ], - "description": "PHPUnit extension for database interaction testing", + "description": "DbUnit port for PHP/PHPUnit to support database interaction testing.", "homepage": "https://github.com/sebastianbergmann/dbunit/", "keywords": [ "database", "testing", "xunit" ], - "time": "2017-02-03T08:50:36+00:00" + "time": "2016-12-02T14:39:14+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "5.0.2", + "version": "4.0.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "531553c4795a1df54114342d68ca337d5d81c8a0" + "reference": "09e2277d14ea467e5a984010f501343ef29ffc69" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/531553c4795a1df54114342d68ca337d5d81c8a0", - "reference": "531553c4795a1df54114342d68ca337d5d81c8a0", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/09e2277d14ea467e5a984010f501343ef29ffc69", + "reference": "09e2277d14ea467e5a984010f501343ef29ffc69", "shasum": "" }, "require": { "ext-dom": "*", "ext-xmlwriter": "*", - "php": "^7.0", + "php": "^5.6 || ^7.0", "phpunit/php-file-iterator": "^1.3", "phpunit/php-text-template": "^1.2", - "phpunit/php-token-stream": "^1.4.11 || ^2.0", + "phpunit/php-token-stream": "^1.4.2 || ^2.0", "sebastian/code-unit-reverse-lookup": "^1.0", - "sebastian/environment": "^2.0", - "sebastian/version": "^2.0" + "sebastian/environment": "^1.3.2 || ^2.0", + "sebastian/version": "^1.0 || ^2.0" }, "require-dev": { - "ext-xdebug": "^2.5", - "phpunit/phpunit": "^6.0" + "ext-xdebug": "^2.1.4", + "phpunit/phpunit": "^5.7" }, "suggest": { "ext-xdebug": "^2.5.1" @@ -2120,7 +2123,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "5.0.x-dev" + "dev-master": "4.0.x-dev" } }, "autoload": { @@ -2146,7 +2149,7 @@ "testing", "xunit" ], - "time": "2017-03-01T09:14:18+00:00" + "time": "2017-03-01T09:12:17+00:00" }, { "name": "phpunit/php-file-iterator", @@ -2336,16 +2339,16 @@ }, { "name": "phpunit/phpunit", - "version": "6.0.8", + "version": "5.7.15", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "47ee3fa1bca5c50f1d25105201eb20df777bd7b6" + "reference": "b99112aecc01f62acf3d81a3f59646700a1849e5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/47ee3fa1bca5c50f1d25105201eb20df777bd7b6", - "reference": "47ee3fa1bca5c50f1d25105201eb20df777bd7b6", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/b99112aecc01f62acf3d81a3f59646700a1849e5", + "reference": "b99112aecc01f62acf3d81a3f59646700a1849e5", "shasum": "" }, "require": { @@ -2354,33 +2357,33 @@ "ext-libxml": "*", "ext-mbstring": "*", "ext-xml": "*", - "myclabs/deep-copy": "^1.3", - "php": "^7.0", + "myclabs/deep-copy": "~1.3", + "php": "^5.6 || ^7.0", "phpspec/prophecy": "^1.6.2", - "phpunit/php-code-coverage": "^5.0", - "phpunit/php-file-iterator": "^1.4", - "phpunit/php-text-template": "^1.2", + "phpunit/php-code-coverage": "^4.0.4", + "phpunit/php-file-iterator": "~1.4", + "phpunit/php-text-template": "~1.2", "phpunit/php-timer": "^1.0.6", - "phpunit/phpunit-mock-objects": "^4.0", - "sebastian/comparator": "^1.2.4 || ^2.0", - "sebastian/diff": "^1.2", - "sebastian/environment": "^2.0", - "sebastian/exporter": "^2.0 || ^3.0", - "sebastian/global-state": "^1.1 || ^2.0", - "sebastian/object-enumerator": "^2.0 || ^3.0", - "sebastian/resource-operations": "^1.0", - "sebastian/version": "^2.0" + "phpunit/phpunit-mock-objects": "^3.2", + "sebastian/comparator": "^1.2.4", + "sebastian/diff": "~1.2", + "sebastian/environment": "^1.3.4 || ^2.0", + "sebastian/exporter": "~2.0", + "sebastian/global-state": "^1.1", + "sebastian/object-enumerator": "~2.0", + "sebastian/resource-operations": "~1.0", + "sebastian/version": "~1.0.3|~2.0", + "symfony/yaml": "~2.1|~3.0" }, "conflict": { - "phpdocumentor/reflection-docblock": "3.0.2", - "phpunit/dbunit": "<3.0" + "phpdocumentor/reflection-docblock": "3.0.2" }, "require-dev": { "ext-pdo": "*" }, "suggest": { "ext-xdebug": "*", - "phpunit/php-invoker": "^1.1" + "phpunit/php-invoker": "~1.1" }, "bin": [ "phpunit" @@ -2388,7 +2391,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "6.0.x-dev" + "dev-master": "5.7.x-dev" } }, "autoload": { @@ -2414,33 +2417,33 @@ "testing", "xunit" ], - "time": "2017-03-02T15:24:03+00:00" + "time": "2017-03-02T15:22:43+00:00" }, { "name": "phpunit/phpunit-mock-objects", - "version": "4.0.0", + "version": "3.4.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "3819745c44f3aff9518fd655f320c4535d541af7" + "reference": "3ab72b65b39b491e0c011e2e09bb2206c2aa8e24" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/3819745c44f3aff9518fd655f320c4535d541af7", - "reference": "3819745c44f3aff9518fd655f320c4535d541af7", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/3ab72b65b39b491e0c011e2e09bb2206c2aa8e24", + "reference": "3ab72b65b39b491e0c011e2e09bb2206c2aa8e24", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", - "php": "^7.0", + "php": "^5.6 || ^7.0", "phpunit/php-text-template": "^1.2", - "sebastian/exporter": "^2.0" + "sebastian/exporter": "^1.2 || ^2.0" }, "conflict": { - "phpunit/phpunit": "<6.0" + "phpunit/phpunit": "<5.4.0" }, "require-dev": { - "phpunit/phpunit": "^6.0" + "phpunit/phpunit": "^5.4" }, "suggest": { "ext-soap": "*" @@ -2448,7 +2451,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.0.x-dev" + "dev-master": "3.2.x-dev" } }, "autoload": { @@ -2473,7 +2476,7 @@ "mock", "xunit" ], - "time": "2017-02-02T10:36:38+00:00" + "time": "2016-12-08T20:27:08+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", diff --git a/src/ActionGroup.php b/src/ActionGroup.php index 173e714..37fea40 100644 --- a/src/ActionGroup.php +++ b/src/ActionGroup.php @@ -77,6 +77,10 @@ class ActionGroup $this->actions = $actions; } + /** + * Adds a single action to the list of actions. + * @param Action $action + */ public function addAction(Action $action) { $this->actions[] = $action; diff --git a/src/Game.php b/src/Game.php index 49ecee8..836b692 100644 --- a/src/Game.php +++ b/src/Game.php @@ -6,17 +6,11 @@ namespace LotGD\Core; use Doctrine\ORM\EntityManagerInterface; use Monolog\Logger; -use LotGD\Core\Models\ { - Character, - Viewpoint, - Scene, - SceneConnection +use LotGD\Core\Models\{ + Character, SceneConnectable, Viewpoint, Scene, SceneConnection }; use LotGD\Core\Exceptions\ { - ActionNotFoundException, - CharacterNotFoundException, - InvalidConfigurationException, - SceneNotFoundException + ActionNotFoundException, CharacterNotFoundException, InvalidConfigurationException, SceneNotFoundException }; /** @@ -247,7 +241,9 @@ class Game ActionGroup::DefaultGroup => new ActionGroup(ActionGroup::DefaultGroup, '', 0), ]; - $scene->getConnections()->map(function(SceneConnection $connection) use ($scene, $actionGroups) { + // Iterates through all connections and adds an action to the connected scene to the action group. If the connection + // belongs to a new connection Group, it creates a new ActionGroup. + $scene->getConnections()->map(function(SceneConnection $connection) use ($scene, &$actionGroups) { if ($connection->getOutgoingScene() === $scene) { // current scene is outgoing, use incoming. $connectedScene = $connection->getIncomingScene(); @@ -256,6 +252,12 @@ class Game // current scene is not outgoing, thus incoming, use outgoing. $connectedScene = $connection->getOutgoingScene(); $connectionGroupName = $connection->getIncomingConnectionGroupName(); + + // Check if the connection is unidirectional - if yes, the current scene (incoming in this branch) cannot + // connect to the outgoing scene. + if ($connection->isDirectionality(SceneConnectable::Unidirectional)) { + return; + } } $this->getLogger()->addDebug(" Adding navigation action for child sceneId={$connectedScene->getId()}"); @@ -268,20 +270,15 @@ class Game $actionGroups[$connectionGroupName]->addAction($action); } else { $connectionGroup = $scene->getConnectionGroup($connectionGroupName); - $actionGroup = new ActionGroup($connectionGroupName->getName(), $connectionGroupName->getTitle(), 0); + $actionGroup = new ActionGroup($connectionGroupName, $connectionGroup->getTitle(), 0); $actionGroup->addAction($action); $actionGroups[$connectionGroupName] = $actionGroup; } } }); - /*$as = array_map(function ($c) { - $id = $c->getId(); - $this->getLogger()->addDebug(" Adding navigation action for child sceneId={$id}"); - return new Action($c->getId()); - }, $scene->getChildren()->toArray());*/ - //$defaultGroup->setActions($as); - //$count = count($as); + + // Logging $counts = implode(", ", array_map(function($k, $v) { return $k .count($v); }, array_keys($actionGroups), array_values($actionGroups))); diff --git a/src/Models/Scene.php b/src/Models/Scene.php index fc69cde..8b32b75 100644 --- a/src/Models/Scene.php +++ b/src/Models/Scene.php @@ -210,8 +210,10 @@ class Scene implements CreateableInterface, SceneConnectable public function getConnections(): Collection { return new ArrayCollection( - $this->outgoingConnections->toArray(), - $this->incomingConnections->toArray() + array_merge( + $this->outgoingConnections->toArray(), + $this->incomingConnections->toArray() + ) ); } diff --git a/src/Models/SceneConnection.php b/src/Models/SceneConnection.php index a9c23d9..ffe581a 100644 --- a/src/Models/SceneConnection.php +++ b/src/Models/SceneConnection.php @@ -111,4 +111,18 @@ class SceneConnection { return $this->incomingScene; } + + /** + * Returns if the directionality of this entity is as given as the first parameter. + * @param int $directionality + * @return bool + */ + public function isDirectionality(int $directionality): bool + { + if ($this->directionality === $directionality) { + return true; + } else { + return false; + } + } } diff --git a/src/Models/SceneConnectionGroup.php b/src/Models/SceneConnectionGroup.php index 99e2f9c..12a91e4 100644 --- a/src/Models/SceneConnectionGroup.php +++ b/src/Models/SceneConnectionGroup.php @@ -72,6 +72,15 @@ class SceneConnectionGroup implements SceneConnectable return $this->name; } + /** + * Returns the title of this connection group. + * @return string + */ + public function getTitle(): string + { + return $this->title; + } + /** * @inheritDoc */ diff --git a/tests/GameTest.php b/tests/GameTest.php index 483adc6..0e444d1 100644 --- a/tests/GameTest.php +++ b/tests/GameTest.php @@ -190,4 +190,63 @@ class GameTest extends CoreModelTestCase $v = $this->g->getViewpoint(); $this->assertSame($s->getTemplate(), $v->getTemplate()); } + + public function testIfActionsAreAddedAsExpected() + { + $viewpointToArray = function(Viewpoint $v) { + $returnTree = []; + foreach ($v->getActionGroups() as $actionGroup) { + $returnTree[$actionGroup->getId()] = []; + + foreach ($actionGroup->getActions() as $action) { + $returnTree[$actionGroup->getId()][] = $action->getDestinationSceneId(); + } + } + + return [$v->getTitle(), $returnTree]; + }; + + $sortedValues = function(array $array) { + $values = array_values($array); + sort($values); + return $values; + }; + + $c = $this->getEntityManager()->getRepository(Character::class)->find(3); + $this->g->setCharacter($c); + + $v0 = $this->g->getViewpoint(); + $this->g->takeAction($v0->getActionGroups()[0]->getActions()[2]->getId()); + + $v1 = $this->g->getViewpoint(); + $this->assertSame([ + "Parent Scene", + [ + ActionGroup::DefaultGroup => [1], + "lotgd/tests/none/child1" => [5], + "lotgd/tests/none/child2" => [6], + ActionGroup::HiddenGroup => [], + ] + ], $viewpointToArray($v1)); + + $this->g->takeAction($v1->getActionGroups()[1]->getActions()[0]->getId()); + $v2 = $this->g->getviewpoint(); + $this->assertSame([ + "Child Scene 1", + [ + ActionGroup::DefaultGroup => [6, 4], + ActionGroup::HiddenGroup => [], + ] + ], $viewpointToArray($v2)); + + $this->g->takeAction($v1->getActionGroups()[0]->getActions()[0]->getId()); + $v3 = $this->g->getviewpoint(); + $this->assertSame([ + "Child Scene 2", + [ + ActionGroup::DefaultGroup => [4], + ActionGroup::HiddenGroup => [], + ] + ], $viewpointToArray($v3)); + } } diff --git a/tests/datasets/game.yml b/tests/datasets/game.yml index 7e682c9..12e6446 100644 --- a/tests/datasets/game.yml +++ b/tests/datasets/game.yml @@ -36,10 +36,55 @@ scenes: title: "The Weaponry" description: "This is the place where you can buy awesome weapons" template: "lotgd/tests/weaponry" + - + id: 4 + title: "Parent Scene" + description: "This is a parent scene that connects to two children." + template: "lotgd/tests/none" + - + id: 5 + title: "Child Scene 1" + description: "This is a parent scene that connects to two children." + template: "lotgd/tests/none" + - + id: 6 + title: "Child Scene 2" + description: "This is a parent scene that connects to two children." + template: "lotgd/tests/none" +scene_connection_groups: + - + scene_id: 4 + name: "lotgd/tests/none/child1" + title: "Child 1" + - + scene_id: 4 + name: "lotgd/tests/none/child2" + title: "Child 2" scene_connections: - outgoing_scene_id: 1 incoming_scene_id: 2 + directionality: 0 - outgoing_scene_id: 1 incoming_scene_id: 3 + directionality: 0 + - + outgoing_scene_id: 1 + incoming_scene_id: 4 + directionality: 0 + - + outgoing_scene_id: 4 + incoming_scene_id: 5 + outgoingConnectionGroupName: "lotgd/tests/none/child1" + directionality: 0 + - + outgoing_scene_id: 4 + incoming_scene_id: 6 + outgoingConnectionGroupName: "lotgd/tests/none/child2" + directionality: 0 + - + outgoing_scene_id: 5 + incoming_scene_id: 6 + directionality: 1 +