Skip to content

Commit 83a6f3b

Browse files
authored
Add support for numeric type (#183)
1 parent 6708cca commit 83a6f3b

File tree

5 files changed

+86
-34
lines changed

5 files changed

+86
-34
lines changed

.github/workflows/ci.yml

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
name: CI
22

3-
on: [pull_request]
3+
on:
4+
push:
5+
pull_request:
46

57
jobs:
68
composer-validate:
79
runs-on: ubuntu-latest
10+
name: "Composer Validate"
811
steps:
912
- uses: actions/checkout@v1
10-
11-
- name: Validate composer.json
12-
run: composer validate
13+
- name: 'Validate composer.json'
14+
run: composer validate --no-check-all --strict --no-check-lock
1315

1416
composer-install:
1517
needs: composer-validate
1618
runs-on: ubuntu-latest
19+
name: "Composer Install"
1720
steps:
1821
- uses: actions/checkout@v1
19-
20-
- name: Install dependencies
22+
- name: 'Install dependencies'
2123
run: composer install --prefer-dist --no-progress --no-suggest --no-interaction
24+
25+
code-style:
26+
needs: composer-install
27+
runs-on: ubuntu-latest
28+
name: "Code Style"
29+
strategy:
30+
matrix:
31+
php_versions:
32+
- '7.2'
33+
- '7.3'
34+
- '7.4'
35+
- '8.0'
36+
fail-fast: false
37+
steps:
38+
- uses: actions/checkout@v2
39+
- name: 'Setup PHP'
40+
uses: shivammathur/setup-php@v2
41+
with:
42+
php-version: ${{ matrix.php_versions }}
43+
ini-values: memory_limit=-1
44+
tools: composer
45+
coverage: none
46+
- name: 'Install PHP dependencies with Composer'
47+
run: composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader
48+
working-directory: './'
49+
- name: 'Code Style'
50+
run: './vendor/bin/phpcs ./ -p --encoding=utf-8 --extensions=php --ignore="vendor|Tests" --standard=./vendor/escapestudios/symfony2-coding-standard/Symfony'

DBAL/Types/AbstractEnumType.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
use Doctrine\DBAL\Platforms\AbstractPlatform;
1616
use Doctrine\DBAL\Platforms\MySqlPlatform;
17-
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
17+
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
1818
use Doctrine\DBAL\Platforms\SqlitePlatform;
19-
use Doctrine\DBAL\Platforms\SQLServerPlatform;
19+
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
2020
use Doctrine\DBAL\Types\Type;
2121
use Fresh\DoctrineEnumBundle\Exception\InvalidArgumentException;
2222

@@ -103,8 +103,8 @@ static function (string $value) {
103103
$sqlDeclaration = \sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
104104

105105
break;
106-
case $platform instanceof PostgreSqlPlatform:
107-
case $platform instanceof SQLServerPlatform:
106+
case $platform instanceof PostgreSQL100Platform:
107+
case $platform instanceof SQLServer2012Platform:
108108
$sqlDeclaration = \sprintf('VARCHAR(255) CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
109109

110110
break;
@@ -141,7 +141,7 @@ public function getName(): string
141141
*
142142
* @static
143143
*
144-
* @return string[]
144+
* @return mixed[]
145145
*/
146146
public static function getChoices(): array
147147
{
@@ -153,7 +153,7 @@ public static function getChoices(): array
153153
*
154154
* @static
155155
*
156-
* @return string[] Values for the ENUM field
156+
* @return mixed[] Values for the ENUM field
157157
*/
158158
public static function getValues(): array
159159
{
@@ -180,7 +180,7 @@ public static function getRandomValue()
180180
*
181181
* @static
182182
*
183-
* @return string[] Array of values in readable format
183+
* @return mixed[] Array of values in readable format
184184
*/
185185
public static function getReadableValues(): array
186186
{
@@ -190,27 +190,27 @@ public static function getReadableValues(): array
190190
/**
191191
* Asserts that given choice exists in the array of ENUM values.
192192
*
193-
* @param string $value ENUM value
193+
* @param mixed $value ENUM value
194194
*
195195
* @throws InvalidArgumentException
196196
*/
197-
public static function assertValidChoice(string $value): void
197+
public static function assertValidChoice($value): void
198198
{
199199
if (!isset(static::$choices[$value])) {
200-
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for ENUM type "%s".', $value, static::class));
200+
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for ENUM type "%s".', (string) $value, static::class));
201201
}
202202
}
203203

204204
/**
205205
* Get value in readable format.
206206
*
207-
* @param string $value ENUM value
207+
* @param mixed $value ENUM value
208208
*
209209
* @static
210210
*
211-
* @return string Value in readable format
211+
* @return mixed Value in readable format
212212
*/
213-
public static function getReadableValue(string $value): string
213+
public static function getReadableValue($value)
214214
{
215215
static::assertValidChoice($value);
216216

@@ -220,13 +220,13 @@ public static function getReadableValue(string $value): string
220220
/**
221221
* Check if some string value exists in the array of ENUM values.
222222
*
223-
* @param string $value ENUM value
223+
* @param mixed $value ENUM value
224224
*
225225
* @static
226226
*
227227
* @return bool
228228
*/
229-
public static function isValueExist(string $value): bool
229+
public static function isValueExist($value): bool
230230
{
231231
return isset(static::$choices[$value]);
232232
}
@@ -236,9 +236,9 @@ public static function isValueExist(string $value): bool
236236
*
237237
* @static
238238
*
239-
* @return string|null Default value for DDL statement
239+
* @return mixed|null Default value for DDL statement
240240
*/
241-
public static function getDefaultValue(): ?string
241+
public static function getDefaultValue()
242242
{
243243
return null;
244244
}

Tests/DBAL/Types/AbstractEnumTypeTest.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
use Doctrine\DBAL\Platforms\AbstractPlatform;
1616
use Doctrine\DBAL\Platforms\MySqlPlatform;
17-
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
17+
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
1818
use Doctrine\DBAL\Platforms\SqlitePlatform;
19-
use Doctrine\DBAL\Platforms\SQLServerPlatform;
19+
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
2020
use Doctrine\DBAL\Types\Type;
2121
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
2222
use Fresh\DoctrineEnumBundle\Exception\InvalidArgumentException;
@@ -81,12 +81,12 @@ public static function platformProviderForGetSqlDeclarationWithoutDefaultValue()
8181
];
8282
yield 'postgresql' => [
8383
['name' => 'position'],
84-
new PostgreSqlPlatform(),
84+
new PostgreSQL100Platform(),
8585
"VARCHAR(255) CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))",
8686
];
8787
yield 'sql server' => [
8888
['name' => 'position'],
89-
new SQLServerPlatform(),
89+
new SQLServer2012Platform(),
9090
"VARCHAR(255) CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))",
9191
];
9292
}
@@ -118,12 +118,12 @@ public static function platformProviderForGetSqlDeclarationWithDefaultValue(): i
118118
];
119119
yield 'postgresql' => [
120120
['name' => 'position'],
121-
new PostgreSqlPlatform(),
121+
new PostgreSQL100Platform(),
122122
"VARCHAR(255) CHECK(position IN ('pending', 'done', 'failed')) DEFAULT 'pending'",
123123
];
124124
yield 'sql server' => [
125125
['name' => 'position'],
126-
new SQLServerPlatform(),
126+
new SQLServer2012Platform(),
127127
"VARCHAR(255) CHECK(position IN ('pending', 'done', 'failed')) DEFAULT 'pending'",
128128
];
129129
}
@@ -172,26 +172,41 @@ public function testGetReadableValues(): void
172172
self::assertEquals($choices, $this->type::getReadableValues());
173173
}
174174

175-
public function testAssertValidChoice(): void
175+
public function testAssertValidChoiceString(): void
176176
{
177177
self::assertNull($this->type::assertValidChoice(BasketballPositionType::SMALL_FORWARD));
178178
}
179179

180+
public function testAssertValidChoiceNumeric(): void
181+
{
182+
$this->type = Type::getType('NumericType');
183+
self::assertNull($this->type::assertValidChoice(NumericType::TWO));
184+
$this->type = Type::getType('BasketballPositionType');
185+
}
186+
180187
public function testInvalidArgumentExceptionInAssertValidChoice(): void
181188
{
182189
$this->expectException(InvalidArgumentException::class);
183190
$this->type::assertValidChoice('YO');
184191
}
185192

186-
public function testGetReadableValue(): void
193+
public function testGetReadableValueString(): void
187194
{
188195
self::assertEquals('Small Forward', $this->type::getReadableValue(BasketballPositionType::SMALL_FORWARD));
189196
}
190197

198+
public function testGetReadableValueNumeric(): void
199+
{
200+
$this->type = Type::getType('NumericType');
201+
self::assertEquals(2, $this->type::getReadableValue(NumericType::TWO));
202+
$this->type = Type::getType('BasketballPositionType');
203+
}
204+
191205
public function testGetDefaultValue(): void
192206
{
193207
self::assertNull($this->type::getDefaultValue());
194208
self::assertEquals('pending', Type::getType('TaskStatusType')::getDefaultValue());
209+
self::assertEquals(0, Type::getType('NumericType')::getDefaultValue());
195210
}
196211

197212
public function testInvalidArgumentExceptionInGetReadableValue(): void
@@ -223,8 +238,8 @@ public function testMappedDatabaseTypesDoesNotContainEnumOnNonMySQL(): void
223238
{
224239
$testProviders = [
225240
new SqlitePlatform(),
226-
new PostgreSqlPlatform(),
227-
new SQLServerPlatform(),
241+
new PostgreSQL100Platform(),
242+
new SQLServer2012Platform(),
228243
];
229244

230245
foreach ($testProviders as $testProvider) {

Tests/Fixtures/DBAL/Types/NumericType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ final class NumericType extends AbstractEnumType
4040
self::THREE => 3,
4141
self::FOUR => 4,
4242
];
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public static function getDefaultValue(): ?int
48+
{
49+
return self::ZERO;
50+
}
4351
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"twig/twig": "^3.0"
3636
},
3737
"require-dev": {
38-
"escapestudios/symfony2-coding-standard": "^3.11",
38+
"escapestudios/symfony2-coding-standard": "^3.12",
3939
"phpstan/phpstan": "^0.12",
4040
"phpstan/phpstan-doctrine":"^0.12",
4141
"phpstan/phpstan-phpunit": "^0.12",

0 commit comments

Comments
 (0)