Skip to content

Commit 389cd2b

Browse files
authored
[8.x] Add AssertableJson::each() method (#38684)
* add AssertableJson::each() method * add tests for new method
1 parent 0ea3bb6 commit 389cd2b

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/Illuminate/Testing/Fluent/AssertableJson.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,32 @@ public function first(Closure $callback): self
117117
return $this->scope($key, $callback);
118118
}
119119

120+
/**
121+
* Instantiate a new "scope" on each child element.
122+
*
123+
* @param \Closure $callback
124+
* @return $this
125+
*/
126+
public function each(Closure $callback): self
127+
{
128+
$props = $this->prop();
129+
130+
$path = $this->dotPath();
131+
132+
PHPUnit::assertNotEmpty($props, $path === ''
133+
? 'Cannot scope directly onto each element of the root level because it is empty.'
134+
: sprintf('Cannot scope directly onto each element of property [%s] because it is empty.', $path)
135+
);
136+
137+
foreach (array_keys($props) as $key) {
138+
$this->interactsWith($key);
139+
140+
$this->scope($key, $callback);
141+
}
142+
143+
return $this;
144+
}
145+
120146
/**
121147
* Create a new instance from an array.
122148
*

tests/Testing/Fluent/AssertTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,64 @@ public function testFirstScopeFailsWhenPropSingleValue()
699699
});
700700
}
701701

702+
public function testEachScope()
703+
{
704+
$assert = AssertableJson::fromArray([
705+
'foo' => [
706+
'key' => 'first',
707+
],
708+
'bar' => [
709+
'key' => 'second',
710+
],
711+
]);
712+
713+
$assert->each(function (AssertableJson $item) {
714+
$item->whereType('key', 'string');
715+
});
716+
}
717+
718+
public function testEachScopeFailsWhenNoProps()
719+
{
720+
$assert = AssertableJson::fromArray([]);
721+
722+
$this->expectException(AssertionFailedError::class);
723+
$this->expectExceptionMessage('Cannot scope directly onto each element of the root level because it is empty.');
724+
725+
$assert->each(function (AssertableJson $item) {
726+
//
727+
});
728+
}
729+
730+
public function testEachNestedScopeFailsWhenNoProps()
731+
{
732+
$assert = AssertableJson::fromArray([
733+
'foo' => [],
734+
]);
735+
736+
$this->expectException(AssertionFailedError::class);
737+
$this->expectExceptionMessage('Cannot scope directly onto each element of property [foo] because it is empty.');
738+
739+
$assert->has('foo', function (AssertableJson $assert) {
740+
$assert->each(function (AssertableJson $item) {
741+
//
742+
});
743+
});
744+
}
745+
746+
public function testEachScopeFailsWhenPropSingleValue()
747+
{
748+
$assert = AssertableJson::fromArray([
749+
'foo' => 'bar',
750+
]);
751+
752+
$this->expectException(AssertionFailedError::class);
753+
$this->expectExceptionMessage('Property [foo] is not scopeable.');
754+
755+
$assert->each(function (AssertableJson $item) {
756+
//
757+
});
758+
}
759+
702760
public function testFailsWhenNotInteractingWithAllPropsInScope()
703761
{
704762
$assert = AssertableJson::fromArray([

0 commit comments

Comments
 (0)