From 8732efc2a1972e77f0e47ea61ac6190a599f8c03 Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Sun, 31 May 2020 14:26:28 -0500 Subject: [PATCH 01/10] 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. --- src/DataObjects/ExpectedResponse.php | 34 ++++++++ src/DataObjects/Origin.php | 38 +++++++++ src/DataObjects/RealResponse.php | 36 +++++++++ src/DataObjects/Request.php | 44 +++++++++++ src/DataObjects/Transaction.php | 114 +++++++++++++++++++++++++++ src/Server.php | 3 +- 6 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 src/DataObjects/ExpectedResponse.php create mode 100644 src/DataObjects/Origin.php create mode 100644 src/DataObjects/RealResponse.php create mode 100644 src/DataObjects/Request.php create mode 100644 src/DataObjects/Transaction.php diff --git a/src/DataObjects/ExpectedResponse.php b/src/DataObjects/ExpectedResponse.php new file mode 100644 index 0000000..7a7899d --- /dev/null +++ b/src/DataObjects/ExpectedResponse.php @@ -0,0 +1,34 @@ +statusCode = $expected->statusCode; + $this->headers = $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..f2b4f0a --- /dev/null +++ b/src/DataObjects/Request.php @@ -0,0 +1,44 @@ +body = $request->body; + $this->bodyEncoding = $request->bodyEncoding; + $this->headers = $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..166e128 --- /dev/null +++ b/src/DataObjects/Transaction.php @@ -0,0 +1,114 @@ +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; + $this->fail = $transaction->fail; + $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") { From 4797afb6a8f1aa531389ba12150bf85566e9f120 Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Wed, 17 Jun 2020 17:39:06 -0500 Subject: [PATCH 02/10] Change headers to an array. This allows for easier ability to work with it. Cast the headers to an array in the construct, and define the type hint as a string=>string array. --- src/DataObjects/Request.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DataObjects/Request.php b/src/DataObjects/Request.php index f2b4f0a..e62b499 100644 --- a/src/DataObjects/Request.php +++ b/src/DataObjects/Request.php @@ -19,7 +19,7 @@ class Request /** * Keys are HTTP header names, values are HTTP header contents - * @var object $headers + * @var array $headers */ public $headers; @@ -37,7 +37,7 @@ public function __construct($request) { $this->body = $request->body; $this->bodyEncoding = $request->bodyEncoding; - $this->headers = $request->headers; + $this->headers = (array) $request->headers; $this->uri = $request->uri; $this->method = $request->method; } From e5382c6238da2ff2c9b8b484b292a41dbe2f2856 Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Wed, 17 Jun 2020 17:42:16 -0500 Subject: [PATCH 03/10] Install dredd into the docker container. This is necessary, or else cucumber tests will fail. --- Dockerfile | 1 + 1 file changed, 1 insertion(+) 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 From 49c317fa1c0a7911bf24c538ef4446d761c9ae55 Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Wed, 17 Jun 2020 17:43:32 -0500 Subject: [PATCH 04/10] Add Transaction objects to tests, to validate it doesn't negatively affect anything. --- tests/DreddRunnerTest.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/DreddRunnerTest.php b/tests/DreddRunnerTest.php index 16412bb..5037c7e 100644 --- a/tests/DreddRunnerTest.php +++ b/tests/DreddRunnerTest.php @@ -1,6 +1,6 @@ name = $transactionName; Hooks::before($transactionName, function($transaction) { @@ -149,7 +149,7 @@ public function it_can_run_beforeEachHooks() { $transactionName = 'transaction'; - $transaction = new stdClass(); + $transaction = new Transaction(new stdClass()); $transaction->name = $transactionName; Hooks::beforeEach(function($transaction) { @@ -168,11 +168,11 @@ public function it_can_cause_transaction_to_fail() { $transactionName = 'transaction'; - $transaction = new stdClass(); + $transaction = new Transaction(new stdClass()); $transaction->name = $transactionName; $transaction->fail = false; - Hooks::before($transactionName, function(&$transaction) { + Hooks::before($transactionName, function(Transaction &$transaction) { $transaction->fail = true; }); @@ -191,11 +191,11 @@ public function it_can_cause_before_each_transaction_to_fail() { $transactionName = 'transaction'; - $transaction = new stdClass(); + $transaction = new Transaction(new stdClass()); $transaction->name = $transactionName; $transaction->fail = false; - Hooks::beforeEach(function(&$transaction) { + Hooks::beforeEach(function(Transaction &$transaction) { $transaction->fail = true; }); @@ -214,10 +214,10 @@ public function it_can_handle_one_level_of_wildcards_in_transaction_names() $transactionName = 'Admin > admin logs in'; $wildcardName = 'Admin > *'; - $transaction = new stdClass(); + $transaction = new Transaction(new stdClass()); $transaction->name = $transactionName; - Hooks::before($wildcardName, function(&$transaction) + Hooks::before($wildcardName, function(Transaction &$transaction) { echo 'yay this is also called'; }); @@ -234,11 +234,11 @@ public function it_can_handle_multiple_levels_of_nesting_in_transaction_names() { $wildcardName = 'Admin > admin logs in > *'; - $transaction = new stdClass(); + $transaction = new Transaction(new stdClass()); $transactionName = 'Admin > admin logs in > another event'; $transaction->name = $transactionName; - Hooks::before($wildcardName, function(&$transaction) + Hooks::before($wildcardName, function(Transaction &$transaction) { echo 'yay this is also called'; }); From d9d7b8c301c2766434b4150cd05e689259c6e1f2 Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Wed, 17 Jun 2020 18:56:56 -0500 Subject: [PATCH 05/10] Should be `node` not `nodejs`. --- features/failing_transaction.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: """ From e43050dc312c5bee456bf73180b2f7dadaeaf205 Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Wed, 17 Jun 2020 18:57:21 -0500 Subject: [PATCH 06/10] Add a cucumber test for Transaction objects. --- features/transaction_object.feature | 32 +++++++++++++++++++++++++++++ hooks/hook_transaction_object.php | 9 ++++++++ 2 files changed, 41 insertions(+) create mode 100644 features/transaction_object.feature create mode 100644 hooks/hook_transaction_object.php 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: + """ + Date: Wed, 17 Jun 2020 19:04:02 -0500 Subject: [PATCH 07/10] Undo Transaction objects, as these stdClass ones aren't well formed. --- tests/DreddRunnerTest.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/DreddRunnerTest.php b/tests/DreddRunnerTest.php index 5037c7e..16412bb 100644 --- a/tests/DreddRunnerTest.php +++ b/tests/DreddRunnerTest.php @@ -1,6 +1,6 @@ name = $transactionName; Hooks::before($transactionName, function($transaction) { @@ -149,7 +149,7 @@ public function it_can_run_beforeEachHooks() { $transactionName = 'transaction'; - $transaction = new Transaction(new stdClass()); + $transaction = new stdClass(); $transaction->name = $transactionName; Hooks::beforeEach(function($transaction) { @@ -168,11 +168,11 @@ public function it_can_cause_transaction_to_fail() { $transactionName = 'transaction'; - $transaction = new Transaction(new stdClass()); + $transaction = new stdClass(); $transaction->name = $transactionName; $transaction->fail = false; - Hooks::before($transactionName, function(Transaction &$transaction) { + Hooks::before($transactionName, function(&$transaction) { $transaction->fail = true; }); @@ -191,11 +191,11 @@ public function it_can_cause_before_each_transaction_to_fail() { $transactionName = 'transaction'; - $transaction = new Transaction(new stdClass()); + $transaction = new stdClass(); $transaction->name = $transactionName; $transaction->fail = false; - Hooks::beforeEach(function(Transaction &$transaction) { + Hooks::beforeEach(function(&$transaction) { $transaction->fail = true; }); @@ -214,10 +214,10 @@ public function it_can_handle_one_level_of_wildcards_in_transaction_names() $transactionName = 'Admin > admin logs in'; $wildcardName = 'Admin > *'; - $transaction = new Transaction(new stdClass()); + $transaction = new stdClass(); $transaction->name = $transactionName; - Hooks::before($wildcardName, function(Transaction &$transaction) + Hooks::before($wildcardName, function(&$transaction) { echo 'yay this is also called'; }); @@ -234,11 +234,11 @@ public function it_can_handle_multiple_levels_of_nesting_in_transaction_names() { $wildcardName = 'Admin > admin logs in > *'; - $transaction = new Transaction(new stdClass()); + $transaction = new stdClass(); $transactionName = 'Admin > admin logs in > another event'; $transaction->name = $transactionName; - Hooks::before($wildcardName, function(Transaction &$transaction) + Hooks::before($wildcardName, function(&$transaction) { echo 'yay this is also called'; }); From 35fee5d5804ff79da0ab3e4c77747007672de225 Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Wed, 17 Jun 2020 19:14:31 -0500 Subject: [PATCH 08/10] Convert headers to an array of string=>string. --- src/DataObjects/ExpectedResponse.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DataObjects/ExpectedResponse.php b/src/DataObjects/ExpectedResponse.php index 7a7899d..aa8b724 100644 --- a/src/DataObjects/ExpectedResponse.php +++ b/src/DataObjects/ExpectedResponse.php @@ -10,7 +10,7 @@ class ExpectedResponse /** * Keys are HTTP header names, values are HTTP header contents * - * @var object + * @var array */ public $headers; @@ -27,7 +27,7 @@ class ExpectedResponse public function __construct($expected) { $this->statusCode = $expected->statusCode; - $this->headers = $expected->headers; + $this->headers = (array) $expected->headers; $this->body = $expected->body; $this->bodySchema = $expected->bodySchema; } From 0c2557f11cc09588fc8a88ae719a3379ee95d23e Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Wed, 17 Jun 2020 19:14:46 -0500 Subject: [PATCH 09/10] Add some defaults, just in case. --- src/DataObjects/Transaction.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/DataObjects/Transaction.php b/src/DataObjects/Transaction.php index 166e128..6174669 100644 --- a/src/DataObjects/Transaction.php +++ b/src/DataObjects/Transaction.php @@ -2,6 +2,8 @@ namespace Dredd\DataObjects; +use stdClass; + /** * Transaction object is passed as a first argument to * {@link https://dredd.org/en/latest/hooks/index.html#hooks hook functions} @@ -104,8 +106,10 @@ public function __construct($transaction) $this->port = $transaction->port; $this->protocol = $transaction->protocol; $this->fullPath = $transaction->fullPath; - $this->skip = $transaction->skip; - $this->fail = $transaction->fail; + $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); From ef960e1cc4b2cda57878c782e2555b55584caa77 Mon Sep 17 00:00:00 2001 From: Marisa Clardy Date: Wed, 17 Jun 2020 19:15:18 -0500 Subject: [PATCH 10/10] Add a test specifically for Transaction object. --- tests/DreddHooksTest.php | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) 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); + } }