Skip to content

Commit

Permalink
Merge pull request #35 from xsolla/user_search
Browse files Browse the repository at this point in the history
Added public user id processing for User Search webhook method.
  • Loading branch information
ipanyukov committed May 25, 2016
2 parents 66631de + d4a4063 commit a000633
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
All notable changes to this project will be documented in this file.

## [Unreleased](https://github.com/xsolla/xsolla-sdk-php/compare/v2.5.0...master)
### Added
* Added public user id processing for [User Search](http://developers.xsolla.com/api.html#user-search) webhook method.

## [v2.5.0](https://github.com/xsolla/xsolla-sdk-php/compare/v2.4.1...v2.5.0) - 2016-05-18
### Added
Expand Down
2 changes: 2 additions & 0 deletions src/Webhook/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
abstract class Message
{
const USER_VALIDATION = 'user_validation';
const USER_SEARCH = 'user_search';
const PAYMENT = 'payment';
const REFUND = 'refund';
const CREATE_SUBSCRIPTION = 'create_subscription';
Expand All @@ -17,6 +18,7 @@ abstract class Message

protected static $classMap = array(
self::USER_VALIDATION => '\Xsolla\SDK\Webhook\Message\UserValidationMessage',
self::USER_SEARCH => '\Xsolla\SDK\Webhook\Message\UserSearchMessage',
self::PAYMENT => '\Xsolla\SDK\Webhook\Message\PaymentMessage',
self::REFUND => '\Xsolla\SDK\Webhook\Message\RefundMessage',
self::CREATE_SUBSCRIPTION => '\Xsolla\SDK\Webhook\Message\CreateSubscriptionMessage',
Expand Down
26 changes: 26 additions & 0 deletions src/Webhook/Message/UserSearchMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Xsolla\SDK\Webhook\Message;

class UserSearchMessage extends Message
{
/**
* @return string|null
*/
public function getUserPublicId()
{
if (array_key_exists('public_id', $this->request['user'])) {
return $this->request['user']['public_id'];
}
}

/**
* @return string|null
*/
public function getUserId()
{
if (array_key_exists('id', $this->request['user'])) {
return $this->request['user']['id'];
}
}
}
11 changes: 1 addition & 10 deletions src/Webhook/Response/PinCodeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
namespace Xsolla\SDK\Webhook\Response;

use Xsolla\SDK\API\XsollaClient;
use Xsolla\SDK\Exception\Webhook\XsollaWebhookException;
use Xsolla\SDK\Webhook\WebhookResponse;

class PinCodeResponse extends WebhookResponse
{
public function __construct($pinCode)
{
if (!is_string($pinCode)) {
throw new XsollaWebhookException(sprintf(
'Pin code should be non-empty string. %s given',
is_object($pinCode) ? get_class($pinCode) : gettype($pinCode)
));
}
if ('' === $pinCode) {
throw new XsollaWebhookException('Pin code should be non-empty string. Empty string given');
}
$this->validateStringParameter('Pin code', $pinCode);
parent::__construct(200, XsollaClient::jsonEncode(array('pin_code' => $pinCode)));
}
}
19 changes: 19 additions & 0 deletions src/Webhook/Response/UserResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Xsolla\SDK\Webhook\Response;

use Xsolla\SDK\Webhook\User;
use Xsolla\SDK\Webhook\WebhookResponse;

class UserResponse extends WebhookResponse
{
/**
* UserResponse constructor.
* @param User $user
*/
public function __construct(User $user)
{
$this->validateStringParameter('User id', $user->getId());
parent::__construct(200, $user->toJson());
}
}
96 changes: 96 additions & 0 deletions src/Webhook/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Xsolla\SDK\Webhook;

use Xsolla\SDK\API\XsollaClient;

class User
{
protected $id;
protected $publicId;
protected $name;
protected $email;
protected $phone;

public function getId()
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;

return $this;
}

public function getPublicId()
{
return $this->publicId;
}

public function setPublicId($publicId)
{
$this->publicId = $publicId;

return $this;
}

public function getName()
{
return $this->name;
}

public function setName($name)
{
$this->name = $name;

return $this;
}

public function getEmail()
{
return $this->email;
}

public function setEmail($email)
{
$this->email = $email;

return $this;
}

public function getPhone()
{
return $this->phone;
}

public function setPhone($phone)
{
$this->phone = $phone;

return $this;
}

public function toJson()
{
$response = array();
if ($this->id) {
$response['id'] = $this->id;
}
if ($this->name) {
$response['name'] = $this->name;
}
if ($this->publicId) {
$response['public_id'] = $this->publicId;
}
if ($this->email) {
$response['email'] = $this->email;
}
if ($this->phone) {
$response['phone'] = $this->phone;
}

return XsollaClient::jsonEncode(array('user' => $response));
}
}
14 changes: 14 additions & 0 deletions src/Webhook/WebhookResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@ public function getSymfonyResponse()
{
return $this->symfonyResponse;
}

protected function validateStringParameter($name, $value)
{
if (!is_string($value)) {
throw new XsollaWebhookException(sprintf(
'%s should be non-empty string. %s given',
$name,
is_object($value) ? get_class($value) : gettype($value)
));
}
if ('' === $value) {
throw new XsollaWebhookException($name.' should be non-empty string. Empty string given');
}
}
}
7 changes: 7 additions & 0 deletions tests/Webhook/Message/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public function factoryProvider()
'isPayment' => false,
'isRefund' => false,
),
array(
'notificationType' => 'user_search',
'expectedClass' => '\Xsolla\SDK\Webhook\Message\UserSearchMessage',
'isUserValidation' => false,
'isPayment' => false,
'isRefund' => false,
),
);
}
}
25 changes: 25 additions & 0 deletions tests/Webhook/Message/UserSearchMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Xsolla\SDK\Tests\Webhook\Message;

