Skip to content

Commit 013e7b6

Browse files
committed
Use data objects that are documented for the transaction.
This allows for in-IDE documentation and static analysis of the transaction objects on the hooks.
1 parent 89ac3d4 commit 013e7b6

6 files changed

Lines changed: 268 additions & 1 deletion

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Dredd\DataObjects;
4+
5+
class ExpectedResponse
6+
{
7+
/** @var string $statusCode */
8+
public $statusCode;
9+
10+
/**
11+
* Keys are HTTP header names, values are HTTP header contents
12+
*
13+
* @var object
14+
*/
15+
public $headers;
16+
17+
/** @var string $body */
18+
public $body;
19+
20+
/**
21+
* JSON Schema of the response body
22+
*
23+
* @var object $bodySchema
24+
*/
25+
public $bodySchema;
26+
27+
public function __construct($expected)
28+
{
29+
$this->statusCode = $expected->statusCode;
30+
$this->headers = $expected->headers;
31+
$this->body = $expected->body;
32+
$this->bodySchema = $expected->bodySchema;
33+
}
34+
}

src/DataObjects/Origin.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Dredd\DataObjects;
4+
5+
/**
6+
* Reference to the transaction definition in the original API description document.
7+
* (See also {@link https://github.com/apiaryio/dredd-transactions#user-content-data-structures Dredd Transactions})
8+
*/
9+
class Origin
10+
{
11+
/** @var string $filename */
12+
public $filename;
13+
14+
/** @var string $apiName */
15+
public $apiName;
16+
17+
/** @var string $resourceGroupName */
18+
public $resourceGroupName;
19+
20+
/** @var string $resourceName */
21+
public $resourceName;
22+
23+
/** @var string $actionName */
24+
public $actionName;
25+
26+
/** @var string $exampleName */
27+
public $exampleName;
28+
29+
public function __construct($origin)
30+
{
31+
$this->filename = $origin->filename;
32+
$this->apiName = $origin->apiName;
33+
$this->resourceGroupName = $origin->resourceGroupName;
34+
$this->resourceName = $origin->resourceName;
35+
$this->actionName = $origin->actionName;
36+
$this->exampleName = $origin->exampleName;
37+
}
38+
}

src/DataObjects/RealResponse.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Dredd\DataObjects;
4+
5+
class RealResponse
6+
{
7+
/** @var string $statusCode */
8+
public $statusCode;
9+
10+
/**
11+
* Keys are HTTP header names, values are HTTP header contents
12+
*
13+
* @var object
14+
*/
15+
public $headers;
16+
17+
/** @var string $body */
18+
public $body;
19+
20+
/**
21+
* - `utf-8` (string) - indicates `body` contains a textual content encoded in UTF-8
22+
* - `base64` (string) - indicates `body` contains a binary content encoded in Base64
23+
*
24+
* @var string $bodyEncoding
25+
* @psalm-var 'utf-8'|'base64' $bodyEncoding
26+
*/
27+
public $bodyEncoding;
28+
29+
public function __construct($expected)
30+
{
31+
$this->statusCode = $expected->statusCode;
32+
$this->headers = $expected->headers;
33+
$this->body = $expected->body;
34+
$this->bodyEncoding = $expected->bodyEncoding;
35+
}
36+
}

src/DataObjects/Request.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Dredd\DataObjects;
4+
5+
class Request
6+
{
7+
/** @var string $body */
8+
public $body;
9+
10+
/**
11+
* Can be manually set in {@link https://dredd.org/en/latest/hooks/index.html#hooks hooks}
12+
* - `utf-8` (string) - indicates `body` contains a textual content encoded in UTF-8
13+
* - `base64` (string) - indicates `body` contains a binary content encoded in Base64
14+
*
15+
* @var string $bodyEncoding
16+
* @psalm-var 'utf-8'|'base64' $bodyEncoding
17+
*/
18+
public $bodyEncoding;
19+
20+
/**
21+
* Keys are HTTP header names, values are HTTP header contents
22+
* @var object $headers
23+
*/
24+
public $headers;
25+
26+
/**
27+
* Request URI as it was written in API description
28+
*
29+
* @var string $uri
30+
*/
31+
public $uri;
32+
33+
/** @var string $method */
34+
public $method;
35+
36+
public function __construct($request)
37+
{
38+
$this->body = $request->body;
39+
$this->bodyEncoding = $request->bodyEncoding;
40+
$this->headers = $request->headers;
41+
$this->uri = $request->uri;
42+
$this->method = $request->method;
43+
}
44+
}

