Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 30, 2021
1 parent 7705b94 commit 0567c27
Showing 1 changed file with 91 additions and 57 deletions.
148 changes: 91 additions & 57 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,92 +58,126 @@ public function it_can_append(): void

public function it_can_apply(): void
{
$input = array_combine(range('A', 'Z'), range('A', 'Z'));
$input = range('a', 'e');
$stack = [];

$this::fromIterable($input)
->apply(static function ($item) {
// do what you want here.
->apply(
static function ($item) use (&$stack): bool {
$stack += [$item => []];
$stack[$item][] = 'fn1';

return true;
})
return true;
}
)
->shouldIterateAs($input);

$this::fromIterable($input)
->apply(static function ($item) {
// do what you want here.
$expected = [
'a' => ['fn1'],
'b' => ['fn1'],
'c' => ['fn1'],
'd' => ['fn1'],
'e' => ['fn1'],
];

return false;
})
->shouldIterateAs($input);
if ($stack !== $expected) {
throw new Exception('Error');
}

$this::fromIterable($input)
->apply(
static function ($item) {
return $item;
}
)
->shouldIterateAs($input);
// ---

$stack = [];

$this::fromIterable($input)
->apply(
static function ($item) {
static function ($item) use (&$stack): bool {
$stack += [$item => []];
$stack[$item][] = 'fn1';

return false;
}
)
->shouldReturnAnInstanceOf(Collection::class);
->shouldIterateAs($input);

$callback = static function (): void {
throw new Exception('foo');
};
$expected = [
'a' => ['fn1'],
];

$this::fromIterable($input)
->apply($callback)
->shouldThrow(Exception::class)
->during('all');
if ($stack !== $expected) {
throw new Exception('Error');
}

$apply1 = static function ($value) {
return true === $value % 2;
};
// ---

$apply2 = static function ($value) {
return true === $value % 3;
};
$stack = [];

$this::fromIterable([1, 2, 3, 4, 5, 6])
->apply($apply1)
->apply($apply2)
->shouldIterateAs([1, 2, 3, 4, 5, 6]);
$this::fromIterable($input)
->apply(
static function ($item) use (&$stack): bool {
$stack += [$item => []];
$stack[$item][] = 'fn1';

$a = (new stdClass());
$b = (new stdClass());
$c = (new stdClass());
return true;
},
static function ($item) use (&$stack): bool {
$stack += [$item => []];
$stack[$item][] = 'fn2';

$a->prop = 'a';
$b->prop = 'b';
$c->prop = 'c';
return true;
}
)
->shouldIterateAs($input);

$input = [
$a, $b, $c,
$expected = [
'a' => ['fn1', 'fn2'],
'b' => ['fn1', 'fn2'],
'c' => ['fn1', 'fn2'],
'd' => ['fn1', 'fn2'],
'e' => ['fn1', 'fn2'],
];

if ($stack !== $expected) {
throw new Exception('Error');
}

// ---

$stack = [];

$this::fromIterable($input)
->apply(
static function ($value, $key): bool {
$value->prop = $key;
static function ($item) use (&$stack): bool {
$stack += [$item => []];
$stack[$item][] = 'fn1';

if ('c' === $item) {
return false;
}

return true;
},
static function ($item) use (&$stack): bool {
$stack += [$item => []];
$stack[$item][] = 'fn2';

if ('b' === $item) {
return false;
}

return true;
}
)
->map(
static function ($value): int {
return $value->prop;
}
)
->shouldIterateAs([
0,
1,
2,
]);
->shouldIterateAs($input);

$expected = [
'a' => ['fn1', 'fn2'],
'b' => ['fn1', 'fn2'],
'c' => ['fn1'],
];

if ($stack !== $expected) {
throw new Exception('Error');
}
}

public function it_can_associate(): void
Expand Down

0 comments on commit 0567c27

Please sign in to comment.