-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #387 from Florin1/add_callback_voter
Add callback voter
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 3.5 (unreleased) | ||
|
||
* Added CallbackVoter | ||
|
||
## 3.4 (2023-05-17) | ||
|
||
* Removed support for unsupported PHP version 7.4 | ||
|
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,26 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Matcher\Voter; | ||
|
||
use Knp\Menu\ItemInterface; | ||
|
||
/** | ||
* Voter based on a callback | ||
*/ | ||
final class CallbackVoter implements VoterInterface | ||
{ | ||
public function matchItem(ItemInterface $item): ?bool | ||
{ | ||
$callback = $item->getExtra('match_callback'); | ||
|
||
if (null === $callback) { | ||
return null; | ||
} | ||
|
||
if (!\is_callable($callback)) { | ||
throw new \InvalidArgumentException('Extra "match_callback" must be callable.'); | ||
} | ||
|
||
return $callback(); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Tests\Matcher\Voter; | ||
|
||
use Knp\Menu\ItemInterface; | ||
use Knp\Menu\Matcher\Voter\CallbackVoter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class CallbackVoterTest extends TestCase | ||
{ | ||
public function testNoMatchCallbackSet(): void | ||
{ | ||
$item = $this->createMock(ItemInterface::class); | ||
$item->expects($this->once()) | ||
->method('getExtra') | ||
->with('match_callback') | ||
->willReturn(null); | ||
|
||
$voter = new CallbackVoter(); | ||
|
||
$this->assertNull($voter->matchItem($item)); | ||
} | ||
|
||
public function testMatchCallbackIsNotCallable(): void | ||
{ | ||
$item = $this->createMock(ItemInterface::class); | ||
$item->expects($this->once()) | ||
->method('getExtra') | ||
->with('match_callback') | ||
->willReturn('foo'); | ||
|
||
$voter = new CallbackVoter(); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
|
||
$voter->matchItem($item); | ||
} | ||
|
||
/** | ||
* @dataProvider provideData | ||
*/ | ||
public function testMatching(callable $callback, ?bool $expected): void | ||
{ | ||
$item = $this->createMock(ItemInterface::class); | ||
$item->expects($this->once()) | ||
->method('getExtra') | ||
->with('match_callback') | ||
->willReturn($callback); | ||
|
||
$voter = new CallbackVoter(); | ||
|
||
$this->assertSame($expected, $voter->matchItem($item)); | ||
} | ||
|
||
/** | ||
* @return iterable<string, array{callable(): ?bool, ?bool}> | ||
*/ | ||
public static function provideData(): iterable | ||
{ | ||
yield 'matching' => [fn (): ?bool => true, true]; | ||
yield 'not matching' => [fn (): ?bool => false, false]; | ||
yield 'skipping' => [fn (): ?bool => null, null]; | ||
} | ||
} |