Skip to content

Commit

Permalink
Merge pull request #308 from pact-foundation/matcher_email
Browse files Browse the repository at this point in the history
Matcher email
  • Loading branch information
cfmack committed Apr 29, 2023
2 parents 44fa06d + e6ac90a commit 2380bd3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
52 changes: 30 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ PHP version of [Pact](https://pact.io). Enables consumer driven contract testing
Table of contents
=================

* [Versions](#versions)
* [Specifications](#specifications)
* [Installation](#installation)
* [Basic Consumer Usage](#basic-consumer-usage)
* [Start and Stop the Mock Server](#start-and-stop-the-mock-server)
* [Create Consumer Unit Test](#create-consumer-unit-test)
* [Create Mock Request](#create-mock-request)
* [Create Mock Response](#create-mock-response)
* [Build the Interaction](#build-the-interaction)
* [Make the Request](#make-the-request)
* [Make Assertions](#make-assertions)
* [Basic Provider Usage](#basic-provider-usage)
* [Create Unit Tests](#create-unit-test)
* [Start API](#start-api)
* [Provider Verification](#provider-verification)
* [Verify From Pact Broker](#verify-from-pact-broker)
* [Verify All from Pact Broker](#verify-all-from-pact-broker)
* [Verify Files by Path](#verify-files-by-path)
* [Tips](#tips)
* [Starting API Asynchronously](#starting-api-asynchronously)
* [Set Up Provider State](#set-up-provider-state)
* [Examples](#additional-examples)
- [Pact PHP](#pact-php)
- [Table of contents](#table-of-contents)
- [Versions](#versions)
- [Specifications](#specifications)
- [Installation](#installation)
- [Basic Consumer Usage](#basic-consumer-usage)
- [Start and Stop the Mock Server](#start-and-stop-the-mock-server)
- [Create Consumer Unit Test](#create-consumer-unit-test)
- [Create Mock Request](#create-mock-request)
- [Create Mock Response](#create-mock-response)
- [Build the Interaction](#build-the-interaction)
- [Make the Request](#make-the-request)
- [Verify Interactions](#verify-interactions)
- [Make Assertions](#make-assertions)
- [Basic Provider Usage](#basic-provider-usage)
- [Create Unit Test](#create-unit-test)
- [Start API](#start-api)
- [Provider Verification](#provider-verification)
- [Verify From Pact Broker](#verify-from-pact-broker)
- [Verify All from Pact Broker](#verify-all-from-pact-broker)
- [Verify Files by Path](#verify-files-by-path)
- [Tips](#tips)
- [Starting API Asynchronously](#starting-api-asynchronously)
- [Set Up Provider State](#set-up-provider-state)
- [Additional Examples](#additional-examples)
- [Message support](#message-support)
- [Consumer Side Message Processing](#consumer-side-message-processing)
- [Provider Side Message Validation](#provider-side-message-validation)
- [Usage for the optional `pact-stub-service`](#usage-for-the-optional-pact-stub-service)

## Versions
9.X updates internal dependencies and libraries. This results in dropping PHP 7.4
Expand Down Expand Up @@ -173,6 +180,7 @@ hexadecimal | Regex to match a hexadecimal number. Example: 3F | Value (Defaults
uuid | Regex to match a uuid. | Value (Defaults to ce118b6e-d8e1-11e7-9296-cec278b6b50a) | $matcher->uuid('ce118b6e-d8e1-11e7-9296-cec278b6b50a')
ipv4Address | Regex to match a ipv4 address. | Value (Defaults to 127.0.0.13) | $matcher->ipv4Address('127.0.0.1')
ipv6Address | Regex to match a ipv6 address. | Value (Defaults to ::ffff:192.0.2.128) | $matcher->ipv6Address('::ffff:192.0.2.1')
email | Regex to match an address. | Value (hello@pact.io) | $matcher->email('hello@pact.io')

### Build the Interaction

Expand Down
11 changes: 11 additions & 0 deletions src/PhpPact/Consumer/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
class Matcher
{
public const EMAIL_FORMAT = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$";
public const ISO8601_DATE_FORMAT = '^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))?)$';
public const ISO8601_DATETIME_FORMAT = '^\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d([+-][0-2]\\d(?:|:?[0-5]\\d)|Z)?$';
public const ISO8601_DATETIME_WITH_MILLIS_FORMAT = '^\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d\\.\\d{3}([+-][0-2]\\d(?:|:?[0-5]\\d)|Z)?$';
Expand Down Expand Up @@ -257,4 +258,14 @@ public function ipv6Address(string $ip = '::ffff:192.0.2.128'): array
{
return $this->term($ip, self::IPV6_FORMAT);
}

/**
* @return array<string, mixed>
*
* @throws \Exception
*/
public function email(string $email = 'hello@pact.io'): array
{
return $this->term($email, self::EMAIL_FORMAT);
}
}
21 changes: 21 additions & 0 deletions tests/PhpPact/Consumer/Matcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,25 @@ public function testIpv6Address()

$this->assertEquals($expected, $this->matcher->ipv6Address());
}


/**
* @throws Exception
*/
public function testEmail()
{

$expected = [
'data' => [
'generate' => 'hello@pact.io',
'matcher' => [
'json_class' => 'Regexp',
'o' => 0,
's' => '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$',
],
],
'json_class' => 'Pact::Term',
];
$this->assertEquals($expected, $this->matcher->email());
}
}

0 comments on commit 2380bd3

Please sign in to comment.