3
3
use Behat \Behat \Tester \Exception \PendingException ;
4
4
use Behat \Behat \Context \Context ;
5
5
use Behat \Gherkin \Node \PyStringNode ;
6
+ use Behat \Behat \Hook \Scope \BeforeScenarioScope ;
6
7
use Behat \Gherkin \Node \TableNode ;
7
8
use PascalDeVink \ShortUuid \ShortUuid ;
8
9
use Rezzza \RestApiBehatExtension \Rest \RestApiBrowser ;
@@ -20,41 +21,45 @@ class WebContext implements Context
20
21
21
22
private $ restApiBrowser ;
22
23
23
- public function __construct (RestApiBrowser $ restApiBrowser )
24
+ private $ jsonContext ;
25
+
26
+ public function __construct (EventSourcing \EventStore $ eventStore , RestApiBrowser $ restApiBrowser )
24
27
{
25
- $ serializer = new EventSourcing \EventSerializer (
26
- new Domain \EventMapping ,
27
- new Symfony \Component \Serializer \Serializer (
28
- [
29
- new Symfony \Component \Serializer \Normalizer \PropertyNormalizer (
30
- null ,
31
- new Symfony \Component \Serializer \NameConverter \CamelCaseToSnakeCaseNameConverter
32
- )
33
- ],
34
- [ new Symfony \Component \Serializer \Encoder \JsonEncoder ]
35
- )
36
- );
37
- $ this ->eventStore = new Adapters \FilesystemEventStore (
38
- __DIR__ .'/../../var/eventstore ' ,
39
- $ serializer ,
40
- new Ports \FileHelper
41
- );
28
+ $ this ->eventStore = $ eventStore ;
42
29
$ this ->restApiBrowser = $ restApiBrowser ;
43
30
$ this ->restApiBrowser ->setRequestHeader ('Content-Type ' , 'application/json ' );
44
31
$ this ->restApiBrowser ->setRequestHeader ('X-Show-Exception-Token ' , 't0kt0k ' );
45
32
$ this ->userId = ShortUuid::uuid4 ();
46
33
}
47
34
35
+ /**
36
+ * @BeforeScenario
37
+ */
38
+ public function gatherContexts (BeforeScenarioScope $ scope )
39
+ {
40
+ $ environment = $ scope ->getEnvironment ();
41
+
42
+ $ this ->jsonContext = $ environment ->getContext ('Rezzza\RestApiBehatExtension\Json\JsonContext ' );
43
+ }
44
+
45
+ /**
46
+ * @Transform :location
47
+ */
48
+ public function castLocation ($ location )
49
+ {
50
+ return Domain \Location::fromString ($ location );
51
+ }
52
+
48
53
/**
49
54
* @Given I registred my vehicle with platenumber :platenumber
50
55
*/
51
56
public function iRegistredMyVehicleWithPlatenumber ($ platenumber )
52
57
{
53
58
$ this ->eventStore ->commit (
54
- new EventSourcing \EventStream (
55
- new EventSourcing \EventStreamId ( ' vehicle-fleet_ ' .$ this ->userId ),
59
+ new EventSourcing \Stream (
60
+ new EventSourcing \StreamName ( ' vehicle_fleet- ' .$ this ->userId ),
56
61
new \ArrayIterator ([
57
- new Domain \VehicleWasRegistered ($ platenumber , $ this ->userId )
62
+ new Domain \VehicleWasRegistered ($ this ->userId , $ platenumber )
58
63
])
59
64
)
60
65
);
@@ -63,9 +68,17 @@ public function iRegistredMyVehicleWithPlatenumber($platenumber)
63
68
/**
64
69
* @When I park my vehicle with platenumber :platenumber at location :location
65
70
*/
66
- public function iParkMyVehicleWithPlatenumberAtLocation ($ platenumber , $ location )
71
+ public function iParkMyVehicleWithPlatenumberAtLocation ($ platenumber , Domain \ Location $ location )
67
72
{
68
- throw new PendingException ();
73
+ // Endpoint should be improve to follow REST
74
+ $ this ->restApiBrowser ->sendRequest (
75
+ 'POST ' ,
76
+ sprintf ('/users/%s/vehicles/location ' , $ this ->userId ),
77
+ json_encode ([
78
+ 'platenumber ' => $ platenumber ,
79
+ 'location ' => $ location ,
80
+ ])
81
+ );
69
82
}
70
83
71
84
/**
@@ -84,18 +97,28 @@ public function iRegisterMyVehicleWithPlatenumberDescribedAsInMyVehicleFleet($pl
84
97
}
85
98
86
99
/**
87
- * @Then the vehicle with platenumber :arg1 should be part of my vehicle fleet
100
+ * @Then the vehicle with platenumber :platenumber should be part of my vehicle fleet
88
101
*/
89
- public function theVehicleWithPlatenumberShouldBePartOfMyVehicleFleet ($ arg1 )
102
+ public function theVehicleWithPlatenumberShouldBePartOfMyVehicleFleet ($ platenumber )
90
103
{
91
- throw new PendingException ();
104
+ $ this ->restApiBrowser ->sendRequest (
105
+ 'GET ' ,
106
+ sprintf ('/users/%s/vehicles ' , $ this ->userId )
107
+ );
108
+ // Shoud iterate to be a better assertion
109
+ $ this ->jsonContext ->theJsonNodeShouldBeEqualTo ('vehicles[0].platenumber ' , $ platenumber );
92
110
}
93
111
94
112
/**
95
- * @Then the known location of my vehicle :arg1 should be :arg2
113
+ * @Then the known location of my vehicle :platenumber should be :location
96
114
*/
97
- public function theKnownLocationOfMyVehicleShouldBe ($ arg1 , $ arg2 )
115
+ public function theKnownLocationOfMyVehicleShouldBe ($ platenumber , Domain \ Location $ location )
98
116
{
99
- throw new PendingException ();
117
+ $ this ->restApiBrowser ->sendRequest (
118
+ 'GET ' ,
119
+ sprintf ('/users/%s/vehicles ' , $ this ->userId )
120
+ );
121
+ $ this ->jsonContext ->theJsonNodeShouldBeEqualTo ('vehicles[0].location.latitude ' , $ location ->getLatitude ());
122
+ $ this ->jsonContext ->theJsonNodeShouldBeEqualTo ('vehicles[0].location.longitude ' , $ location ->getLongitude ());
100
123
}
101
124
}
0 commit comments