aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Memcache
ModeNameSize
-rw-r--r--RedisCommand.php1599logstatsplain
id='n23' href='#n23'>23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

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(): void {
		$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(): void {
		$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(): void {
		$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(): void {
		$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(): void {
		$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(): void {
		$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(): void {
		$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(): void {
		$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);
	}
}