src/DataObjects/Transaction.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Dredd\DataObjects;
4+
5+
/**
6+
* Transaction object is passed as a first argument to
7+
* {@link https://dredd.org/en/latest/hooks/index.html#hooks hook functions}
8+
* and is one of the main public interfaces in Dredd.
9+
*
10+
* @link https://dredd.org/en/latest/data-structures.html#transaction-object
11+
*/
12+
class Transaction
13+
{
14+
/** @var string $id */
15+
public $id;
16+
17+
/**
18+
* Reference to the transaction definition in the original API description document
19+
* (See also {@link https://github.com/apiaryio/dredd-transactions#user-content-data-structures Dredd Transactions})
20+
*
21+
* @var string $name
22+
*/
23+
public $name;
24+
25+
/** @var string $host */
26+
public $host;
27+
28+
/** @var int $port */
29+
public $port;
30+
31+
/** @var string $protocol */
32+
public $protocol;
33+
34+
/**
35+
* Expanded URI Template with parameters (if any) used for the HTTP request Dredd performs to the tested server
36+
*
37+
* @link https://tools.ietf.org/html/rfc6570.html
38+
* @var string $fullPath
39+
*/
40+
public $fullPath;
41+
42+
/**
43+
* Can be set to `true` and the transaction will be skipped
44+
*
45+
* @var bool $skip
46+
*/
47+
public $skip;
48+
49+
/**
50+
* Can be set to `true` or string and the transaction will fail
51+
* - (string) - failure message with details why the transaction failed
52+
* - (boolean)
53+
* @var bool|string $fail
54+
*/
55+
public $fail;
56+
57+
/** @var Origin $origin */
58+
public $origin;
59+
60+
/**
61+
* Test data passed to Dredd’s reporters
62+
*
63+
* @link https://dredd.org/en/latest/data-structures.html#transaction-test
64+
* @var object
65+
*/
66+
public $test;
67+
68+
/**
69+
* Transaction runtime errors
70+
*
71+
* Whenever an exception occurs during a test run it’s being recorded under the errors property of the test.
72+
*
73+
* @link https://dredd.org/en/latest/data-structures.html#test-runtime-error
74+
* @var object
75+
*/
76+
public $errors;
77+
78+
/**
79+
* Transaction result equals to the result of the
80+
* {@link https://github.com/apiaryio/gavel.js Gavel} validation library.
81+
*
82+
* @link https://dredd.org/en/latest/data-structures.html#transaction-results
83+
* @var object
84+
*/
85+
public $results;
86+
87+
/**
88+
* The HTTP request Dredd performs to the tested server, taken from the API description
89+
* @var Request $request
90+
*/
91+
public $request;
92+
93+
/** @var ExpectedResponse $expected */
94+
public $expected;
95+
96+
/** @var RealResponse $real */
97+
public $real;
98+
99+
public function __construct($transaction)
100+
{
101+
$this->id = $transaction->id;
102+
$this->name = $transaction->name;
103+
$this->host = $transaction->host;
104+
$this->port = $transaction->port;
105+
$this->protocol = $transaction->protocol;
106+
$this->fullPath = $transaction->fullPath;
107+
$this->skip = $transaction->skip;
108+
$this->fail = $transaction->fail;
109+
$this->origin = new Origin($transaction->origin);
110+
$this->request = new Request($transaction->request);
111+
$this->expected = new ExpectedResponse($transaction->expected);
112+
$this->real = new RealResponse($transaction->real);
113+
}
114+
}

src/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Dredd;
22

33
use UnexpectedValueException;
4+
use Dredd\DataObjects\Transaction;
45

56
class Server
67
{
@@ -75,7 +76,7 @@ public function run($force = false)
7576
public function processMessage($message, $client)
7677
{
7778
$event = $message->event;
78-
$data = $message->data;
79+
$data = new Transaction($message->data);
7980
$uuid = $message->uuid;
8081

8182
if ($event == "beforeEach") {

0 commit comments

Comments
 (0)