123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- declare(strict_types=1);
- namespace lib\Accounts;
- use InvalidArgumentException;
- use OC\Accounts\AccountPropertyCollection;
- use OCP\Accounts\IAccountProperty;
- use OCP\Accounts\IAccountPropertyCollection;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class AccountPropertyCollectionTest extends TestCase {
-
- protected $collection;
- protected const COLLECTION_NAME = 'my_multivalue_property';
- public function setUp(): void {
- parent::setUp();
- $this->collection = new AccountPropertyCollection(self::COLLECTION_NAME);
- }
-
- protected function makePropertyMock(string $propertyName): MockObject {
- $mock = $this->createMock(IAccountProperty::class);
- $mock->expects($this->any())
- ->method('getName')
- ->willReturn($propertyName);
- return $mock;
- }
- public function testSetAndGetProperties() {
- $propsBefore = $this->collection->getProperties();
- $this->assertIsArray($propsBefore);
- $this->assertEmpty($propsBefore);
- $props = [
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- ];
- $this->collection->setProperties($props);
- $propsAfter = $this->collection->getProperties();
- $this->assertIsArray($propsAfter);
- $this->assertCount(count($props), $propsAfter);
- }
- public function testSetPropertiesMixedInvalid() {
- $props = [
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock('sneaky_property'),
- $this->makePropertyMock(self::COLLECTION_NAME),
- ];
- $this->expectException(InvalidArgumentException::class);
- $this->collection->setProperties($props);
- }
- public function testAddProperty() {
- $props = [
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- ];
- $this->collection->setProperties($props);
- $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
- $this->collection->addProperty($additionalProperty);
- $propsAfter = $this->collection->getProperties();
- $this->assertCount(count($props) + 1, $propsAfter);
- $this->assertNotFalse(array_search($additionalProperty, $propsAfter, true));
- }
- public function testAddPropertyInvalid() {
- $props = [
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- ];
- $this->collection->setProperties($props);
- $additionalProperty = $this->makePropertyMock('sneaky_property');
- $exceptionThrown = false;
- try {
- $this->collection->addProperty($additionalProperty);
- } catch (\InvalidArgumentException $e) {
- $exceptionThrown = true;
- } finally {
- $propsAfter = $this->collection->getProperties();
- $this->assertCount(count($props), $propsAfter);
- $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
- $this->assertTrue($exceptionThrown);
- }
- }
- public function testRemoveProperty() {
- $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
- $props = [
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- $additionalProperty,
- $this->makePropertyMock(self::COLLECTION_NAME),
- ];
- $this->collection->setProperties($props);
- $propsBefore = $this->collection->getProperties();
- $this->collection->removeProperty($additionalProperty);
- $propsAfter = $this->collection->getProperties();
- $this->assertTrue(count($propsBefore) > count($propsAfter));
- $this->assertCount(count($propsBefore) - 1, $propsAfter);
- $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
- }
- public function testRemovePropertyNotFound() {
- $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
- $props = [
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- ];
- $this->collection->setProperties($props);
- $propsBefore = $this->collection->getProperties();
- $this->collection->removeProperty($additionalProperty);
- $propsAfter = $this->collection->getProperties();
-
- $this->assertCount(count($propsBefore), $propsAfter);
- }
- public function testRemovePropertyByValue() {
- $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
- $additionalProperty->expects($this->any())
- ->method('getValue')
- ->willReturn('Lorem ipsum');
- $additionalPropertyTwo = clone $additionalProperty;
- $props = [
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- $additionalProperty,
- $this->makePropertyMock(self::COLLECTION_NAME),
- $additionalPropertyTwo
- ];
- $this->collection->setProperties($props);
- $propsBefore = $this->collection->getProperties();
- $this->collection->removePropertyByValue('Lorem ipsum');
- $propsAfter = $this->collection->getProperties();
- $this->assertTrue(count($propsBefore) > count($propsAfter));
- $this->assertCount(count($propsBefore) - 2, $propsAfter);
- $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
- $this->assertFalse(array_search($additionalPropertyTwo, $propsAfter, true));
- }
- public function testRemovePropertyByValueNotFound() {
- $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
- $additionalProperty->expects($this->any())
- ->method('getValue')
- ->willReturn('Lorem ipsum');
- $props = [
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- $this->makePropertyMock(self::COLLECTION_NAME),
- ];
- $this->collection->setProperties($props);
- $propsBefore = $this->collection->getProperties();
- $this->collection->removePropertyByValue('Lorem ipsum');
- $propsAfter = $this->collection->getProperties();
-
- $this->assertCount(count($propsBefore), $propsAfter);
- }
- }
|