use Xsolla\SDK\Webhook\Message\UserSearchMessage;

/**
* @group unit
*/
class UserSearchMessageTest extends \PHPUnit_Framework_TestCase
{
public function testUserPublicId()
{
$request = array(
'user' => array(
'public_id' => '1234567',
),
'notification_type' => 'user_search',
);
$message = new UserSearchMessage($request);
static::assertSame($request['user'], $message->getUser());
static::assertSame(null, $message->getUserId());
static::assertSame($request['user']['public_id'], $message->getUserPublicId());
}
}
47 changes: 47 additions & 0 deletions tests/Webhook/Response/PinCodeResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Xsolla\SDK\Tests\Webhook\Message;

use Xsolla\SDK\Webhook\Response\PinCodeResponse;

/**
* @group unit
*/
class PinCodeResponseTest extends \PHPUnit_Framework_TestCase
{
public function testPinCodeHasInvalidType()
{
$this->setExpectedException(
'\Xsolla\SDK\Exception\Webhook\XsollaWebhookException',
'Pin code should be non-empty string. stdClass given'
);
new PinCodeResponse(new \StdClass());
}

public function testPinCodeIsEmptyString()
{
$this->setExpectedException(
'\Xsolla\SDK\Exception\Webhook\XsollaWebhookException',
'Pin code should be non-empty string. NULL given'
);
new PinCodeResponse(null);
}

public function testPinCodeIsNull()
{
$this->setExpectedException(
'\Xsolla\SDK\Exception\Webhook\XsollaWebhookException',
'Pin code should be non-empty string. Empty string given'
);
new PinCodeResponse('');
}

public function testPinCodeResponse()
{
$response = new PinCodeResponse('pin_code');
$this->assertJsonStringEqualsJsonString(
'{"pin_code":"pin_code"}',
$response->getSymfonyResponse()->getContent()
);
}
}
67 changes: 67 additions & 0 deletions tests/Webhook/Response/UserSearchResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Xsolla\SDK\Tests\Webhook\Message;

use Xsolla\SDK\Webhook\Response\UserResponse;
use Xsolla\SDK\Webhook\User;

/**
* @group unit
*/
class UserSearchResponseTest extends \PHPUnit_Framework_TestCase
{
public function testUserIdHasInvalidType()
{
$this->setExpectedException(
'\Xsolla\SDK\Exception\Webhook\XsollaWebhookException',
'User id should be non-empty string. stdClass given'
);
$user = new User();
new UserResponse($user->setId(new \stdClass()));
}

public function testUserIdIsEmptyString()
{
$this->setExpectedException(
'\Xsolla\SDK\Exception\Webhook\XsollaWebhookException',
'User id should be non-empty string. Empty string given'
);
$user = new User();
new UserResponse($user->setId(''));
}

public function testUserIdIsNull()
{
$this->setExpectedException(
'\Xsolla\SDK\Exception\Webhook\XsollaWebhookException',
'User id should be non-empty string. NULL given'
);
new UserResponse(new User());
}

public function testShortResponseFormat()
{
$user = new User();
$response = new UserResponse($user->setId('user_id'));
$this->assertJsonStringEqualsJsonString(
'{"user":{"id":"user_id"}}',
$response->getSymfonyResponse()->getContent()
);
}

public function testFullResponseFormat()
{
$user = new User();
$response = new UserResponse(
$user->setId('user_id')
->setEmail('user_email')
->setPhone('user_phone')
->setName('user_name')
->setPublicId('user_public_id')
);
$this->assertJsonStringEqualsJsonString(
'{"user":{"id":"user_id","email":"user_email","phone":"user_phone","name":"user_name","public_id":"user_public_id"}}',
$response->getSymfonyResponse()->getContent()
);
}
}

0 comments on commit a000633

Please sign in to comment.