diff --git a/src/Collections/TraversableUtilities.php b/src/Collections/TraversableUtilities.php new file mode 100644 index 0000000..8d7921c --- /dev/null +++ b/src/Collections/TraversableUtilities.php @@ -0,0 +1,27 @@ +count(); + } + + if ($countable === null || $countable === false) { + return 0; + } + + throw new TypeError; + } + +} diff --git a/tests/Collections/TraversableUtilitiesTest.php b/tests/Collections/TraversableUtilitiesTest.php new file mode 100644 index 0000000..d3c1073 --- /dev/null +++ b/tests/Collections/TraversableUtilitiesTest.php @@ -0,0 +1,55 @@ +assertEquals( + $expectedCount, TraversableUtilities::count($countable) + ); + } + + /** + * @dataProvider provideNotCountables + */ + public function testCountWithNotCountables ($notCountable) { + $this->expectException("TypeError"); + TraversableUtilities::count($notCountable); + } + + /// + /// Data providers + /// + + public function provideCountables () : iterable { + yield [0, null]; + yield [0, false]; + yield [0, []]; + yield [3, ["a", "b", "c"]]; + yield [42, new class implements Countable { + public function count () : int { + return 42; + } + } + ]; + } + + public function provideNotCountables () : iterable { + yield [true]; + yield [new \stdClass]; + yield [0]; + yield [""]; + yield ["abc"]; + } + +}