Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
refactor(Customer): adjust customer email verification request to API…
Browse files Browse the repository at this point in the history
… changes

BREAKING CHANGE:

Before:
```
CustomerEmailConfirmRequest::ofIdVersionAndToken($id, $version, $token)
```

After:
```
CustomerEmailConfirmRequest::ofToken($token)
```
  • Loading branch information
Jens Schulze committed Mar 23, 2016
1 parent 318e93f commit 2e3dd32
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 27 deletions.
7 changes: 2 additions & 5 deletions features/bootstrap/ApiContext.php
Expand Up @@ -548,12 +548,9 @@ public function iWantToConfirmTheContextEmailWithToken($context, $token)
$context = $this->getContext($context);
$module = $this->getModuleName($context);
$request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'EmailConfirmRequest';
$requestContext = $context . 'Request';
$id = $this->objects[$requestContext]['id'];
$version = $this->objects[$requestContext]['version'];
$this->request = call_user_func_array(
$request. '::ofIdVersionAndToken',
[$id, $version, $token]
$request. '::ofToken',
[$token]
);
}

Expand Down
3 changes: 0 additions & 3 deletions features/request/Customer/CustomerEmailConfirmation.feature
Expand Up @@ -14,15 +14,12 @@ Feature: I want to confirm a customer's email
"""

Scenario: Confirm Token for email change
Given a "customer" is identified by "id" and version 1
Given i want to confirm the "Customer" email with token "token"
Then the path should be "customers/email/confirm"
And the method should be "POST"
And the request should be
"""
{
"id": "id",
"version": 1,
"tokenValue": "token"
}
"""
28 changes: 13 additions & 15 deletions src/Request/Customers/CustomerEmailConfirmRequest.php
Expand Up @@ -9,20 +9,21 @@
use Commercetools\Core\Client\HttpMethod;
use Commercetools\Core\Client\JsonRequest;
use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractUpdateRequest;
use Commercetools\Core\Request\AbstractApiRequest;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\Customer\Customer;
use Commercetools\Core\Response\ResourceResponse;
use Psr\Http\Message\ResponseInterface;

/**
* @package Commercetools\Core\Request\Customers
* @link https://dev.commercetools.com/http-api-projects-customers.html#verify-customers-email
* @method Customer mapResponse(ApiResponseInterface $response)
*/
class CustomerEmailConfirmRequest extends AbstractUpdateRequest
class CustomerEmailConfirmRequest extends AbstractApiRequest
{
protected $resultClass = '\Commercetools\Core\Model\Customer\Customer';

const ID = 'id';
const TOKEN_VALUE = 'tokenValue';

/**
Expand All @@ -31,29 +32,23 @@ class CustomerEmailConfirmRequest extends AbstractUpdateRequest
protected $tokenValue;

/**
* @param string $id
* @param int $version
* @param string $tokenValue
* @param Context $context
*/
public function __construct($id, $version, $tokenValue, Context $context = null)
public function __construct($tokenValue, Context $context = null)
{
parent::__construct(CustomersEndpoint::endpoint(), $id, $version, [], $context);
$this->setId($id);
$this->setVersion($version);
parent::__construct(CustomersEndpoint::endpoint(), $context);
$this->tokenValue = $tokenValue;
}

/**
* @param string $id
* @param int $version
* @param string $tokenValue
* @param Context $context
* @return static
*/
public static function ofIdVersionAndToken($id, $version, $tokenValue, Context $context = null)
public static function ofToken($tokenValue, Context $context = null)
{
return new static($id, $version, $tokenValue, $context);
return new static($tokenValue, $context);
}

/**
Expand All @@ -72,10 +67,13 @@ protected function getPath()
public function httpRequest()
{
$payload = [
static::ID => $this->getId(),
static::VERSION => $this->getVersion(),
static::TOKEN_VALUE => $this->tokenValue,
];
return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload);
}

public function buildResponse(ResponseInterface $response)
{
return new ResourceResponse($response, $this, $this->getContext());
}
}
30 changes: 30 additions & 0 deletions tests/integration/Customer/CustomerLoginRequestTest.php
Expand Up @@ -11,6 +11,8 @@
use Commercetools\Core\Request\Customers\CustomerByTokenGetRequest;
use Commercetools\Core\Request\Customers\CustomerCreateRequest;
use Commercetools\Core\Request\Customers\CustomerDeleteRequest;
use Commercetools\Core\Request\Customers\CustomerEmailConfirmRequest;
use Commercetools\Core\Request\Customers\CustomerEmailTokenRequest;
use Commercetools\Core\Request\Customers\CustomerLoginRequest;
use Commercetools\Core\Request\Customers\CustomerPasswordChangeRequest;
use Commercetools\Core\Request\Customers\CustomerPasswordResetRequest;
Expand Down Expand Up @@ -143,4 +145,32 @@ public function testPasswordReset()
$response = $request->executeWithClient($this->getClient());
$this->assertTrue($response->isError());
}

public function testVerifyEmail()
{
$draft = $this->getDraft('email');
$customer = $this->createCustomer($draft);

$this->assertFalse($customer->getIsEmailVerified());

$request = CustomerEmailTokenRequest::ofIdVersionAndTtl(
$customer->getId(),
$customer->getVersion(),
15
);
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);

$token = $result->getValue();
$this->assertNotEmpty($token);

$request = CustomerEmailConfirmRequest::ofToken(
$token
);
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);
$this->deleteRequest->setVersion($result->getVersion());

$this->assertTrue($result->getIsEmailVerified());
}
}
Expand Up @@ -18,27 +18,27 @@ class CustomerEmailConfirmRequestTest extends RequestTestCase

public function testHttpRequestMethod()
{
$request = CustomerEmailConfirmRequest::ofIdVersionAndToken('customerId', 1, 'token');
$request = CustomerEmailConfirmRequest::ofToken('token');
$httpRequest = $request->httpRequest();

$this->assertSame(HttpMethod::POST, $httpRequest->getMethod());
}

public function testHttpRequestPath()
{
$request = CustomerEmailConfirmRequest::ofIdVersionAndToken('customerId', 1, 'token');
$request = CustomerEmailConfirmRequest::ofToken('token');
$httpRequest = $request->httpRequest();

$this->assertSame('customers/email/confirm', (string)$httpRequest->getUri());
}

public function testHttpRequestObject()
{
$request = CustomerEmailConfirmRequest::ofIdVersionAndToken('customerId', 1, 'token');
$request = CustomerEmailConfirmRequest::ofToken('token');
$httpRequest = $request->httpRequest();

$this->assertJsonStringEqualsJsonString(
json_encode(['id' => 'customerId', 'version' => 1, 'tokenValue' => 'token']),
json_encode(['tokenValue' => 'token']),
(string)$httpRequest->getBody()
);
}
Expand Down

0 comments on commit 2e3dd32

Please sign in to comment.