Skip to content

Commit a668ee9

Browse files
author
Alex Rothuis
committed
Improve doctrine types: remove string casting, add type checking
1 parent 06ab69d commit a668ee9

10 files changed

+135
-9
lines changed

src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/ConfigurationContactInformationType.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use Surfnet\Stepup\Configuration\Value\ContactInformation;
2626

2727
/**
28-
* Custom Type for the Location Value Object for the Configuration domain
28+
* Custom Type for the ContactInformation Value Object for the Configuration domain
2929
*/
3030
class ConfigurationContactInformationType extends Type
3131
{
@@ -39,10 +39,20 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
3939
public function convertToDatabaseValue($value, AbstractPlatform $platform)
4040
{
4141
if (is_null($value)) {
42-
return $value;
42+
return null;
43+
}
44+
45+
if (!$value instanceof ContactInformation) {
46+
throw new ConversionException(
47+
sprintf(
48+
"Encountered illegal contact information of type %s '%s', expected a ContactInformation instance",
49+
is_object($value) ? get_class($value) : gettype($value),
50+
is_scalar($value) ? (string) $value : ''
51+
)
52+
);
4353
}
4454

45-
return (string) $value;
55+
return $value->getContactInformation();
4656
}
4757

4858
public function convertToPHPValue($value, AbstractPlatform $platform)

src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/ConfigurationInstitutionType.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
4242
return $value;
4343
}
4444

45-
return (string) $value;
45+
if (!$value instanceof Institution) {
46+
throw new ConversionException(
47+
sprintf(
48+
"Encountered illegal institution of type %s '%s', expected an Institution instance",
49+
is_object($value) ? get_class($value) : gettype($value),
50+
is_scalar($value) ? (string) $value : ''
51+
)
52+
);
53+
}
54+
55+
return $value->getInstitution();
4656
}
4757

4858
public function convertToPHPValue($value, AbstractPlatform $platform)

src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/ConfigurationLocationType.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
4242
return $value;
4343
}
4444

45-
return (string) $value;
45+
if (!$value instanceof Location) {
46+
throw new ConversionException(
47+
sprintf(
48+
"Encountered illegal location of type %s '%s', expected a Location instance",
49+
is_object($value) ? get_class($value) : gettype($value),
50+
is_scalar($value) ? (string) $value : ''
51+
)
52+
);
53+
}
54+
55+
return $value->getLocation();
4656
}
4757

4858
public function convertToPHPValue($value, AbstractPlatform $platform)

src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/DocumentNumberType.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,28 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
4242
}
4343

