From d245f24f5bc05b58b06803cf9456341cbc305d8f Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Wed, 13 Nov 2024 17:08:26 -0500 Subject: [PATCH] Add collection tests --- tests/Unit/Models/CollectionTest.php | 116 +++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tests/Unit/Models/CollectionTest.php diff --git a/tests/Unit/Models/CollectionTest.php b/tests/Unit/Models/CollectionTest.php new file mode 100644 index 00000000..4d98d8d8 --- /dev/null +++ b/tests/Unit/Models/CollectionTest.php @@ -0,0 +1,116 @@ + 'John Doe']); + + $collection = new Collection([$user]); + + $this->assertTrue( + $collection->contains(function (Model $model) { + return $model->getFirstAttribute('cn') === 'John Doe'; + }) + ); + + $this->assertFalse( + $collection->contains(function (Model $model) { + return $model->getFirstAttribute('cn') === 'Jane Doe'; + }) + ); + } + + public function test_contains_with_key_operator_and_value() + { + $user = new User(['cn' => 'John Doe']); + + $collection = new Collection([$user]); + + $this->assertTrue( + $collection->contains('cn', '=', ['John Doe']) + ); + + $this->assertFalse( + $collection->contains('cn', '=', ['Jane Doe']) + ); + } + + public function test_contains_with_model() + { + $user = new User(['dn' => 'cn=John Doe']); + + $otherUser = new User(['dn' => 'cn=Jane Doe']); + + $collection = new Collection([$user]); + + $this->assertTrue( + $collection->contains($user) + ); + + $this->assertFalse( + $collection->contains($otherUser) + ); + } + + public function test_contains_with_model_without_dn() + { + $user = new User(['cn' => 'John Doe']); + + $collection = new Collection([$user]); + + $this->assertFalse( + $collection->contains(new User) + ); + } + + public function test_contains_with_multiple_models() + { + $user = new User(['dn' => 'cn=John Doe']); + + $otherUser = new User(['dn' => 'cn=Jane Doe']); + + $collection = new Collection([$user, $otherUser]); + + $this->assertTrue( + $collection->contains([ + $user, + $otherUser, + ]) + ); + + $this->assertFalse( + $collection->contains([ + new User(['dn' => 'cn=Foo Bar']), + new User(['dn' => 'cn=Bar Baz']), + ]) + ); + } + + public function test_contains_with_model_collection() + { + $user = new User(['dn' => 'cn=John Doe']); + + $otherUser = new User(['dn' => 'cn=Jane Doe']); + + $collection = new Collection([$user, $otherUser]); + + $this->assertTrue( + $collection->contains(new Collection([$user])) + ); + + $this->assertFalse( + $collection->contains(new Collection([ + new User(['dn' => 'cn=Foo Bar']), + new User(['dn' => 'cn=Bar Baz']), + ])) + ); + } +}