Skip to content

Commit

Permalink
Merge pull request #37 from Rojtjo/master
Browse files Browse the repository at this point in the history
Allow condition to be callable
  • Loading branch information
sebastiandedeyne committed Apr 4, 2017
2 parents 769f50c + e264ec8 commit 7827702
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 10 deletions.
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -30,6 +30,9 @@
}
},
"autoload-dev": {
"files": [
"tests/callables.php"
],
"psr-4": {
"Spatie\\Menu\\Test\\": "tests"
}
Expand Down
23 changes: 16 additions & 7 deletions src/Menu.php
Expand Up @@ -118,7 +118,7 @@ public function add(Item $item)
*/
public function addIf($condition, Item $item)
{
if ($condition) {
if ($this->resolveCondition($condition)) {
$this->add($item);
}

Expand Down Expand Up @@ -149,7 +149,7 @@ public function link(string $url, string $text)
*/
public function linkIf($condition, string $url, string $text)
{
if ($condition) {
if ($this->resolveCondition($condition)) {
$this->link($url, $text);
}

Expand Down Expand Up @@ -180,13 +180,22 @@ public function html(string $html, array $parentAttributes = [])
*/
public function htmlIf($condition, string $html, array $parentAttributes = [])
{
if ($condition) {
if ($this->resolveCondition($condition)) {
$this->html($html, $parentAttributes);
}

return $this;
}

/**
* @param $conditional
* @return bool
*/
protected function resolveCondition($conditional)
{
return is_callable($conditional) ? $conditional() : $conditional;
}

/**
* @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header
* @param callable|\Spatie\Menu\Menu|null $menu
Expand Down Expand Up @@ -355,9 +364,9 @@ public function prepend(string $prepend)
*
* @return $this
*/
public function prependIf(bool $condition, string $prepend)
public function prependIf($condition, string $prepend)
{
if ($condition) {
if ($this->resolveCondition($condition)) {
return $this->prepend($prepend);
}

Expand Down Expand Up @@ -387,9 +396,9 @@ public function append(string $append)
*
* @return static
*/
public function appendIf(bool $condition, string $append)
public function appendIf($condition, string $append)
{
if ($condition) {
if ($this->resolveCondition($condition)) {
return $this->append($append);
}

Expand Down
36 changes: 33 additions & 3 deletions tests/MenuAddTest.php
Expand Up @@ -110,11 +110,21 @@ public function it_can_conditionally_add_an_item()
{
$this->menu = Menu::new()
->addIf(true, Link::to('#', 'Foo'))
->addIf(false, Link::to('#', 'Bar'));
->addIf(false, Link::to('#', 'Bar'))
->addIf(function () {
return true;
}, Link::to('#', 'Baz'))
->addIf(function () {
return false;
}, Link::to('#', 'Qux'))
->addIf('is_true', Link::to('#', 'Quux'))
->addIf('is_false', Link::to('#', 'Quuz'));

$this->assertRenders('
<ul>
<li><a href="#">Foo</a></li>
<li><a href="#">Baz</a></li>
<li><a href="#">Quux</a></li>
</ul>
');
}
Expand All @@ -124,11 +134,21 @@ public function it_can_conditionally_add_a_link()
{
$this->menu = Menu::new()
->linkIf(true, '#', 'Foo')
->linkIf(false, '#', 'Bar');
->linkIf(false, '#', 'Bar')
->linkIf(function () {
return true;
}, '#', 'Baz')
->linkIf(function () {
return false;
}, '#', 'Qux')
->linkIf('is_true', '#', 'Quux')
->linkIf('is_false', '#', 'Quuz');

$this->assertRenders('
<ul>
<li><a href="#">Foo</a></li>
<li><a href="#">Baz</a></li>
<li><a href="#">Quux</a></li>
</ul>
');
}
Expand All @@ -138,11 +158,21 @@ public function it_can_conditionally_add_html()
{
$this->menu = Menu::new()
->htmlIf(true, 'Foo')
->htmlIf(false, 'Bar');
->htmlIf(false, 'Bar')
->htmlIf(function () {
return true;
}, 'Baz')
->htmlIf(function () {
return false;
}, 'Qux')
->htmlIf('is_true', 'Quux')
->htmlIf('is_false', 'Quuz');

$this->assertRenders('
<ul>
<li>Foo</li>
<li>Baz</li>
<li>Quux</li>
</ul>
');
}
Expand Down
60 changes: 60 additions & 0 deletions tests/MenuExtraHtmlTest.php
Expand Up @@ -15,6 +15,36 @@ public function it_can_prepend_content()
$this->assertRenders('<h1>Hi!</h1><ul></ul>');
}

public function prependIfDataProvider()
{
return [
[true, '<h1>Hi!</h1>', '<h1>Hi!</h1><ul></ul>'],
[false, '<h1>Hi!</h1>', '<ul></ul>'],
[function () {
return true;
}, '<h1>Hi!</h1>', '<h1>Hi!</h1><ul></ul>'],
[function () {
return false;
}, '<h1>Hi!</h1>', '<ul></ul>'],
['is_true', '<h1>Hi!</h1>', '<h1>Hi!</h1><ul></ul>'],
['is_false', '<h1>Hi!</h1>', '<ul></ul>'],
];
}

/**
* @test
* @dataProvider prependIfDataProvider
* @param \Closure|bool $condition
* @param string $prepend
* @param string $expected
*/
public function it_can_conditionally_prepend_content($condition, string $prepend, string $expected)
{
$this->menu = Menu::new()->prependIf($condition, $prepend);

$this->assertRenders($expected);
}

/** @test */
public function it_can_append_content()
{
Expand All @@ -23,6 +53,36 @@ public function it_can_append_content()
$this->assertRenders('<ul></ul><aside>Bye!</aside>');
}

public function appendIfDataProvider()
{
return [
[true, '<aside>Bye!</aside>', '<ul></ul><aside>Bye!</aside>'],
[false, '<aside>Bye!</aside>', '<ul></ul>'],
[function () {
return true;
}, '<aside>Bye!</aside>', '<ul></ul><aside>Bye!</aside>'],
[function () {
return false;
}, '<aside>Bye!</aside>', '<ul></ul>'],
['is_true', '<aside>Bye!</aside>', '<ul></ul><aside>Bye!</aside>'],
['is_false', '<aside>Bye!</aside>', '<ul></ul>'],
];
}

/**
* @test
* @dataProvider appendIfDataProvider
* @param \Closure|bool $condition
* @param string $prepend
* @param string $expected
*/
public function it_can_conditionally_append_content($condition, string $prepend, string $expected)
{
$this->menu = Menu::new()->appendIf($condition, $prepend);

$this->assertRenders($expected);
}

/** @test */
public function it_renders_classes()
{
Expand Down
17 changes: 17 additions & 0 deletions tests/callables.php
@@ -0,0 +1,17 @@
<?php

/**
* @return bool
*/
function is_true()
{
return true;
}

/**
* @return bool
*/
function is_false()
{
return false;
}

0 comments on commit 7827702

Please sign in to comment.