Skip to content

Commit c024f00

Browse files
committed
test parameters type validity
1 parent 727523e commit c024f00

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Migrations\Query\Exception;
6+
7+
use Doctrine\Migrations\Exception\MigrationException;
8+
use InvalidArgumentException;
9+
use function sprintf;
10+
11+
class InvalidArguments extends InvalidArgumentException implements MigrationException
12+
{
13+
public static function wrongTypesArgumentCount(string $statement, int $parameters, int $types) : self
14+
{
15+
return new self(sprintf(
16+
'The number of types (%s) is higher than the number of passed parameters (%s) for the query "%s".',
17+
$types,
18+
$parameters,
19+
$statement
20+
));
21+
}
22+
}

lib/Doctrine/Migrations/Query/Query.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Doctrine\Migrations\Query;
66

7+
use Doctrine\Migrations\Query\Exception\InvalidArguments;
8+
use function count;
9+
710
/**
811
* The Query wraps the sql query, parameters and types.
912
*/
@@ -24,6 +27,10 @@ final class Query
2427
*/
2528
public function __construct(string $statement, array $parameters = [], array $types = [])
2629
{
30+
if (count($types) > count($parameters)) {
31+
throw InvalidArguments::wrongTypesArgumentCount($statement, count($parameters), count($types));
32+
}
33+
2734
$this->statement = $statement;
2835
$this->parameters = $parameters;
2936
$this->types = $types;

tests/Doctrine/Migrations/Tests/Query/QueryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\Migrations\Tests\Query;
66

7+
use Doctrine\Migrations\Query\Exception\InvalidArguments;
78
use Doctrine\Migrations\Query\Query;
89
use Doctrine\Migrations\Tests\MigrationTestCase;
910

@@ -18,4 +19,12 @@ public function testGetQuery() : void
1819
self::assertSame(['bar', 'baz'], $query->getParameters());
1920
self::assertSame(['qux', 'quux'], $query->getTypes());
2021
}
22+
23+
public function testInvalidTypeArguments() : void
24+
{
25+
$this->expectException(InvalidArguments::class);
26+
$this->expectExceptionMessage('The number of types (2) is higher than the number of passed parameters (1) for the query "Select 1".');
27+
28+
new Query('Select 1', ['bar'], ['qux', 'quux']);
29+
}
2130
}

0 commit comments

Comments
 (0)