Skip to content

Commit

Permalink
Merge f3f4fac into 05c44e7
Browse files Browse the repository at this point in the history
  • Loading branch information
mrceperka authored Oct 24, 2022
2 parents 05c44e7 + f3f4fac commit 8f3115a
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ parameters:
-
message: '~Call to an undefined method .+::children\(\)\.~'
path: "src/Bridges/SymfonyBundle/DependencyInjection/Configuration.php"
-
message: '~Class BackedEnum not found\.~'
path: "src/SqlProcessor.php"
-
message: '~Access to property \$value on an unknown class BackedEnum\.~'
path: "src/SqlProcessor.php"

5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
4 changes: 4 additions & 0 deletions src/SqlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ function ($matches) use ($args, &$j, $last): string {
*/
public function processModifier(string $type, $value): string
{
if ($value instanceof \BackedEnum) {
$value = $value->value;
}

switch (gettype($value)) {
case 'string':
switch ($type) {
Expand Down
139 changes: 139 additions & 0 deletions tests/cases/unit/SqlProcessorTest.backed_enum.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php declare(strict_types = 1);

/**
* @testCase
* @phpVersion >= 8.1
*/

namespace NextrasTests\Dbal;


use Mockery;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\Dbal\SqlProcessor;
use Tester\Assert;


require_once __DIR__ . '/../../bootstrap.php';


enum DirectionEnum: string
{
case LEFT = 'left';
case RIGHT = 'right';
}


enum BinaryEnum: int
{
case ZERO = 0;
case ONE = 1;
}


class SqlProcessorBackedEnumTest extends TestCase
{
/** @var IPlatform|Mockery\MockInterface */
private $platform;

/** @var IDriver|Mockery\MockInterface */
private $driver;

/** @var SqlProcessor */
private $parser;


protected function setUp()
{
parent::setUp();
$this->driver = Mockery::mock(IDriver::class);
$this->platform = \Mockery::mock(IPlatform::class);
$this->parser = new SqlProcessor($this->driver, $this->platform);
}


public function testString()
{
$cases = DirectionEnum::cases();
foreach ($cases as $case) {
$this->driver->shouldReceive('convertStringToSql')->once()->with($case->value)->andReturn('hit');
Assert::same('hit', $this->parser->processModifier('s', $case));
}

$cases = BinaryEnum::cases();
foreach ($cases as $case) {
Assert::exception(
function () use ($case) {
$this->parser->processModifier('s', $case);
},
InvalidArgumentException::class,
'Modifier %s expects value to be string, integer given.'
);
}

}


public function testStringArray()
{

$cases = DirectionEnum::cases();
$this->driver->shouldReceive('convertStringToSql')->times(count($cases))
->andReturnArg(0);
Assert::same('(left, right)', $this->parser->processModifier('s[]', $cases));

Assert::exception(
function () {
$cases = BinaryEnum::cases();
$this->parser->processModifier('s[]', $cases);
},
InvalidArgumentException::class,
'Modifier %s expects value to be string, integer given.'
);

}


public function testInt()
{
$cases = DirectionEnum::cases();
foreach ($cases as $case) {
Assert::exception(
function () use ($case) {
$this->parser->processModifier('i', $case);
},
InvalidArgumentException::class,
'Modifier %i expects value to be int, string given.'
);
}

$cases = BinaryEnum::cases();
foreach ($cases as $case) {
Assert::same((string) $case->value, $this->parser->processModifier('i', $case));
}

}


public function testIntArray()
{
$cases = DirectionEnum::cases();
Assert::exception(
function () use ($cases) {
$this->parser->processModifier('i[]', $cases);
},
InvalidArgumentException::class,
'Modifier %i expects value to be int, string given.'
);

$cases = BinaryEnum::cases();
Assert::same('(0, 1)', $this->parser->processModifier('i[]', $cases));

}
}


$test = new SqlProcessorBackedEnumTest();
$test->run();

0 comments on commit 8f3115a

Please sign in to comment.