add IAccountPropertyCollection with implementation
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
afea57352b
commit
0bade27479
|
@ -10,6 +10,7 @@ return array(
|
|||
'OCP\\Accounts\\IAccount' => $baseDir . '/lib/public/Accounts/IAccount.php',
|
||||
'OCP\\Accounts\\IAccountManager' => $baseDir . '/lib/public/Accounts/IAccountManager.php',
|
||||
'OCP\\Accounts\\IAccountProperty' => $baseDir . '/lib/public/Accounts/IAccountProperty.php',
|
||||
'OCP\\Accounts\\IAccountPropertyCollection' => $baseDir . '/lib/public/Accounts/IAccountPropertyCollection.php',
|
||||
'OCP\\Accounts\\PropertyDoesNotExistException' => $baseDir . '/lib/public/Accounts/PropertyDoesNotExistException.php',
|
||||
'OCP\\Activity\\ActivitySettings' => $baseDir . '/lib/public/Activity/ActivitySettings.php',
|
||||
'OCP\\Activity\\IConsumer' => $baseDir . '/lib/public/Activity/IConsumer.php',
|
||||
|
@ -581,6 +582,7 @@ return array(
|
|||
'OC\\Accounts\\Account' => $baseDir . '/lib/private/Accounts/Account.php',
|
||||
'OC\\Accounts\\AccountManager' => $baseDir . '/lib/private/Accounts/AccountManager.php',
|
||||
'OC\\Accounts\\AccountProperty' => $baseDir . '/lib/private/Accounts/AccountProperty.php',
|
||||
'OC\\Accounts\\AccountPropertyCollection' => $baseDir . '/lib/private/Accounts/AccountPropertyCollection.php',
|
||||
'OC\\Accounts\\Hooks' => $baseDir . '/lib/private/Accounts/Hooks.php',
|
||||
'OC\\Activity\\ActivitySettingsAdapter' => $baseDir . '/lib/private/Activity/ActivitySettingsAdapter.php',
|
||||
'OC\\Activity\\Event' => $baseDir . '/lib/private/Activity/Event.php',
|
||||
|
|
|
@ -39,6 +39,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
|
|||
'OCP\\Accounts\\IAccount' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccount.php',
|
||||
'OCP\\Accounts\\IAccountManager' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountManager.php',
|
||||
'OCP\\Accounts\\IAccountProperty' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountProperty.php',
|
||||
'OCP\\Accounts\\IAccountPropertyCollection' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountPropertyCollection.php',
|
||||
'OCP\\Accounts\\PropertyDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Accounts/PropertyDoesNotExistException.php',
|
||||
'OCP\\Activity\\ActivitySettings' => __DIR__ . '/../../..' . '/lib/public/Activity/ActivitySettings.php',
|
||||
'OCP\\Activity\\IConsumer' => __DIR__ . '/../../..' . '/lib/public/Activity/IConsumer.php',
|
||||
|
@ -610,6 +611,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
|
|||
'OC\\Accounts\\Account' => __DIR__ . '/../../..' . '/lib/private/Accounts/Account.php',
|
||||
'OC\\Accounts\\AccountManager' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountManager.php',
|
||||
'OC\\Accounts\\AccountProperty' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountProperty.php',
|
||||
'OC\\Accounts\\AccountPropertyCollection' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountPropertyCollection.php',
|
||||
'OC\\Accounts\\Hooks' => __DIR__ . '/../../..' . '/lib/private/Accounts/Hooks.php',
|
||||
'OC\\Activity\\ActivitySettingsAdapter' => __DIR__ . '/../../..' . '/lib/private/Activity/ActivitySettingsAdapter.php',
|
||||
'OC\\Activity\\Event' => __DIR__ . '/../../..' . '/lib/private/Activity/Event.php',
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\Accounts;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use OCP\Accounts\IAccountProperty;
|
||||
use OCP\Accounts\IAccountPropertyCollection;
|
||||
|
||||
class AccountPropertyCollection implements IAccountPropertyCollection {
|
||||
|
||||
/** @var string */
|
||||
protected $collectionName = '';
|
||||
|
||||
/** @var IAccountProperty[] */
|
||||
protected $properties = [];
|
||||
|
||||
public function __construct(string $collectionName) {
|
||||
$this->collectionName = $collectionName;
|
||||
}
|
||||
|
||||
public function setProperties(array $properties): IAccountPropertyCollection {
|
||||
/** @var IAccountProperty $property */
|
||||
$this->properties = [];
|
||||
foreach ($properties as $property) {
|
||||
$this->addProperty($property);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getProperties(): array {
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
public function addProperty(IAccountProperty $property): IAccountPropertyCollection {
|
||||
if ($property->getName() !== $this->collectionName) {
|
||||
throw new InvalidArgumentException('Provided property does not match collection name');
|
||||
}
|
||||
$this->properties[] = $property;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeProperty(IAccountProperty $property): IAccountPropertyCollection {
|
||||
$ref = array_search($property, $this->properties, true);
|
||||
if ($ref !== false) {
|
||||
unset($this->properties[$ref]);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removePropertyByValue(string $value): IAccountPropertyCollection {
|
||||
foreach ($this->properties as $i => $property) {
|
||||
if ($property->getValue() === $value) {
|
||||
unset($this->properties[$i]);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return [$this->collectionName => $this->properties];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCP\Accounts;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* Interface IAccountPropertyCollection
|
||||
*
|
||||
* @package OCP\Accounts
|
||||
*
|
||||
* @since 22.0.0
|
||||
*/
|
||||
interface IAccountPropertyCollection extends JsonSerializable {
|
||||
|
||||
/**
|
||||
* set properties of this collection
|
||||
*
|
||||
* @param IAccountProperty[] $properties
|
||||
* @throws InvalidArgumentException
|
||||
* @since 22.0.0
|
||||
*/
|
||||
public function setProperties(array $properties): IAccountPropertyCollection;
|
||||
|
||||
/**
|
||||
* @return IAccountProperty[]
|
||||
* @since 22.0.0
|
||||
*/
|
||||
public function getProperties(): array;
|
||||
|
||||
/**
|
||||
* adds a property to this collection
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @since 22.0.0
|
||||
*/
|
||||
public function addProperty(IAccountProperty $property): IAccountPropertyCollection;
|
||||
|
||||
/**
|
||||
* removes a property of this collection
|
||||
*
|
||||
* @since 22.0.0
|
||||
*/
|
||||
public function removeProperty(IAccountProperty $property): IAccountPropertyCollection;
|
||||
|
||||
/**
|
||||
* removes a property identified by its value
|
||||
*
|
||||
* @since 22.0.0
|
||||
*/
|
||||
public function removePropertyByValue(string $value): IAccountPropertyCollection;
|
||||
}
|
|
@ -0,0 +1,209 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
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 {
|
||||
/** @var IAccountPropertyCollection */
|
||||
protected $collection;
|
||||
|
||||
protected const COLLECTION_NAME = 'my_multivalue_property';
|
||||
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->collection = new AccountPropertyCollection(self::COLLECTION_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return IAccountProperty|MockObject
|
||||
*/
|
||||
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();
|
||||
|
||||
// no errors, gently
|
||||
$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();
|
||||
|
||||
// no errors, gently
|
||||
$this->assertCount(count($propsBefore), $propsAfter);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue