-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Critera api to exit a single group() level (#147)
It was decided that 306f813 was too risky a change that could be considered API breaking and thus a major version jump. To reduce this we can add a new method for explicitly escaping a single group level instead of making Criteria::end() do two different things depending on context. This adds the ability for authors to add multiple groups to their display Criteria, without interrupting people with nested group() rules that rely on end() to return the form field the rules apply to.
- Loading branch information
Showing
2 changed files
with
75 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace UncleCheese\DisplayLogic\Tests; | ||
|
||
use OutOfRangeException; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Forms\TextField; | ||
use UncleCheese\DisplayLogic\Criteria; | ||
|
||
class CriteriaTest extends SapphireTest | ||
{ | ||
public function testEndIsRecursiveAndReturnsTheFormField() | ||
{ | ||
$field = new TextField('test'); | ||
$displayRulesEnd = $field->displayIf('one')->isEmpty() | ||
->andIf() | ||
->group() | ||
->orIf('two')->isEqualTo('no') | ||
->orIf('two')->isEqualTo('non') | ||
->orIf() | ||
->group() // nested group | ||
->andIf('three')->isEqualTo('yes') | ||
->andIf('four')->isEqualTo('oui') | ||
->end(); | ||
$this->assertInstanceOf(TextField::class, $displayRulesEnd); | ||
} | ||
|
||
public function testEndGroupOnlyReturnsTheParentCriteria() | ||
{ | ||
$field = new TextField('test'); | ||
$displayRulesEnd = $field->displayIf('one')->isEmpty() | ||
->andIf() | ||
->group() | ||
->orIf('two')->isEqualTo('no') | ||
->orIf('two')->isEqualTo('non') | ||
->endGroup() | ||
->andIf() | ||
->group() | ||
->orIf('three')->isEqualTo('yes') | ||
->orIf('three')->isEqualTo('oui') | ||
->endGroup(); | ||
$this->assertInstanceOf(Criteria::class, $displayRulesEnd); | ||
} | ||
|
||
public function testEndGroupThrowsExceptionWhenThereIsNoParentCriteria() | ||
{ | ||
$this->expectException(OutOfRangeException::class); | ||
|
||
$field = new TextField('test'); | ||
$displayRulesEnd = $field->displayIf('one')->isEmpty()->endGroup(); | ||
} | ||
} |