From 8a4ac34a967d2216ba7123788a0deb6f413b66f6 Mon Sep 17 00:00:00 2001 From: p8pepe Date: Thu, 3 Sep 2015 13:28:02 +0200 Subject: [PATCH] nearest-stations api --- behat.yml.dist | 10 + composer.json | 6 +- composer.lock | 231 +++++++++++++++++- .../ApiPublicTransportUserContext.php | 83 +++++++ .../bootstrap/PublicTransportUserContext.php | 6 +- features/searchNearestDockingStation.feature | 1 + src/AppBundle/Controller/ApiController.php | 25 ++ src/Cocoders/CityBike/DockingStations.php | 2 + .../DockingStationsFileRepository.php | 5 + .../InMemory/CityBike/DockingStations.php | 5 + 10 files changed, 370 insertions(+), 4 deletions(-) create mode 100644 features/bootstrap/ApiPublicTransportUserContext.php create mode 100644 src/AppBundle/Controller/ApiController.php diff --git a/behat.yml.dist b/behat.yml.dist index f19038d..2f72f90 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -2,6 +2,11 @@ default: extensions: RMiller\BehatSpec\BehatExtension: path: bin/phpspec + Behat\Symfony2Extension: ~ + Behat\MinkExtension: + default_session: 'symfony2' + sessions: + symfony2: { symfony2: ~ } suites: administrator: contexts: [ AdministratorContext ] @@ -9,3 +14,8 @@ default: public_transport_user: contexts: [ PublicTransportUserContext ] filters: { role: public transport user } + api_public_transport_user: + contexts: + - ApiPublicTransportUserContext: + dockingStations: @cocoders.repository.docking_station + filters: { role: public transport user, tags: api } diff --git a/composer.json b/composer.json index fd09d23..7867a64 100755 --- a/composer.json +++ b/composer.json @@ -28,7 +28,11 @@ "behat/behat": "~3.0", "phpspec/phpspec": "~2.2", "rmiller/behat-spec": "~0.2", - "sensio/generator-bundle": "~2.3" + "sensio/generator-bundle": "~2.3", + "behat/mink": "~1.5.0", + "behat/mink-extension": "~2.0.0", + "behat/symfony2-extension": "~2.0.0", + "behat/mink-browserkit-driver": "~1.1.0" }, "scripts": { "post-root-package-install": [ diff --git a/composer.lock b/composer.lock index 9ed26a4..1d502e0 100755 --- 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": "23ab04f9081ff7066771bd79597b39af", + "hash": "8a3d3d2c7619cedfd2dde31e64056de0", "packages": [ { "name": "doctrine/annotations", @@ -1978,6 +1978,235 @@ ], "time": "2014-06-06 01:24:32" }, + { + "name": "behat/mink", + "version": "v1.5.0", + "source": { + "type": "git", + "url": "https://github.com/minkphp/Mink.git", + "reference": "0769e6d9726c140a54dbf827a438c0f9912749fe" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/minkphp/Mink/zipball/0769e6d9726c140a54dbf827a438c0f9912749fe", + "reference": "0769e6d9726c140a54dbf827a438c0f9912749fe", + "shasum": "" + }, + "require": { + "php": ">=5.3.1", + "symfony/css-selector": "~2.0" + }, + "suggest": { + "behat/mink-browserkit-driver": "extremely fast headless driver for Symfony\\Kernel-based apps (Sf2, Silex)", + "behat/mink-goutte-driver": "fast headless driver for any app without JS emulation", + "behat/mink-selenium2-driver": "slow, but JS-enabled driver for any app (requires Selenium2)", + "behat/mink-zombie-driver": "fast and JS-enabled headless driver for any app (requires node.js)" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-develop": "1.5.x-dev" + } + }, + "autoload": { + "psr-0": { + "Behat\\Mink": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + } + ], + "description": "Web acceptance testing framework for PHP 5.3", + "homepage": "http://mink.behat.org/", + "keywords": [ + "browser", + "testing", + "web" + ], + "time": "2013-04-13 23:39:27" + }, + { + "name": "behat/mink-browserkit-driver", + "version": "v1.1.0", + "source": { + "type": "git", + "url": "https://github.com/minkphp/MinkBrowserKitDriver.git", + "reference": "63960c8fcad4529faad1ff33e950217980baa64c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/minkphp/MinkBrowserKitDriver/zipball/63960c8fcad4529faad1ff33e950217980baa64c", + "reference": "63960c8fcad4529faad1ff33e950217980baa64c", + "shasum": "" + }, + "require": { + "behat/mink": "~1.5.0", + "php": ">=5.3.1", + "symfony/browser-kit": "~2.0", + "symfony/dom-crawler": "~2.0" + }, + "require-dev": { + "silex/silex": "@dev" + }, + "type": "mink-driver", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "psr-0": { + "Behat\\Mink\\Driver": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + } + ], + "description": "Symfony2 BrowserKit driver for Mink framework", + "homepage": "http://mink.behat.org/", + "keywords": [ + "Mink", + "Symfony2", + "browser", + "testing" + ], + "time": "2013-04-13 23:46:30" + }, + { + "name": "behat/mink-extension", + "version": "v2.0.1", + "source": { + "type": "git", + "url": "https://github.com/Behat/MinkExtension.git", + "reference": "06a4cb56614b047d8d15ea5cd392d19fd3d856e8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Behat/MinkExtension/zipball/06a4cb56614b047d8d15ea5cd392d19fd3d856e8", + "reference": "06a4cb56614b047d8d15ea5cd392d19fd3d856e8", + "shasum": "" + }, + "require": { + "behat/behat": "~3.0,>=3.0.5", + "behat/mink": "~1.5", + "php": ">=5.3.2", + "symfony/config": "~2.2" + }, + "require-dev": { + "behat/mink-goutte-driver": "~1.0", + "phpspec/phpspec": "~2.0" + }, + "type": "behat-extension", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Behat\\MinkExtension": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christophe Coevoet", + "email": "stof@notk.org" + }, + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com" + } + ], + "description": "Mink extension for Behat", + "homepage": "http://extensions.behat.org/mink", + "keywords": [ + "browser", + "gui", + "test", + "web" + ], + "time": "2014-09-23 10:59:27" + }, + { + "name": "behat/symfony2-extension", + "version": "v2.0.0", + "source": { + "type": "git", + "url": "https://github.com/Behat/Symfony2Extension.git", + "reference": "815ec1f6764fca0882cd744731c48edb82dea204" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Behat/Symfony2Extension/zipball/815ec1f6764fca0882cd744731c48edb82dea204", + "reference": "815ec1f6764fca0882cd744731c48edb82dea204", + "shasum": "" + }, + "require": { + "behat/behat": "~3.0,>=3.0.4", + "php": ">=5.3.3", + "symfony/framework-bundle": "~2.0" + }, + "require-dev": { + "behat/mink-browserkit-driver": "~1.0", + "behat/mink-extension": "~2.0@dev", + "phpspec/phpspec": "~2.0", + "phpunit/phpunit": "~4.0", + "symfony/symfony": "~2.1" + }, + "type": "behat-extension", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Behat\\Symfony2Extension": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christophe Coevoet", + "email": "stof@notk.org" + }, + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com" + } + ], + "description": "Symfony2 framework extension for Behat", + "homepage": "http://behat.org", + "keywords": [ + "BDD", + "framework", + "symfony" + ], + "time": "2014-09-04 22:10:45" + }, { "name": "behat/transliterator", "version": "v1.0.1", diff --git a/features/bootstrap/ApiPublicTransportUserContext.php b/features/bootstrap/ApiPublicTransportUserContext.php new file mode 100644 index 0000000..84640eb --- /dev/null +++ b/features/bootstrap/ApiPublicTransportUserContext.php @@ -0,0 +1,83 @@ +dockingStations = $dockingStations; + } + + public function iAmSearchingNearestBikeDockingStationsFromMyPosisitonWhichIs($positionString) + { + $position = \Cocoders\CityBike\Position::fromString($positionString); + + $this->mink->getSession()->visit('/nearest-stations/'.urlencode($position->getLat()).'/'.urlencode($position->getLong())); + } + + public function iShouldSeeSuchNearestDockingStations(TableNode $table) + { + $this->mink->assertSession()->statusCodeEquals(200); + $content = $this->mink->getSession()->getPage()->getContent(); + $stations = json_decode($content, true); + + if (!$stations) { + throw new \Exception('There is no stations'); + } + + foreach ($stations as $key => $foundDockingStation) { + if ($foundDockingStation['distance'] != $table->getHash()[$key]['distance (km)']) { + throw new \Exception('Distance does not match'); + } + if ($foundDockingStation['availableBikes'] != $table->getHash()[$key]['available bikes']) { + throw new \Exception('Available does not match'); + } + if ($foundDockingStation['lat'] != $table->getHash()[$key]['lat']) { + throw new \Exception('lat does not match'); + } + if ($foundDockingStation['long'] != $table->getHash()[$key]['long']) { + throw new \Exception('long does not match'); + } + if ($foundDockingStation['name'] != $table->getHash()[$key]['name']) { + throw new \Exception('name does not match'); + } + } + } + + /** + * Sets Mink instance. + * + * @param \Behat\Mink\Mink $mink Mink session manager + */ + public function setMink(\Behat\Mink\Mink $mink) + { + $this->mink = $mink; + } + + /** + * Sets parameters provided for Mink. + * + * @param array $parameters + */ + public function setMinkParameters(array $parameters) + { + $this->minkParameters = $parameters; + } +} diff --git a/features/bootstrap/PublicTransportUserContext.php b/features/bootstrap/PublicTransportUserContext.php index 40eb233..c494e8e 100755 --- a/features/bootstrap/PublicTransportUserContext.php +++ b/features/bootstrap/PublicTransportUserContext.php @@ -14,8 +14,8 @@ class PublicTransportUserContext implements Context, SnippetAcceptingContext { /** @var \Cocoders\CityBike\DockingStations */ - private $dockingStations; - private $foundDockingStations; + protected $dockingStations; + protected $foundDockingStations; /** * @var FoundDockingStation[] */ @@ -39,6 +39,8 @@ public function __construct() */ public function thereAreSuchDockingStations(TableNode $table) { + $this->dockingStations->removeAll(); + foreach ($table->getHash() as $row) { $dockingStation = new \Cocoders\CityBike\DockingStation( $row['id'], diff --git a/features/searchNearestDockingStation.feature b/features/searchNearestDockingStation.feature index 1d131e8..597d21c 100644 --- a/features/searchNearestDockingStation.feature +++ b/features/searchNearestDockingStation.feature @@ -11,6 +11,7 @@ Feature: Searching docking station | 2 | Toruń Św Katarzyny | 53.0132672 | 18.613699 | 2 | | 3 | Toruń Plac Rapackiego | 53.0099343 | 18.6010726 | 0 | + @api Scenario: Searching next docking station When I am searching nearest bike docking stations from my posisiton which is "53.03531,18.598338" Then I should see such nearest docking stations: diff --git a/src/AppBundle/Controller/ApiController.php b/src/AppBundle/Controller/ApiController.php new file mode 100644 index 0000000..23e09a8 --- /dev/null +++ b/src/AppBundle/Controller/ApiController.php @@ -0,0 +1,25 @@ +search(new Position($lat, $long), $this->get('cocoders.repository.docking_station')->findAll()); + + return new JsonResponse($foundDockingStations); + } +} diff --git a/src/Cocoders/CityBike/DockingStations.php b/src/Cocoders/CityBike/DockingStations.php index a9dfc85..8d5dd75 100755 --- a/src/Cocoders/CityBike/DockingStations.php +++ b/src/Cocoders/CityBike/DockingStations.php @@ -19,4 +19,6 @@ public function findAll(); * @return DockingStation */ public function find($id); + + public function removeAll(); } diff --git a/src/Cocoders/File/CityBike/DockingStationsFileRepository.php b/src/Cocoders/File/CityBike/DockingStationsFileRepository.php index 0f0d73d..99625bb 100644 --- a/src/Cocoders/File/CityBike/DockingStationsFileRepository.php +++ b/src/Cocoders/File/CityBike/DockingStationsFileRepository.php @@ -45,4 +45,9 @@ public function find($id) { return $this->stationsRepo->findById($id); } + + public function removeAll() + { + $this->stationsRepo->clear(); + } } diff --git a/src/Cocoders/InMemory/CityBike/DockingStations.php b/src/Cocoders/InMemory/CityBike/DockingStations.php index ac1dd7a..5a539eb 100755 --- a/src/Cocoders/InMemory/CityBike/DockingStations.php +++ b/src/Cocoders/InMemory/CityBike/DockingStations.php @@ -39,4 +39,9 @@ public function find($id) } return null; } + + public function removeAll() + { + $this->stations = []; + } }