Skip to content

Commit

Permalink
test parameters type validity
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Jan 20, 2020
1 parent 727523e commit c024f00
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/Doctrine/Migrations/Query/Exception/InvalidArguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Query\Exception;

use Doctrine\Migrations\Exception\MigrationException;
use InvalidArgumentException;
use function sprintf;

class InvalidArguments extends InvalidArgumentException implements MigrationException
{
public static function wrongTypesArgumentCount(string $statement, int $parameters, int $types) : self
{
return new self(sprintf(
'The number of types (%s) is higher than the number of passed parameters (%s) for the query "%s".',
$types,
$parameters,
$statement
));
}
}
7 changes: 7 additions & 0 deletions lib/Doctrine/Migrations/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Doctrine\Migrations\Query;

use Doctrine\Migrations\Query\Exception\InvalidArguments;
use function count;

/**
* The Query wraps the sql query, parameters and types.
*/
Expand All @@ -24,6 +27,10 @@ final class Query
*/
public function __construct(string $statement, array $parameters = [], array $types = [])
{
if (count($types) > count($parameters)) {
throw InvalidArguments::wrongTypesArgumentCount($statement, count($parameters), count($types));
}

$this->statement = $statement;
$this->parameters = $parameters;
$this->types = $types;
Expand Down
9 changes: 9 additions & 0 deletions tests/Doctrine/Migrations/Tests/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Migrations\Tests\Query;

use Doctrine\Migrations\Query\Exception\InvalidArguments;
use Doctrine\Migrations\Query\Query;
use Doctrine\Migrations\Tests\MigrationTestCase;

Expand All @@ -18,4 +19,12 @@ public function testGetQuery() : void
self::assertSame(['bar', 'baz'], $query->getParameters());
self::assertSame(['qux', 'quux'], $query->getTypes());
}

public function testInvalidTypeArguments() : void
{
$this->expectException(InvalidArguments::class);
$this->expectExceptionMessage('The number of types (2) is higher than the number of passed parameters (1) for the query "Select 1".');

new Query('Select 1', ['bar'], ['qux', 'quux']);
}
}

0 comments on commit c024f00

Please sign in to comment.