meare/codeception-mountebank

This package is abandoned and no longer maintained. No replacement package was suggested.

mountebank integration with Codeception

v1.0 2017-01-29 17:49 UTC

This package is auto-updated.

Last update: 2021-03-31 10:29:46 UTC


README

Latest Version on Packagist Software License Quality Score Total Downloads

codeception-mountebank provides mountebank integration with Codeception. Module allows to:

  • Configure imposters required for tests run;
  • Use imposters as mocks and verify specific requests were made;
  • Save imposters after run for debugging purposes;

Module uses Juggler to interact with mountebank and it's possible to take complete control on mountebank in your custom helper classes or modules.

Note that only HTTP imposters are currently supported.

Install

Require module via Composer:

$ composer require meare/codeception-mountebank

Configuration

Module should be enabled and configured in suite configuration file

class_name: AcceptanceTester
modules:
    enabled:
        - Mountebank
    config:
        Mountebank:
            # mountebank host
            # Required
            host: 'localhost'

            # Port mountebank listens on
            # 2525 by default
            port: 2525

            # Imposters configuration
            # All previous imposters are deleted before the run
            imposters:

                # Imposter alias
                xyz:

                    # Path to imposter contract
                    # Required
                    contract: '_data/mountebank/xyz/stub.json'

                    # Set this property to save imposter contract after tests run
                    # Property value is path to save contract to
                    save: '_output/mountebank/xyz/stub.json'

                    # Set to true if imposter is used as mock
                    # Mock imposters are restored from original contract after each test
                    # Default: false
                    mock: true

Usage

Mock verification

mountebank should be started with --mock flag to use mocking

» mountebank docs on mocking

Imposters mock property should be set to true in suite configuration. It guarantees that imposter will be restored before each test. Restoring means deleting existing imposter from mountebank and posting contract from configuration. This is done to clean requests imposter recorded.

Module provides 3 methods to verify mock imposter:

seeImposterHasRequests($alias)

Asserts that there was at least 1 request recorded on imposter

seeImposterHasRequestsByCriteria($alias, $criteria)

Asserts that there was at least 1 request that satisfies criteria recorded on imposter.

If $criteria is array then request is considered matching if $criteria is subarray of request, e.g.:

$I->seeImposterHasRequestsByCriteria('xyz', [
  'method' => 'GET',
  'query' => [
    'account_id' => '7'
  ]
])
{
  "protocol": "http",
  "port": 4646,
  "numberOfRequests": 1,
  "name": "xyz",
  "requests": [
    {
      "requestFrom": "::ffff:127.0.0.1:57484",
      "method": "GET",
      "path": "/balance",
      "query": {
        "account_id": "7",
        "currency": "USD",
      },
      "headers": {
        "Host": "localhost",
        "Connection": "close"
      },
      "body": "",
      "timestamp": "2017-01-12T16:03:07.632Z"
    }
  ]
}

More complex criteria could be expressed as callback. Callback signature is:

/**
 * @var string $request decoded request object from contract JSON.
 *
 * @return bool Whether requests matches
 */
function(array $request) {}

Callback will be called for each request imposter has until true is returned.

seeImposterHasNoRequests($alias)

Asserts that there is no requests recorded on imposter.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Credits

License

The MIT License (MIT). Please see License File for more information.