From 2c2b28af9573e20dc94b983c1d4dd02597a3c788 Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Tue, 2 Jan 2024 16:19:32 -0700 Subject: [PATCH] 100% code coverage everywhere now --- flight/util/Collection.php | 2 +- tests/CollectionTest.php | 98 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/CollectionTest.php diff --git a/flight/util/Collection.php b/flight/util/Collection.php index 5998b25..01ddb81 100644 --- a/flight/util/Collection.php +++ b/flight/util/Collection.php @@ -16,7 +16,7 @@ use Iterator; use JsonSerializable; if (!interface_exists('JsonSerializable')) { - require_once __DIR__ . '/LegacyJsonSerializable.php'; + require_once __DIR__ . '/LegacyJsonSerializable.php'; // @codeCoverageIgnore } /** diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php new file mode 100644 index 0000000..8d6ba76 --- /dev/null +++ b/tests/CollectionTest.php @@ -0,0 +1,98 @@ + + * @license MIT, http://flightphp.com/license + */ +class CollectionTest extends PHPUnit\Framework\TestCase +{ + /** + * @var \flight\util\Collection + */ + private $collection; + + protected function setUp(): void + { + $this->collection = new \flight\util\Collection(['a' => 1, 'b' => 2]); + } + + // Get an item + public function testGet() + { + $this->assertEquals(1, $this->collection->a); + } + + // Set an item + public function testSet() + { + $this->collection->c = 3; + $this->assertEquals(3, $this->collection->c); + } + + // Check if an item exists + public function testExists() + { + $this->assertTrue(isset($this->collection->a)); + } + + // Unset an item + public function testUnset() + { + unset($this->collection->a); + $this->assertFalse(isset($this->collection->a)); + } + + // Count items + public function testCount() + { + $this->assertEquals(2, count($this->collection)); + } + + // Iterate through items + public function testIterate() + { + $items = []; + foreach ($this->collection as $key => $value) { + $items[$key] = $value; + } + + $this->assertEquals(['a' => 1, 'b' => 2], $items); + } + + public function testJsonSerialize() + { + $this->assertEquals(['a' => 1, 'b' => 2], $this->collection->jsonSerialize()); + } + + public function testOffsetSetWithNullOffset() { + $this->collection->offsetSet(null, 3); + $this->assertEquals(3, $this->collection->offsetGet(0)); + } + + public function testOffsetExists() { + $this->collection->a = 1; + $this->assertTrue($this->collection->offsetExists('a')); + } + + public function testOffsetUnset() { + $this->collection->a = 1; + $this->assertTrue($this->collection->offsetExists('a')); + $this->collection->offsetUnset('a'); + $this->assertFalse($this->collection->offsetExists('a')); + } + + public function testKeys() { + $this->collection->a = 1; + $this->collection->b = 2; + $this->assertEquals(['a', 'b'], $this->collection->keys()); + } + + public function testClear() { + $this->collection->a = 1; + $this->collection->b = 2; + $this->assertEquals(['a', 'b'], $this->collection->keys()); + $this->collection->clear(); + $this->assertEquals(0, $this->collection->count()); + } +}