-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4aea63
commit f935ea7
Showing
4 changed files
with
146 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Kirby\Query\Runners; | ||
|
||
use Exception; | ||
|
||
class FooObj { | ||
public string $bax = 'qox'; | ||
|
||
public function print(string $string = 'bar'): string | ||
{ | ||
return $string; | ||
} | ||
} | ||
|
||
/** | ||
* @coversDefaultClass \Kirby\Query\Runners\Runtime | ||
*/ | ||
class RuntimeTest extends TestCase | ||
{ | ||
/** | ||
* @covers ::access | ||
*/ | ||
public function testAccessWithArray(): void | ||
{ | ||
$array = [ | ||
'bar' => 'bar', | ||
'bax' => fn (string $string = 'bax') => $string | ||
]; | ||
|
||
$result = Runtime::access($array, 'bar'); | ||
$this->assertSame('bar', $result); | ||
|
||
$result = Runtime::access($array, 'bax'); | ||
$this->assertSame('bax', $result); | ||
|
||
$result = Runtime::access($array, 'bax', false, 'custom'); | ||
$this->assertSame('custom', $result); | ||
|
||
$result = Runtime::access($array, 'fox'); | ||
$this->assertNull($result); | ||
} | ||
|
||
/** | ||
* @covers ::access | ||
*/ | ||
public function testAccessWithObject(): void | ||
{ | ||
$obj = new FooObj; | ||
$result = Runtime::access($obj, 'print'); | ||
$this->assertSame('bar', $result); | ||
|
||
$result = Runtime::access($obj, 'print', false, 'custom'); | ||
$this->assertSame('custom', $result); | ||
|
||
$result = Runtime::access($obj, 'bax'); | ||
$this->assertSame('qox', $result); | ||
|
||
$result = Runtime::access($obj, 'fox'); | ||
$this->assertNull($result); | ||
} | ||
|
||
/** | ||
* @covers ::access | ||
*/ | ||
public function testAccessWithNullSafe(): void | ||
{ | ||
$result = Runtime::access(null, 'bar', true); | ||
$this->assertNull($result); | ||
} | ||
|
||
/** | ||
* @covers ::access | ||
*/ | ||
public function testAccessWithoutNullSafe(): void | ||
{ | ||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage('Cannot access "bar" on NULL'); | ||
Runtime::access(null, 'bar', false); | ||
} | ||
|
||
/** | ||
* @covers ::get | ||
*/ | ||
public function testGet(): void | ||
{ | ||
$context = ['foo' => 'bar', 'qox' => fn () => 'bax']; | ||
$functions = ['fox' => fn () => 'fax']; | ||
|
||
$result = Runtime::get('foo', $context); | ||
$this->assertSame('bar', $result); | ||
|
||
$result = Runtime::get('qox', $context); | ||
$this->assertSame('bax', $result); | ||
|
||
$result = Runtime::get('fox', $context); | ||
$this->assertNull($result); | ||
|
||
$result = Runtime::get('fox', $context, $functions); | ||
$this->assertSame('fax', $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters