Skip to content
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion features/failing_transaction.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
32 changes: 32 additions & 0 deletions features/transaction_object.feature
Original file line number Diff line number Diff line change
@@ -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:
"""
<?php

use Dredd\DataObjects\Transaction;
use Dredd\Hooks;

Hooks::beforeEach(function(Transaction &$transaction) {

echo 'Transaction object';
});
"""
When I run `dredd ./apiary.apib http://localhost:4567 --server "node server.js" --language php --hookfiles hook_transaction_object.php --loglevel debug`
Then the output should contain:
"""
Transaction object
"""
9 changes: 9 additions & 0 deletions hooks/hook_transaction_object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Dredd\DataObjects\Transaction;
use Dredd\Hooks;

Hooks::beforeEach(function(Transaction &$transaction) {

echo 'Transaction object';
});
34 changes: 34 additions & 0 deletions src/DataObjects/ExpectedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Dredd\DataObjects;

class ExpectedResponse
{
/** @var string $statusCode */
public $statusCode;

/**
* Keys are HTTP header names, values are HTTP header contents
*
* @var array<string,string>
*/
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;
}
}
38 changes: 38 additions & 0 deletions src/DataObjects/Origin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Dredd\DataObjects;

/**
* Reference to the transaction definition in the original API description document.
* (See also {@link https://github.com/apiaryio/dredd-transactions#user-content-data-structures Dredd Transactions})
*/
class Origin
{
/** @var string $filename */
public $filename;

/** @var string $apiName */
public $apiName;

/** @var string $resourceGroupName */
public $resourceGroupName;

/** @var string $resourceName */
public $resourceName;

/** @var string $actionName */
public $actionName;

/** @var string $exampleName */
public $exampleName;

public function __construct($origin)
{
$this->filename = $origin->filename;
$this->apiName = $origin->apiName;
$this->resourceGroupName = $origin->resourceGroupName;
$this->resourceName = $origin->resourceName;
$this->actionName = $origin->actionName;
$this->exampleName = $origin->exampleName;
}
}
36 changes: 36 additions & 0 deletions src/DataObjects/RealResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Dredd\DataObjects;

class RealResponse
{
/** @var string $statusCode */
public $statusCode;

/**
* Keys are HTTP header names, values are HTTP header contents
*
* @var object
*/
public $headers;

/** @var string $body */
public $body;

/**
* - `utf-8` (string) - indicates `body` contains a textual content encoded in UTF-8
* - `base64` (string) - indicates `body` contains a binary content encoded in Base64
*
* @var string $bodyEncoding
* @psalm-var 'utf-8'|'base64' $bodyEncoding
*/
public $bodyEncoding;

public function __construct($expected)
{
$this->statusCode = $expected->statusCode;
$this->headers = $expected->headers;
$this->body = $expected->body;
$this->bodyEncoding = $expected->bodyEncoding;
}
}
44 changes: 44 additions & 0 deletions src/DataObjects/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Dredd\DataObjects;

class Request
{
/** @var string $body */
public $body;

/**
* Can be manually set in {@link https://dredd.org/en/latest/hooks/index.html#hooks hooks}
* - `utf-8` (string) - indicates `body` contains a textual content encoded in UTF-8
* - `base64` (string) - indicates `body` contains a binary content encoded in Base64
*
* @var string $bodyEncoding
* @psalm-var 'utf-8'|'base64' $bodyEncoding
*/
public $bodyEncoding;

/**
* Keys are HTTP header names, values are HTTP header contents
* @var array<string,string> $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;
}
}
118 changes: 118 additions & 0 deletions src/DataObjects/Transaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

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}
* and is one of the main public interfaces in Dredd.
*
* @link https://dredd.org/en/latest/data-structures.html#transaction-object
*/
class Transaction
{
/** @var string $id */
public $id;

/**
* Reference to the transaction definition in the original API description document
* (See also {@link https://github.com/apiaryio/dredd-transactions#user-content-data-structures Dredd Transactions})
*
* @var string $name
*/
public $name;

/** @var string $host */
public $host;

/** @var int $port */
public $port;

/** @var string $protocol */
public $protocol;

/**
* Expanded URI Template with parameters (if any) used for the HTTP request Dredd performs to the tested server
*
* @link https://tools.ietf.org/html/rfc6570.html
* @var string $fullPath
*/
public $fullPath;

/**
* Can be set to `true` and the transaction will be skipped
*
* @var bool $skip
*/
public $skip;

/**
* Can be set to `true` or string and the transaction will fail
* - (string) - failure message with details why the transaction failed
* - (boolean)
* @var bool|string $fail
*/
public $fail;

/** @var Origin $origin */
public $origin;

/**
* Test data passed to Dredd’s reporters
*
* @link https://dredd.org/en/latest/data-structures.html#transaction-test
* @var object
*/
public $test;

/**
* Transaction runtime errors
*
* Whenever an exception occurs during a test run it’s being recorded under the errors property of the test.
*
* @link https://dredd.org/en/latest/data-structures.html#test-runtime-error
* @var object
*/
public $errors;

/**
* Transaction result equals to the result of the
* {@link https://github.com/apiaryio/gavel.js Gavel} validation library.
*
* @link https://dredd.org/en/latest/data-structures.html#transaction-results
* @var object
*/
public $results;

/**
* The HTTP request Dredd performs to the tested server, taken from the API description
* @var Request $request
*/
public $request;

/** @var ExpectedResponse $expected */
public $expected;

/** @var RealResponse $real */
public $real;

public function __construct($transaction)
{
$this->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);
}
}
3 changes: 2 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Dredd;

use UnexpectedValueException;
use Dredd\DataObjects\Transaction;

class Server
{
Expand Down Expand Up @@ -69,7 +70,7 @@ public function run($force = false)
public function processMessage($message, $client)
{
$event = $message->event;
$data = $message->data;
$data = new Transaction($message->data);
$uuid = $message->uuid;

if ($event == "beforeEach") {
Expand Down
Loading