4444
/**
45-
* @param mixed $value
45+
* @param mixed $value
4646
* @param AbstractPlatform $platform
4747
* @return null|string
48+
* @throws ConversionException
4849
*/
4950
public function convertToDatabaseValue($value, AbstractPlatform $platform)
5051
{
51-
if (!$value instanceof DocumentNumber) {
52+
if (is_null($value)) {
5253
return null;
5354
}
5455

55-
return (string) $value;
56+
if (!$value instanceof DocumentNumber) {
57+
throw new ConversionException(
58+
sprintf(
59+
"Encountered illegal document number of type %s '%s', expected a DocumentNumber instance",
60+
is_object($value) ? get_class($value) : gettype($value),
61+
is_scalar($value) ? (string) $value : ''
62+
)
63+
);
64+
}
65+
66+
return $value->getDocumentNumber();
5667
}
5768

5869
/**

src/Surfnet/StepupMiddleware/ApiBundle/Doctrine/Type/RaLocationNameType.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
4242
return $value;
4343
}
4444

45-
return (string) $value;
45+
if (!$value instanceof RaLocationName) {
46+
throw new ConversionException(
47+
sprintf(
48+
"Encountered illegal RA location name of type %s '%s', expected a RaLocationName instance",
49+
is_object($value) ? get_class($value) : gettype($value),
50+
is_scalar($value) ? (string) $value : ''
51+
)
52+
);
53+
}
54+
55+
return $value->getRaLocationName();
4656
}
4757

4858
public function convertToPHPValue($value, AbstractPlatform $platform)

src/Surfnet/StepupMiddleware/ApiBundle/Tests/Doctrine/Type/ConfigurationContactInformationTypeTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ public function setUp()
4747
$this->platform = new MySqlPlatform();
4848
}
4949

50+
/**
51+
* @test
52+
* @group doctrine
53+
*
54+
* @dataProvider \Surfnet\StepupMiddleware\ApiBundle\Tests\TestDataProvider::notNull
55+
* @param $incorrectValue
56+
*/
57+
public function a_value_can_only_be_converted_to_sql_if_it_is_contact_information_or_null($incorrectValue)
58+
{
59+
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
60+
61+
$configurationContactInformation = Type::getType(ConfigurationContactInformationType::NAME);
62+
$configurationContactInformation->convertToDatabaseValue($incorrectValue, $this->platform);
63+
}
64+
5065
/**
5166
* @test
5267
* @group doctrine

src/Surfnet/StepupMiddleware/ApiBundle/Tests/Doctrine/Type/ConfigurationInstitutionTypeTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ public function a_null_value_remains_null_in_to_sql_conversion()
6060
$this->assertNull($value);
6161
}
6262

63+
/**
64+
* @test
65+
* @group doctrine
66+
*
67+
* @dataProvider \Surfnet\StepupMiddleware\ApiBundle\Tests\TestDataProvider::notNull
68+
* @param $incorrectValue
69+
*/
70+
public function a_value_can_only_be_converted_to_sql_if_it_is_an_institution_or_null($incorrectValue)
71+
{
72+
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
73+
74+
$configurationContactInformation = Type::getType(ConfigurationInstitutionType::NAME);
75+
$configurationContactInformation->convertToDatabaseValue($incorrectValue, $this->platform);
76+
}
77+
6378
/**
6479
* @test
6580
* @group doctrine

src/Surfnet/StepupMiddleware/ApiBundle/Tests/Doctrine/Type/ConfigurationLocationTypeTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ public function a_null_value_remains_null_in_to_sql_conversion()
6060
$this->assertNull($value);
6161
}
6262

63+
/**
64+
* @test
65+
* @group doctrine
66+
*
67+
* @dataProvider \Surfnet\StepupMiddleware\ApiBundle\Tests\TestDataProvider::notNull
68+
* @param $incorrectValue
69+
*/
70+
public function a_value_can_only_be_converted_to_sql_if_it_is_a_location_or_null($incorrectValue)
71+
{
72+
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
73+
74+
$configurationContactInformation = Type::getType(ConfigurationLocationType::NAME);
75+
$configurationContactInformation->convertToDatabaseValue($incorrectValue, $this->platform);
76+
}
77+
6378
/**
6479
* @test
6580
* @group doctrine

src/Surfnet/StepupMiddleware/ApiBundle/Tests/Doctrine/Type/DocumentNumberTypeTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ public function a_null_value_remains_null_when_converting_from_db_to_php_value()
8585
$this->assertNull($value);
8686
}
8787

88+
/**
89+
* @test
90+
* @group doctrine
91+
*
92+
* @dataProvider \Surfnet\StepupMiddleware\ApiBundle\Tests\TestDataProvider::notNull
93+
* @param $incorrectValue
94+
*/
95+
public function a_value_can_only_be_converted_to_sql_if_it_is_a_document_number_or_null($incorrectValue)
96+
{
97+
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
98+
99+
$configurationContactInformation = Type::getType(DocumentNumberType::NAME);
100+
$configurationContactInformation->convertToDatabaseValue($incorrectValue, $this->platform);
101+
}
102+
88103
/**
89104
* @test
90105
* @group doctrine

src/Surfnet/StepupMiddleware/ApiBundle/Tests/Doctrine/Type/RaLocationNameTypeTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ public function a_non_null_value_is_converted_to_the_correct_format()
7676
$this->assertEquals($expected, $output);
7777
}
7878

79+
/**
80+
* @test
81+
* @group doctrine
82+
*
83+
* @dataProvider \Surfnet\StepupMiddleware\ApiBundle\Tests\TestDataProvider::notNull
84+
* @param $incorrectValue
85+
*/
86+
public function a_value_can_only_be_converted_to_sql_if_it_is_an_ra_location_or_null($incorrectValue)
87+
{
88+
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
89+
90+
$configurationContactInformation = Type::getType(RaLocationNameType::NAME);
91+
$configurationContactInformation->convertToDatabaseValue($incorrectValue, $this->platform);
92+
}
93+
7994
/**
8095
* @test
8196
* @group doctrine

0 commit comments

Comments
 (0)