Skip to content

Commit

Permalink
EZP-31029: Adjust Role transformers to introduced type hints in RoleS…
Browse files Browse the repository at this point in the history
…ervice
  • Loading branch information
mikadamczyk committed Oct 17, 2019
1 parent a38cf5b commit 6ad5478
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/lib/Form/DataTransformer/RoleAssignmentTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ public function reverseTransform($value): ?APIRoleAssignment
return null;
}

if (!ctype_digit($value)) {
throw new TransformationFailedException('Expected a numeric string.');
}

try {
return $this->roleService->loadRoleAssignment($value);
return $this->roleService->loadRoleAssignment((int)$value);
} catch (NotFoundException $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/Form/DataTransformer/RoleTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ public function reverseTransform($value): ?APIRole
return null;
}

if (!ctype_digit($value)) {
throw new TransformationFailedException('Expected a numeric string.');
}

try {
return $this->roleService->loadRole($value);
return $this->roleService->loadRole((int)$value);
} catch (NotFoundException $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function testTransform($value, $expected)
*/
public function testTransformWithInvalidInput($value)
{
$languageService = $this->createMock(RoleService::class);
$transformer = new RoleAssignmentTransformer($languageService);
$roleService = $this->createMock(RoleService::class);
$transformer = new RoleAssignmentTransformer($roleService);

$this->expectException(TransformationFailedException::class);
$this->expectExceptionMessage('Expected a ' . APIRoleAsignment::class . ' object.');
Expand Down Expand Up @@ -78,18 +78,35 @@ public function testReverseTransformWithNull()
$this->assertNull($result);
}

public function testReverseTransformWithNotFoundException()
/**
* @dataProvider reverseTransformWithInvalidInputDataProvider
*
* @param $value
*/
public function testReverseTransformWithInvalidInput($value)
{
$service = $this->createMock(RoleService::class);

$transformer = new RoleAssignmentTransformer($service);

$this->expectException(TransformationFailedException::class);
$this->expectExceptionMessage('Location not found');
$this->expectExceptionMessage('Expected a numeric string.');

$transformer->reverseTransform($value);
}

public function testReverseTransformWithNotFoundException()
{
$service = $this->createMock(RoleService::class);
$service->method('loadRoleAssignment')
->will($this->throwException(new class('Location not found') extends NotFoundException {
}));

$transformer = new RoleAssignmentTransformer($service);

$this->expectException(TransformationFailedException::class);
$this->expectExceptionMessage('Location not found');

$transformer->reverseTransform(654321);
}

Expand Down Expand Up @@ -120,4 +137,20 @@ public function transformWithInvalidInputDataProvider(): array
'object' => [new \stdClass()],
];
}

/**
* @return array
*/
public function reverseTransformWithInvalidInputDataProvider(): array
{
return [
'string' => ['string'],
'bool' => [true],
'float' => [12.34],
'array' => [[1]],
'object' => [new \stdClass()],
'scientific_notation' => ['1337e0'],
'hexadecimal' => ['0x539'],
];
}
}
36 changes: 34 additions & 2 deletions src/lib/Tests/Form/DataTransformer/RoleTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function testTransform($value, $expected)
*/
public function testTransformWithInvalidInput($value)
{
$languageService = $this->createMock(RoleService::class);
$transformer = new RoleTransformer($languageService);
$roleService = $this->createMock(RoleService::class);
$transformer = new RoleTransformer($roleService);

$this->expectException(TransformationFailedException::class);
$this->expectExceptionMessage('Expected a ' . APIRole::class . ' object.');
Expand Down Expand Up @@ -78,6 +78,22 @@ public function testReverseTransformWithNull()
$this->assertNull($result);
}

/**
* @dataProvider reverseTransformWithInvalidInputDataProvider
*
* @param $value
*/
public function testReverseTransformWithInvalidInput($value)
{
$roleService = $this->createMock(RoleService::class);
$transformer = new RoleTransformer($roleService);

$this->expectException(TransformationFailedException::class);
$this->expectExceptionMessage('Expected a numeric string.');

$transformer->reverseTransform($value);
}

public function testReverseTransformWithNotFoundException()
{
$this->expectException(TransformationFailedException::class);
Expand Down Expand Up @@ -120,4 +136,20 @@ public function transformWithInvalidInputDataProvider(): array
'object' => [new \stdClass()],
];
}

/**
* @return array
*/
public function reverseTransformWithInvalidInputDataProvider(): array
{
return [
'string' => ['string'],
'bool' => [true],
'float' => [12.34],
'array' => [[1]],
'object' => [new \stdClass()],
'scientific_notation' => ['1337e0'],
'hexadecimal' => ['0x539'],
];
}
}

0 comments on commit 6ad5478

Please sign in to comment.