diff --git a/Dockerfile b/Dockerfile index d3a6c8e..a37c889 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,3 +12,4 @@ RUN apt-get update \ && wget https://getcomposer.org/installer \ && chmod +x installer \ && php installer --install-dir=/usr/local/bin/ --filename=composer \ + && npm install -g dredd --no-optional diff --git a/features/failing_transaction.feature b/features/failing_transaction.feature index 8c76d83..8432f87 100644 --- a/features/failing_transaction.feature +++ b/features/failing_transaction.feature @@ -26,7 +26,7 @@ Feature: Failing a transaction flush(); }); """ - When I run `dredd ./apiary.apib http://localhost:4567 --server "nodejs server.js" --language php --hookfiles failedhook.php --loglevel debug` + When I run `dredd ./apiary.apib http://localhost:4567 --server "node server.js" --language php --hookfiles failedhook.php --loglevel debug` Then the exit status should be 1 And the output should contain: """ diff --git a/features/transaction_object.feature b/features/transaction_object.feature new file mode 100644 index 0000000..1affdba --- /dev/null +++ b/features/transaction_object.feature @@ -0,0 +1,32 @@ +Feature: Failing a transaction + + Background: + Given I have dredd-hooks-php installed + Given I have Dredd installed + And a file named "apiary.apib" with: + """ + # My Api + ## GET /message + + Response 200 (text/html) + """ + And a file "server.js" with a server responding on "http://localhost:4567/message" with "Hello World!" + + @announce + Scenario: + Given a file named "hook_transaction_object.php" with: + """ + + */ + public $headers; + + /** @var string $body */ + public $body; + + /** + * JSON Schema of the response body + * + * @var object $bodySchema + */ + public $bodySchema; + + public function __construct($expected) + { + $this->statusCode = $expected->statusCode; + $this->headers = (array) $expected->headers; + $this->body = $expected->body; + $this->bodySchema = $expected->bodySchema; + } +} diff --git a/src/DataObjects/Origin.php b/src/DataObjects/Origin.php new file mode 100644 index 0000000..18a585c --- /dev/null +++ b/src/DataObjects/Origin.php @@ -0,0 +1,38 @@ +filename = $origin->filename; + $this->apiName = $origin->apiName; + $this->resourceGroupName = $origin->resourceGroupName; + $this->resourceName = $origin->resourceName; + $this->actionName = $origin->actionName; + $this->exampleName = $origin->exampleName; + } +} diff --git a/src/DataObjects/RealResponse.php b/src/DataObjects/RealResponse.php new file mode 100644 index 0000000..7f0e5db --- /dev/null +++ b/src/DataObjects/RealResponse.php @@ -0,0 +1,36 @@ +statusCode = $expected->statusCode; + $this->headers = $expected->headers; + $this->body = $expected->body; + $this->bodyEncoding = $expected->bodyEncoding; + } +} diff --git a/src/DataObjects/Request.php b/src/DataObjects/Request.php new file mode 100644 index 0000000..e62b499 --- /dev/null +++ b/src/DataObjects/Request.php @@ -0,0 +1,44 @@ + $headers + */ + public $headers; + + /** + * Request URI as it was written in API description + * + * @var string $uri + */ + public $uri; + + /** @var string $method */ + public $method; + + public function __construct($request) + { + $this->body = $request->body; + $this->bodyEncoding = $request->bodyEncoding; + $this->headers = (array) $request->headers; + $this->uri = $request->uri; + $this->method = $request->method; + } +} diff --git a/src/DataObjects/Transaction.php b/src/DataObjects/Transaction.php new file mode 100644 index 0000000..6174669 --- /dev/null +++ b/src/DataObjects/Transaction.php @@ -0,0 +1,118 @@ +id = $transaction->id; + $this->name = $transaction->name; + $this->host = $transaction->host; + $this->port = $transaction->port; + $this->protocol = $transaction->protocol; + $this->fullPath = $transaction->fullPath; + $this->skip = $transaction->skip ?? false; + $this->fail = $transaction->fail ?? false; + $this->errors = $transaction->errors ?? new stdClass(); + $this->results = $transaction->results ?? new stdClass(); + $this->origin = new Origin($transaction->origin); + $this->request = new Request($transaction->request); + $this->expected = new ExpectedResponse($transaction->expected); + $this->real = new RealResponse($transaction->real); + } +} diff --git a/src/Server.php b/src/Server.php index da9f1cc..69df5f5 100644 --- a/src/Server.php +++ b/src/Server.php @@ -1,6 +1,7 @@ event; - $data = $message->data; + $data = new Transaction($message->data); $uuid = $message->uuid; if ($event == "beforeEach") { diff --git a/tests/DreddHooksTest.php b/tests/DreddHooksTest.php index a79bed0..aed7a58 100644 --- a/tests/DreddHooksTest.php +++ b/tests/DreddHooksTest.php @@ -1,5 +1,6 @@ assertCount(1, $hooks); $this->assertTrue(is_a($hooks[0], $this->className), sprintf("Expected %s received %s", $this->className, get_class($hooks[0]))); } + + public function it_can_pass_a_transaction_object() + { + $transaction = new Transaction((object)[ + 'id' => 'test', + 'name' => 'test', + 'host' => '127.0.0.1', + 'port' => '12345', + 'protocol' => 'http', + 'fullPath' => 'http://127.0.0.1:12345/path', + 'request' => [ + 'body' => '{}', + 'bodyEncoding' => 'utf8', + 'headers' => [], + 'uri' => 'http://127.0.0.1:12345/path', + 'method' => 'GET', + ], + 'origin' => [ + 'filename' => 'test.apib', + 'apiName' => 'test', + 'resourceGroupName' => '', + 'resourceName' => '', + 'actionName' => '', + 'exampleName' => '', + ], + 'expected' => [ + 'statusCode' => 200, + 'headers' => [], + 'body' => '{}', + 'bodySchema' => new stdClass(), + ], + 'real' => [ + 'statusCode' => 200, + 'headers' => [], + 'body' => '{}', + 'bodyEncoding' => 'utf8', + ], + ]); + + Hooks::before('Admin > *', function(&$transaction) { + $this->assertInstanceOf(Transaction::class, $transaction); + }); + + $hooks = Hooks::getCallbacksForName('beforeHooks', $transaction); + } }