Skip to content

Commit 2558b40

Browse files
authored
Merge pull request #16 from BaguettePHP/feature/support-pgsql-bool
Add @Bool type for PgIdentifier
2 parents 59dde9d + 67bf803 commit 2558b40

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/Type/PgIdentifier.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public function __construct(array $type_names)
3333

3434
public function escapeValue($pdo, $key, $type, $value, &$bind_values)
3535
{
36+
if ($type === '@bool') {
37+
if (\is_bool($value)) {
38+
return $value ? 'true' : 'false';
39+
}
40+
throw new DomainException(\sprintf('param "%s" must be bool', $key));
41+
}
42+
3643
if (!isset($this->types[$type])) {
3744
throw new LogicException("Passed unexpected type '{$type}', please check your configuration.");
3845
}

tests/Type/PgIdentifierTest.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Teto\SQL\Type;
44

5+
use DomainException;
56
use Teto\SQL\DummyPDO;
67
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
78

@@ -12,14 +13,15 @@ class PgIdentifierTest extends TestCase
1213

1314
public function set_up()
1415
{
16+
/** @noinspection PhpMultipleClassDeclarationsInspection */
1517
parent::set_up();
1618

1719
$this->subject = new PgIdentifier([]);
1820
}
1921

2022
/**
2123
* @dataProvider escapeValuesProvider
22-
* @phpstan-param string|array<string> $input
24+
* @phpstan-param string|array<string>|bool $input
2325
* @param string $type
2426
* @param string $expected
2527
* @return void
@@ -33,7 +35,7 @@ public function testEscapeValue($input, $type, $expected)
3335
}
3436

3537
/**
36-
* @return array<array{string|array<?string>,string,string}>
38+
* @return array<array{string|array<?string>|bool,string,string}>
3739
*/
3840
public function escapeValuesProvider()
3941
{
@@ -47,6 +49,49 @@ public function escapeValuesProvider()
4749
[['"foo"' => null] , '@column[]', '"foo"'],
4850
[['foo' => null, 'bar' => 'buz'] , '@column[]', 'foo,bar AS "buz"'],
4951
[['"foo"' => null, 'bar' => ''] , '@column[]', '"foo",bar'],
52+
[true, '@bool', 'true'],
53+
[false, '@bool', 'false'],
54+
];
55+
}
56+
57+
/**
58+
* @dataProvider escapeValuesUnexpectedValuesProvider
59+
* @phpstan-param string|array<string> $input
60+
* @param string $type
61+
* @phpstan-param array{class: class-string<\Exception>, message: non-empty-string} $expected
62+
* @return void
63+
*/
64+
public function testEscapeValue_raiseError($input, $type, array $expected)
65+
{
66+
$this->expectException($expected['class']);
67+
$this->expectExceptionMessage($expected['message']);
68+
69+
$pdo = new DummyPDO();
70+
$bind_values = [];
71+
$_ = $this->subject->escapeValue($pdo, ':key', $type, $input, $bind_values);
72+
}
73+
74+
/**
75+
* @return array<array{mixed, string, array{class: class-string<\Exception>, message: non-empty-string}}>
76+
*/
77+
public function escapeValuesUnexpectedValuesProvider()
78+
{
79+
/** @phpstan-var class-string<DomainException> $DomainException */
80+
$DomainException = 'DomainException';
81+
82+
return [
83+
[1, '@bool', ['class' => $DomainException, 'message' => 'param ":key" must be bool']],
84+
['', '@bool', ['class' => $DomainException, 'message' => 'param ":key" must be bool']],
85+
[null, '@bool', ['class' => $DomainException, 'message' => 'param ":key" must be bool']],
86+
['true', '@bool', ['class' => $DomainException, 'message' => 'param ":key" must be bool']],
87+
[
88+
['"foo"' => null, 'bar' => ''],
89+
'@table',
90+
[
91+
'class' => $DomainException,
92+
'message' => "Passed unexpected \$value as type '@table'. please check your query and parameters.",
93+
],
94+
],
5095
];
5196
}
5297

0 commit comments

Comments
 (0)