Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor by infection #6

Merged
merged 4 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,23 @@ protected static function replaceHolder($pdo, $key, $type, $value, &$bind_values
}

if ($type === '@int') {
if (is_int($value)) {
return $value;
}

if (!is_numeric($value)) {
throw new \DomainException(sprintf('param "%s" must be numeric', $key));
}

$s = (string)$value;
if ($s < self::INT64_MIN || self::INT64_MAX < $s) {
if ($value < self::INT64_MIN || self::INT64_MAX < $value) {
throw new \DomainException(sprintf('param "%s" is integer out of range.', $key));
}

$is_zero = $s === 0 || $s === '0';

if (!$is_zero && !preg_match('/\A-?[1-9][0-9]*\z/', $s)) {
throw new \DomainException();
if ($value !== '0' && !preg_match('/\A-?[1-9][0-9]*\z/', $value)) {
throw new \DomainException(sprintf('param "%s" is unexpected integer notation.', $key));
}

return (int)$s;
return (int)$value;
}

if ($type === '@int[]') {
Expand All @@ -128,7 +129,7 @@ protected static function replaceHolder($pdo, $key, $type, $value, &$bind_values
throw new \LogicException('Validation Error.');
}

if (!preg_match('/\A(?:-?[1-9][0-9]*)(?:,-?[1-9][0-9]*)*\z/', $valuesString)) {
if ($value !== '0' && !preg_match('/\A(?:-?[1-9][0-9]*)(?:,-?[1-9][0-9]*)*\z/', $valuesString)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #4, but forgot to fix.

throw new \DomainException(sprintf('param "%s[%]"'));
}

Expand Down Expand Up @@ -168,7 +169,7 @@ protected static function replaceHolder($pdo, $key, $type, $value, &$bind_values
return $key;
}

if ($type === '') {
if ($type === '' || $type === '@') {
throw new \DomainException(sprintf('type specifier for param "%s" not found', $key));
} else {
throw new \DomainException(sprintf('unexpected type "%s"', $type));
Expand Down
89 changes: 89 additions & 0 deletions tests/Query/ReplaceHolderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Teto\SQL\Query;

use Teto\SQL\Query;
use Teto\SQL\DummyPDO;

/**
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2019 USAMI Kenta
* @license https://github.com/BaguettePHP/TetoSQL/blob/master/LICENSE MPL-2.0
*/
final class ReplaceHolderTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider acceptDataProvider
*/
public function test_accept($type, $input, $expected)
{
$pdo = new DummyPDO();

$actual = call_user_func(\Closure::bind(function () use ($pdo, $type, $input) {
return Query::replaceHolder($pdo, ':key', "@{$type}", $input, $bind_values);
}, null, Query::class));

$this->assertSame($expected, $actual);
}

public function acceptDataProvider()
{
return [
['ascdesc', 'ASC', 'ASC'],
['ascdesc', 'DESC', 'DESC'],
['ascdesc', 'asc', 'asc'],
['ascdesc', 'desc', 'desc'],
['int', 1, 1],
['int', '1', 1],
['int', 0, 0],
['int', '0', 0],
['int', '9223372036854775807', 9223372036854775807],
['int', '-9223372036854775808', (int)'-9223372036854775808'],
['int[]', [1, 2, 3], '1,2,3'],
['int[]', ['1', '2', '3'], '1,2,3'],
['int[]',
['9223372036854775807', '-9223372036854775808'],
'9223372036854775807,-9223372036854775808',
],
['string', 0, '@0@'],
['string', '0', '@0@'],
['string', '', '@@'],
['string[]', ['', ''], '@@,@@'],
];
}

/**
* @dataProvider rejeceptDataProvider
*/
public function test_raise_exception($type, $input, $expected_message)
{
$pdo = new DummyPDO();

$this->expectException(\DomainException::class);
$this->expectExceptionMessage($expected_message);

call_user_func(\Closure::bind(function () use ($pdo, $type, $input) {
return Query::replaceHolder($pdo, ':key', $type, $input, $bind_values);
}, null, Query::class));
}

public function rejeceptDataProvider()
{
return [
['', null, 'type specifier for param ":key" not found'],
['@', null, 'type specifier for param ":key" not found'],
['@piyo', null, 'unexpected type "@piyo"'],
['@ascdesc', 'foo', 'param ":key" must be "ASC", "DESC", "asc" or "desc"'],
['@int', '-0', 'param ":key" is unexpected integer notation'],
['@int', '9223372036854775808', 'param ":key" is integer out of range.'],
['@int', '-9223372036854775809', 'param ":key" is integer out of range.'],
['@int[]', 0, 'param ":key" must be int array'],
['@int[]', [], 'param ":key" must be not empty int array'],
['@int[]', ['1', 'a', '3'], 'param ":key[]" is integer out of range.'],
['@string', [], 'param ":key" must be string or numeric'],
['@string[]', '', 'param ":key" must be string array'],
['@string[]', [], 'param ":key" must be not empty string array'],
['@string[]', ['', null], 'element of param ":key" must be string or numeric'],
];
}
}
2 changes: 1 addition & 1 deletion tests/SQLite/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setUp()

public function getPDO()
{
if (is_null($this->pdo)) {
if ($this->pdo === null) {
$dsn = 'sqlite:/' . __DIR__ . '/db.sq3';
$this->pdo = new \PDO($dsn, null, null, [\PDO::ATTR_PERSISTENT => true]);
}
Expand Down