Skip to content

Commit

Permalink
Fix broken snake-case conversion of __call use-cases (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhermoso committed Jul 7, 2023
1 parent 645628d commit 3742232
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Nodes/AbstractNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,13 @@ public function __call(string $methodName, array $arguments)
*/
foreach ($arguments as $arg) {
if ($arg instanceof PathExp || $arg instanceof self) {
$rule = new LazyRuleStringify($methodName, $arguments);
$rule = new LazyRuleStringify($ruleName, $arguments);
break;
}
}

if (!$rule instanceof LazyRuleStringify) {
$rule = sprintf('%s:%s', $methodName, implode(',', $arguments));
$rule = sprintf('%s:%s', $ruleName, implode(',', $arguments));
}

$this->rules[] = $rule;
Expand Down
61 changes: 61 additions & 0 deletions tests/AutoSnakeCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Square\Hyrule\Tests;

use Monolog\Test\TestCase;
use Square\Hyrule\Hyrule;
use Square\Hyrule\Nodes\StringNode;

class AutoSnakeCaseTest extends TestCase
{
/**
* @param string $camelCase
* @param array<mixed> $args
* @param string $expected
* @return void
* @dataProvider data
*/
public function testAutoSnakeCase(string $camelCase, array $args, string $expected): void
{
$node = Hyrule::create()
->string('foo')
->$camelCase(...$args);
assert($node instanceof StringNode);
$rules = $node->build();
$this->assertEquals($expected, $rules['foo'][1]);
}

/**
* @return array<mixed>
*/
public static function data(): array
{
return [
[
'hello',
[],
'hello',
],
[
'helloWorld',
[],
'hello_world',
],
[
'helloWorld',
[1, 2, 3],
'hello_world:1,2,3',
],
[
'helloBeautifulWorld',
[],
'hello_beautiful_world',
],
[
'helloBeautifulWorld',
['a', 'b', 'c'],
'hello_beautiful_world:a,b,c',
],
];
}
}

0 comments on commit 3742232

Please sign in to comment.