Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-31029: Adjust Role transformers to introduced type hints in RoleService #1111

Merged
merged 2 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,33 @@ public function testReverseTransformWithNull()
$this->assertNull($result);
}

public function testReverseTransformWithNotFoundException()
/**
* @dataProvider reverseTransformWithInvalidInputDataProvider
*/
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 +135,17 @@ public function transformWithInvalidInputDataProvider(): array
'object' => [new \stdClass()],
];
}

public function reverseTransformWithInvalidInputDataProvider(): array
{
return [
'string' => ['string'],
'bool' => [true],
'float' => [12.34],
'array' => [[1]],
'object' => [new \stdClass()],
'scientific_notation' => ['1337e0'],
'hexadecimal' => ['0x539'],
];
}
}
31 changes: 29 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,20 @@ public function testReverseTransformWithNull()
$this->assertNull($result);
}

/**
* @dataProvider reverseTransformWithInvalidInputDataProvider
*/
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 +134,17 @@ public function transformWithInvalidInputDataProvider(): array
'object' => [new \stdClass()],
];
}

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