Skip to content

Commit

Permalink
Merge pull request #59 from rarus/task#58
Browse files Browse the repository at this point in the history
task#58 исправлена ошибка c часовыми поясами и добавлены проверки
  • Loading branch information
rarus-web authored Aug 27, 2018
2 parents 56835a4 + eb73b60 commit b5ceeb9
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 40 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# bonus-server-php-sdk change log

## 0.5.2 (27.08.2018)
- исправлена ошибка c часовыми поясами и добавлены проверки в тесты `Rarus\BonusServer\Util\DateTimeParser` [issue#58](https://github.com/rarus/bonus-server-php-sdk/issues/58)

## 0.5.1 (20.08.2018)
- исправлена ошибка с точностью в парсере timestamp `Rarus\BonusServer\Util\DateTimeParser` [issue#55](https://github.com/rarus/bonus-server-php-sdk/issues/55)
- исправлена ошибка с некорректными датами рождения при добавлении новых пользователей [issue#55](https://github.com/rarus/bonus-server-php-sdk/issues/55)
Expand Down
14 changes: 3 additions & 11 deletions examples/user.add.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
declare(strict_types=1);
require_once __DIR__ . '/init.php';

$userArray = [
'name' => 'Михаил',
'login' => 'grishi+1@rarus.ru',
'phone' => '+7978 888 22 22',
'email' => 'grishi@rarus.ru',
'gender' => 'male',
'birthdate' => '742262400',
];


$newUser = \Rarus\BonusServer\Users\DTO\Fabric::createNewInstance(
'grishi-' . random_int(0, PHP_INT_MAX),
'Михаил Гришин',
'+7978 888 22 22',
'grishi@rarus.ru'
'grishi@rarus.ru',
null,
new \DateTime('06.06.1985')
);

$transport = \Rarus\BonusServer\Users\Transport\Role\Organization\Fabric::getInstance($apiClient, new \Money\Currency('RUB'), $log);
Expand Down
6 changes: 1 addition & 5 deletions src/Rarus/BonusServer/Users/DTO/Fabric.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public static function initUserFromServerResponse(array $arUser, \DateTimeZone $
$user->setGender(Users\DTO\Gender\Fabric::initFromServerResponse($arUser['gender']));
}
if ($arUser['birthdate'] !== 0) {
$gmtOffsetInSeconds = $dateTimeZone->getOffset(new \DateTime('now', $dateTimeZone));
$realTimestamp = (int)$arUser['birthdate'];
$birthdateWithGmt = DateTimeParser::parseTimestampFromServerResponse((string)$realTimestamp, $dateTimeZone);
$birthday = \DateTime::createFromFormat('U', (string)($birthdateWithGmt->getTimestamp() + $gmtOffsetInSeconds), $dateTimeZone);
$birthday->setTimezone($dateTimeZone);
$birthday = DateTimeParser::parseTimestampFromServerResponse((string)$arUser['birthdate'], $dateTimeZone);
$user->setBirthdate($birthday);
}

Expand Down
5 changes: 2 additions & 3 deletions src/Rarus/BonusServer/Users/Formatters/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public static function toArrayForCreateNewUser(BonusServer\Users\DTO\User $newUs
'gender' => $newUser->getGender() === null ? '' : $newUser->getGender()->getCode(),
];
if ($newUser->getBirthdate() !== null) {
$gmtOffsetInSeconds = $dateTimeZone->getOffset(new \DateTime('now', $dateTimeZone));
$gmtTimestamp = (string)($newUser->getBirthdate()->getTimestamp() - $gmtOffsetInSeconds);
$arNewUser['birthdate'] = BonusServer\Util\DateTimeParser::convertToServerFormatTimestamp(\DateTime::createFromFormat('U', $gmtTimestamp, $dateTimeZone));
$utcBirthday = new \DateTime($newUser->getBirthdate()->format('d.m.Y H:i:s'), new \DateTimeZone('UTC'));
$arNewUser['birthdate'] = BonusServer\Util\DateTimeParser::convertToServerFormatTimestamp($utcBirthday);
} else {
$arNewUser['birthdate'] = 0;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Rarus/BonusServer/Util/DateTimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public static function parseTimestampFromServerResponse(string $timestampStr, \D
));
}
// отделяем миллисекунды - 3 последних цифры
$timestampStr = substr($timestampStr, 0, 9) . '.' . substr($timestampStr, 9);
$microseconds = substr($timestampStr, \strlen($timestampStr) - 3);
$timestampStr = substr($timestampStr, 0, -3) . '.' . $microseconds;

$timestamp = \DateTime::createFromFormat('U.u', $timestampStr, new \DateTimeZone('UTC'));
$timestamp->setTimezone(new \DateTimeZone('UTC'));

$timestamp = \DateTime::createFromFormat('U.u', $timestampStr, $dateTimeZone);
$timestamp->setTimezone($dateTimeZone);
if (false === $timestamp) {
throw new ApiClientException(sprintf('ошибка при разборе поля время в ответе сервера [%s]', $timestampStr));
}
Expand Down
56 changes: 44 additions & 12 deletions tests/DemoDataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ public static function createNewCardCollection(int $cardsCount): Cards\DTO\CardC
*/
public static function createNewCard(): Cards\DTO\Card
{
$cardPrefix = random_int(1000000, 100000000);

return Cards\DTO\Fabric::createNewInstance(
'php-unit-test-card',
(string)random_int(1000000, 100000000),
'php-unit-test-card' . $cardPrefix,
(string)$cardPrefix,
\TestEnvironmentManager::getDefaultCurrency());
}

/**
* @param int $usersCount
*
* @return Users\DTO\UserCollection
* @throws Exception
*/
public static function createNewUserWithUserUidAndPasswordCollection(int $usersCount): Users\DTO\UserCollection
{
Expand All @@ -66,12 +69,15 @@ public static function createNewUserWithUserUidAndPasswordCollection(int $usersC
* @param string $userPasswordHash
*
* @return Users\DTO\User
* @throws Exception
*/
public static function createNewUserWithUserUidAndPassword(string $userUid, string $userPasswordHash): \Rarus\BonusServer\Users\DTO\User
{
$userUUID = random_int(0, PHP_INT_MAX);

return Users\DTO\Fabric::createNewInstance(
$userUid,
'Михаил Гришин',
'Михаил Гришин | ' . $userUUID,
'+79788882222',
'grishi@rarus.ru',
null,
Expand All @@ -88,13 +94,15 @@ public static function createNewUserWithUserUidAndPassword(string $userUid, stri
*/
public static function createNewUser(): \Rarus\BonusServer\Users\DTO\User
{
$userUUID = random_int(0, PHP_INT_MAX);

return \Rarus\BonusServer\Users\DTO\Fabric::createNewInstance(
'grishi-' . random_int(0, PHP_INT_MAX),
'Михаил Гришин',
'grishi-' . $userUUID,
'Михаил Гришин | ' . $userUUID,
'+7978 888 22 22',
'grishi@rarus.ru',
Users\DTO\Gender\Fabric::getMale(),
new DateTime('06.08.1995 00:00:00', \TestEnvironmentManager::getDefaultTimezone())
new DateTime('27.08.2018 00:00:00', \TestEnvironmentManager::getDefaultTimezone())
);
}

Expand All @@ -104,25 +112,47 @@ public static function createNewUser(): \Rarus\BonusServer\Users\DTO\User
*/
public static function createNewUserWithSummerBirthday(): \Rarus\BonusServer\Users\DTO\User
{
$userUUID = random_int(0, PHP_INT_MAX);

return \Rarus\BonusServer\Users\DTO\Fabric::createNewInstance(
'grishi-' . random_int(0, PHP_INT_MAX),
'Михаил Гришин',
'grishi-' . $userUUID,
'Михаил Гришин | ' . $userUUID,
'+7978 888 22 22',
'grishi@rarus.ru',
Users\DTO\Gender\Fabric::getMale(),
new DateTime('06.08.1995 00:00:00', \TestEnvironmentManager::getDefaultTimezone())
);
}

/**
* @return Users\DTO\User
* @throws Exception
*/
public static function createNewUserWithNowDateTimeBirthday(): \Rarus\BonusServer\Users\DTO\User
{
$userUUID = random_int(0, PHP_INT_MAX);

return \Rarus\BonusServer\Users\DTO\Fabric::createNewInstance(
'grishi-' . $userUUID,
'Михаил Гришин | ' . $userUUID,
'+7978 888 22 22',
'grishi@rarus.ru',
Users\DTO\Gender\Fabric::getMale(),
new DateTime('now', \TestEnvironmentManager::getDefaultTimezone())
);
}

/**
* @return Users\DTO\User
* @throws Exception
*/
public static function createNewUserWithWinterBirthday(): \Rarus\BonusServer\Users\DTO\User
{
$userUUID = random_int(0, PHP_INT_MAX);

return \Rarus\BonusServer\Users\DTO\Fabric::createNewInstance(
'grishi-' . random_int(0, PHP_INT_MAX),
'Михаил Гришин',
'grishi-' . $userUUID,
'Михаил Гришин | ' . $userUUID,
'+7978 888 22 22',
'grishi@rarus.ru',
Users\DTO\Gender\Fabric::getMale(),
Expand All @@ -136,9 +166,11 @@ public static function createNewUserWithWinterBirthday(): \Rarus\BonusServer\Use
*/
public static function createNewUserWithoutBirthday(): \Rarus\BonusServer\Users\DTO\User
{
$userUUID = random_int(0, PHP_INT_MAX);

return \Rarus\BonusServer\Users\DTO\Fabric::createNewInstance(
'grishi-' . random_int(0, PHP_INT_MAX),
'Михаил Гришин',
'grishi-' . $userUUID,
'Михаил Гришин | ' . $userUUID,
'+7978 888 22 22',
'grishi@rarus.ru',
Users\DTO\Gender\Fabric::getMale()
Expand Down
4 changes: 2 additions & 2 deletions tests/TestEnvironmentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class TestEnvironmentManager
/**
* @var string
*/
private const LOGIN = 'beligo';
private const LOGIN = 'mesm';
/**
* @var string
*/
private const PASSWORD = 'ef2ba1a5da4aa1d2e22fe2d2697cba94e5a7d9d0';
private const PASSWORD = '3a9e090168bf592bcc35990bcc9fab9aff090004';
/**
* @var string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class TransportTest extends TestCase
*/
public function testGetByUserIdMethod(): void
{
// гарантированно создаём карту для привязки к пользователю, если карт нет, то юзера не создать
$newFreeCard = \DemoDataGenerator::createNewCard();
$this->cardTransport->addNewCard($newFreeCard);

$user = $this->userTransport->addNewUser(\DemoDataGenerator::createNewUser());

$user2 = $this->userTransport->getByUserId($user->getUserId());
Expand All @@ -45,11 +49,14 @@ public function testGetByUserIdMethod(): void
*/
public function testAddNewUserMethod(): void
{
// гарантированно создаём карту для привязки к пользователю, если карт нет, то юзера не создать
$newFreeCard = \DemoDataGenerator::createNewCard();
$this->cardTransport->addNewCard($newFreeCard);
// создаём нового юзера
$newUser = \DemoDataGenerator::createNewUser();
// пробуем его добавить
$user = $this->userTransport->addNewUser($newUser);

$this->assertEquals('grishi@rarus.ru', $user->getEmail());
$this->assertEquals($newUser->getBirthdate()->getTimestamp(), $user->getBirthdate()->getTimestamp());
$this->assertEquals($newUser->getBirthdate()->format('d.m.Y H:i:s'), $user->getBirthdate()->format('d.m.Y H:i:s'));
}

Expand All @@ -58,11 +65,30 @@ public function testAddNewUserMethod(): void
*/
public function testAddNewUserWithSummerBirthdayMethod(): void
{
// гарантированно создаём карту для привязки к пользователю, если карт нет, то юзера не создать
$newFreeCard = \DemoDataGenerator::createNewCard();
$this->cardTransport->addNewCard($newFreeCard);

$newUser = \DemoDataGenerator::createNewUserWithSummerBirthday();
$user = $this->userTransport->addNewUser($newUser);

$this->assertEquals($newUser->getEmail(), $user->getEmail());
$this->assertEquals($newUser->getBirthdate()->getTimestamp(), $user->getBirthdate()->getTimestamp());
$this->assertEquals($newUser->getBirthdate()->format('d.m.Y H:i:s'), $user->getBirthdate()->format('d.m.Y H:i:s'));
}

/**
* @covers \Rarus\BonusServer\Users\Transport\Role\Organization\Transport::addNewUser()
*/
public function testAddNewUserWithNowDateTimeBirthdayMethod(): void
{
// гарантированно создаём карту для привязки к пользователю, если карт нет, то юзера не создать
$newFreeCard = \DemoDataGenerator::createNewCard();
$this->cardTransport->addNewCard($newFreeCard);

$newUser = \DemoDataGenerator::createNewUserWithNowDateTimeBirthday();
$user = $this->userTransport->addNewUser($newUser);

$this->assertEquals($newUser->getEmail(), $user->getEmail());
$this->assertEquals($newUser->getBirthdate()->format('d.m.Y H:i:s'), $user->getBirthdate()->format('d.m.Y H:i:s'));
}

Expand All @@ -71,11 +97,14 @@ public function testAddNewUserWithSummerBirthdayMethod(): void
*/
public function testAddNewUserWithWinterBirthdayMethod(): void
{
// гарантированно создаём карту для привязки к пользователю, если карт нет, то юзера не создать
$newFreeCard = \DemoDataGenerator::createNewCard();
$this->cardTransport->addNewCard($newFreeCard);

$newUser = \DemoDataGenerator::createNewUserWithWinterBirthday();
$user = $this->userTransport->addNewUser($newUser);

$this->assertEquals($newUser->getEmail(), $user->getEmail());
$this->assertEquals($newUser->getBirthdate()->getTimestamp(), $user->getBirthdate()->getTimestamp());
$this->assertEquals($newUser->getBirthdate()->format('d.m.Y H:i:s'), $user->getBirthdate()->format('d.m.Y H:i:s'));
}

Expand All @@ -84,6 +113,10 @@ public function testAddNewUserWithWinterBirthdayMethod(): void
*/
public function testAddNewUserWithoutBirthday(): void
{
// гарантированно создаём карту для привязки к пользователю, если карт нет, то юзера не создать
$newFreeCard = \DemoDataGenerator::createNewCard();
$this->cardTransport->addNewCard($newFreeCard);

$newUser = \DemoDataGenerator::createNewUserWithoutBirthday();
$user = $this->userTransport->addNewUser($newUser);

Expand All @@ -95,6 +128,10 @@ public function testAddNewUserWithoutBirthday(): void
*/
public function testAddNewUserAndAttachFreeCardMethod(): void
{
// гарантированно создаём карту для привязки к пользователю, если карт нет, то юзера не создать
$newFreeCard = \DemoDataGenerator::createNewCard();
$this->cardTransport->addNewCard($newFreeCard);

$newUser = \DemoDataGenerator::createNewUser();
$user = $this->userTransport->addNewUserAndAttachFreeCard($newUser);
// юзера вычитали корректного
Expand Down

0 comments on commit b5ceeb9

Please sign in to comment.