Skip to content

Commit 320a760

Browse files
Tests for newly added support for callable with arguments
1 parent 753a69e commit 320a760

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
// phpcs:ignoreFile
4+
declare(strict_types=1);
5+
6+
namespace Cycle\ORM\Tests\Fixtures;
7+
8+
use Cycle\Database\DatabaseInterface;
9+
10+
class StaticCallableRule
11+
{
12+
public static function invoke(string $value, DatabaseInterface $database, array $arguments): array
13+
{
14+
return [
15+
'value' => $value,
16+
'database' => $database,
17+
'arguments' => $arguments,
18+
];
19+
}
20+
}

tests/ORM/Unit/Parser/TypecastTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Cycle\ORM\Tests\Fixtures\Enum\CustomStringable;
1111
use Cycle\ORM\Tests\Fixtures\Enum\TypeIntEnum;
1212
use Cycle\ORM\Tests\Fixtures\Enum\TypeStringEnum;
13+
use Cycle\ORM\Tests\Fixtures\StaticCallableRule;
1314
use Cycle\ORM\Tests\Fixtures\Uuid;
1415
use Mockery as m;
1516
use PHPUnit\Framework\TestCase;
@@ -29,6 +30,7 @@ public function testSetRules(): void
2930
'slug' => fn(string $value) => strtolower($value),
3031
'title' => 'strtoupper',
3132
'test' => [Uuid::class, 'create'],
33+
'callable' => [StaticCallableRule::class, 'invoke', ['foo' => 'bar']],
3234
'uuid' => 'uuid',
3335
'settings' => 'json',
3436
];
@@ -151,6 +153,21 @@ public function testCastCallableValue(): void
151153
$this->assertSame('71ceb213-ec3d-4ae5-911b-ba042abfb204', $result['uuid']->toString());
152154
}
153155

156+
public function testCastCallableWithArguments(): void
157+
{
158+
$this->typecast->setRules(['callable' => [StaticCallableRule::class, 'invoke', ['foo' => 'bar']]]);
159+
160+
$result = $this->typecast->cast(['callable' => 'baz']);
161+
162+
$this->assertIsArray($result['callable']);
163+
$this->assertArrayHasKey('value', $result['callable']);
164+
$this->assertArrayHasKey('database', $result['callable']);
165+
$this->assertArrayHasKey('arguments', $result['callable']);
166+
$this->assertSame('baz', $result['callable']['value']);
167+
$this->assertInstanceOf(DatabaseInterface::class, $result['callable']['database']);
168+
$this->assertSame(['foo' => 'bar'], $result['callable']['arguments']);
169+
}
170+
154171
public function testCastJsonValue(): void
155172
{
156173
$this->typecast->setRules(['foo' => 'json', 'baz' => 'json']);

0 commit comments

Comments
 (0)