Skip to content

Commit

Permalink
REENG-14 Fixed decodeTime in Mapper (#58)
Browse files Browse the repository at this point in the history
* REENG-14 Fixed decodeTime in Mapper
---------

Co-authored-by: Aliaksandr Lishko <[email protected]>
  • Loading branch information
aliaksandr-lishko and Aliaksandr Lishko authored Jul 8, 2024
1 parent 864df5e commit db931c5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 2.7.3
- Fixed decodeTime validation

## 2.7.2
- Initializing spots with empty array in location

Expand Down
3 changes: 3 additions & 0 deletions src/Paysera/WalletApi/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,9 @@ public function encodeTime(Paysera_WalletApi_Entity_Time $object)
public function decodeTime($input)
{
$time = explode(':', $input);
if (count($time) !== 2) {
throw new InvalidArgumentException('Provided time is not in valid format. Valid format is HH:MM.');
}

return new Paysera_WalletApi_Entity_Time(
(int) $time[0] !== 24 ? $time[0] : 0,
Expand Down
79 changes: 79 additions & 0 deletions tests/Paysera/WalletApi/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,85 @@ public function testEncodeLocationEmptySpot() {
$this->assertEquals($expectedData, $encodedLocation);
}

/**
* @dataProvider encodeTimeDataProvider
*/
public function testDecodeTime($data, $expectedException, $expectedResult)
{
if ($expectedException !== null) {
$this->setExpectedException($expectedException);
}

$result = $this->mapper->decodeTime($data);

if ($expectedResult !== null) {
$this->assertInstanceOf(Paysera_WalletApi_Entity_Time::class, $result);
$this->assertEquals($expectedResult->getHours(), $result->getHours());
$this->assertEquals($expectedResult->getMinutes(), $result->getMinutes());
}
}

public function encodeTimeDataProvider()
{
return [
'valid data' => [
'data' => '9:0',
'expectedException' => null,
'expectedResult' => new Paysera_WalletApi_Entity_Time(9, 0),
],
'wrong format: missed minutes' => [
'data' => '9',
'expectedException' => InvalidArgumentException::class,
'expectedResult' => null,
],
'wrong format: wrong separator' => [
'data' => '9-0',
'expectedException' => InvalidArgumentException::class,
'expectedResult' => null,
],
'24:00 converts to 0:0' => [
'data' => '24:00',
'expectedException' => null,
'expectedResult' => new Paysera_WalletApi_Entity_Time(0, 0),
],
'wrong format: extra data' => [
'data' => '9:0:0',
'expectedException' => InvalidArgumentException::class,
'expectedResult' => null,
],
'invalid data: negative hours' => [
'data' => '-9:0',
'expectedException' => Paysera_WalletApi_Exception_LogicException::class,
'expectedResult' => null,
],
'invalid data: negative minutes' => [
'data' => '9:-1',
'expectedException' => Paysera_WalletApi_Exception_LogicException::class,
'expectedResult' => null,
],
'invalid data: hours more that 23' => [
'data' => '25:00',
'expectedException' => Paysera_WalletApi_Exception_LogicException::class,
'expectedResult' => null,
],
'invalid data: minutes more that 59' => [
'data' => '23:60',
'expectedException' => Paysera_WalletApi_Exception_LogicException::class,
'expectedResult' => null,
],
'invalid data: hours are not numeric' => [
'data' => 'a:10',
'expectedException' => InvalidArgumentException::class,
'expectedResult' => null,
],
'invalid data: minutes are not numeric' => [
'data' => '23:a',
'expectedException' => InvalidArgumentException::class,
'expectedResult' => null,
],
];
}

private function setProperty($object, $property, $value)
{
$reflectionProperty = new ReflectionProperty($object, $property);
Expand Down

0 comments on commit db931c5

Please sign in to comment.