Skip to content
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
6 changes: 4 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ public function execute(): mixed
*
* The $strategy parameter must be a valid callable.
*/
public function getBehavior(string|callable $slug, mixed $strategy = self::DEFAULT_STRATEGY): BehaviorInterface
{
public function getBehavior(
string|callable|array $slug,
mixed $strategy = self::DEFAULT_STRATEGY
): BehaviorInterface {
$this->logger->debug('Swivel - Creating new behavior.', compact('slug'));
if ($strategy === static::DEFAULT_STRATEGY) {
$strategy = $slug;
Expand Down
2 changes: 1 addition & 1 deletion src/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function defaultValue(mixed $value): BuilderInterface;
/**
* Creates a new Behavior object with an attached strategy.
*/
public function getBehavior(string|callable $slug, mixed $strategy = null): BehaviorInterface;
public function getBehavior(string|callable|array $slug, mixed $strategy = null): BehaviorInterface;

/**
* Indicates that the feature has no default behavior.
Expand Down
36 changes: 36 additions & 0 deletions test/Tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,42 @@ public function testDefaultValue()
$this->assertInstanceOf(BuilderInterface::class, $builder->defaultValue(null));
}

public function testGetBehaviorAcceptsProtectedMethodCallableAsStrategy()
{
$map = $this->getMockBuilder(Map::class)
->getMock();
$bucket = $this->getMockBuilder(Bucket::class)
->setConstructorArgs([$map])
->getMock();
$builder = new Builder('Test', $bucket);
$builder->setLogger(new NullLogger());

// defaultBehavior() and Manager::invoke() pass the strategy as the first argument; an array callable to a
// protected method is not `callable` from this scope and must still be accepted (regression from 6.0.0)
$behavior = $builder->getBehavior([$this, 'protectedMethod']);
$this->assertInstanceOf(Behavior::class, $behavior);
$this->assertSame('Test'.Map::DELIMITER.Builder::DEFAULT_SLUG, $behavior->getSlug());
$this->assertEquals('ArgaArgb', $behavior->execute(['Arga', 'Argb']));
}

public function testDefaultBehaviorAcceptsProtectedMethodCallable()
{
$map = $this->getMockBuilder(Map::class)
->getMock();
$bucket = $this->getMockBuilder(Bucket::class)
->setConstructorArgs([$map])
->getMock();
$bucket->method('enabled')->willReturn(false);
$builder = new Builder('Test', $bucket);
$builder->setLogger(new NullLogger());

$result = $builder
->addBehavior('a', [$this, 'privateMethod'], ['x', 'y'])
->defaultBehavior([$this, 'protectedMethod'], ['Arga', 'Argb'])
->execute();
$this->assertEquals('ArgaArgb', $result, 'The protected default behavior runs when no behavior is enabled');
}

public function testDefaultBehaviorThrowsIfNoDefaultCalledFirst()
{
$this->expectException(LogicException::class);
Expand Down
Loading