AccountPropertyCollectionTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace lib\Accounts;
  8. use InvalidArgumentException;
  9. use OC\Accounts\AccountPropertyCollection;
  10. use OCP\Accounts\IAccountProperty;
  11. use OCP\Accounts\IAccountPropertyCollection;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Test\TestCase;
  14. class AccountPropertyCollectionTest extends TestCase {
  15. /** @var IAccountPropertyCollection */
  16. protected $collection;
  17. protected const COLLECTION_NAME = 'my_multivalue_property';
  18. public function setUp(): void {
  19. parent::setUp();
  20. $this->collection = new AccountPropertyCollection(self::COLLECTION_NAME);
  21. }
  22. /**
  23. * @return IAccountProperty|MockObject
  24. */
  25. protected function makePropertyMock(string $propertyName): MockObject {
  26. $mock = $this->createMock(IAccountProperty::class);
  27. $mock->expects($this->any())
  28. ->method('getName')
  29. ->willReturn($propertyName);
  30. return $mock;
  31. }
  32. public function testSetAndGetProperties(): void {
  33. $propsBefore = $this->collection->getProperties();
  34. $this->assertIsArray($propsBefore);
  35. $this->assertEmpty($propsBefore);
  36. $props = [
  37. $this->makePropertyMock(self::COLLECTION_NAME),
  38. $this->makePropertyMock(self::COLLECTION_NAME),
  39. $this->makePropertyMock(self::COLLECTION_NAME),
  40. ];
  41. $this->collection->setProperties($props);
  42. $propsAfter = $this->collection->getProperties();
  43. $this->assertIsArray($propsAfter);
  44. $this->assertCount(count($props), $propsAfter);
  45. }
  46. public function testSetPropertiesMixedInvalid(): void {
  47. $props = [
  48. $this->makePropertyMock(self::COLLECTION_NAME),
  49. $this->makePropertyMock('sneaky_property'),
  50. $this->makePropertyMock(self::COLLECTION_NAME),
  51. ];
  52. $this->expectException(InvalidArgumentException::class);
  53. $this->collection->setProperties($props);
  54. }
  55. public function testAddProperty(): void {
  56. $props = [
  57. $this->makePropertyMock(self::COLLECTION_NAME),
  58. $this->makePropertyMock(self::COLLECTION_NAME),
  59. $this->makePropertyMock(self::COLLECTION_NAME),
  60. ];
  61. $this->collection->setProperties($props);
  62. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  63. $this->collection->addProperty($additionalProperty);
  64. $propsAfter = $this->collection->getProperties();
  65. $this->assertCount(count($props) + 1, $propsAfter);
  66. $this->assertNotFalse(array_search($additionalProperty, $propsAfter, true));
  67. }
  68. public function testAddPropertyInvalid(): void {
  69. $props = [
  70. $this->makePropertyMock(self::COLLECTION_NAME),
  71. $this->makePropertyMock(self::COLLECTION_NAME),
  72. $this->makePropertyMock(self::COLLECTION_NAME),
  73. ];
  74. $this->collection->setProperties($props);
  75. $additionalProperty = $this->makePropertyMock('sneaky_property');
  76. $exceptionThrown = false;
  77. try {
  78. $this->collection->addProperty($additionalProperty);
  79. } catch (\InvalidArgumentException $e) {
  80. $exceptionThrown = true;
  81. } finally {
  82. $propsAfter = $this->collection->getProperties();
  83. $this->assertCount(count($props), $propsAfter);
  84. $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
  85. $this->assertTrue($exceptionThrown);
  86. }
  87. }
  88. public function testRemoveProperty(): void {
  89. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  90. $props = [
  91. $this->makePropertyMock(self::COLLECTION_NAME),
  92. $this->makePropertyMock(self::COLLECTION_NAME),
  93. $additionalProperty,
  94. $this->makePropertyMock(self::COLLECTION_NAME),
  95. ];
  96. $this->collection->setProperties($props);
  97. $propsBefore = $this->collection->getProperties();
  98. $this->collection->removeProperty($additionalProperty);
  99. $propsAfter = $this->collection->getProperties();
  100. $this->assertTrue(count($propsBefore) > count($propsAfter));
  101. $this->assertCount(count($propsBefore) - 1, $propsAfter);
  102. $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
  103. }
  104. public function testRemovePropertyNotFound(): void {
  105. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  106. $props = [
  107. $this->makePropertyMock(self::COLLECTION_NAME),
  108. $this->makePropertyMock(self::COLLECTION_NAME),
  109. $this->makePropertyMock(self::COLLECTION_NAME),
  110. ];
  111. $this->collection->setProperties($props);
  112. $propsBefore = $this->collection->getProperties();
  113. $this->collection->removeProperty($additionalProperty);
  114. $propsAfter = $this->collection->getProperties();
  115. // no errors, gently
  116. $this->assertCount(count($propsBefore), $propsAfter);
  117. }
  118. public function testRemovePropertyByValue(): void {
  119. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  120. $additionalProperty->expects($this->any())
  121. ->method('getValue')
  122. ->willReturn('Lorem ipsum');
  123. $additionalPropertyTwo = clone $additionalProperty;
  124. $props = [
  125. $this->makePropertyMock(self::COLLECTION_NAME),
  126. $this->makePropertyMock(self::COLLECTION_NAME),
  127. $additionalProperty,
  128. $this->makePropertyMock(self::COLLECTION_NAME),
  129. $additionalPropertyTwo
  130. ];
  131. $this->collection->setProperties($props);
  132. $propsBefore = $this->collection->getProperties();
  133. $this->collection->removePropertyByValue('Lorem ipsum');
  134. $propsAfter = $this->collection->getProperties();
  135. $this->assertTrue(count($propsBefore) > count($propsAfter));
  136. $this->assertCount(count($propsBefore) - 2, $propsAfter);
  137. $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
  138. $this->assertFalse(array_search($additionalPropertyTwo, $propsAfter, true));
  139. }
  140. public function testRemovePropertyByValueNotFound(): void {
  141. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  142. $additionalProperty->expects($this->any())
  143. ->method('getValue')
  144. ->willReturn('Lorem ipsum');
  145. $props = [
  146. $this->makePropertyMock(self::COLLECTION_NAME),
  147. $this->makePropertyMock(self::COLLECTION_NAME),
  148. $this->makePropertyMock(self::COLLECTION_NAME),
  149. ];
  150. $this->collection->setProperties($props);
  151. $propsBefore = $this->collection->getProperties();
  152. $this->collection->removePropertyByValue('Lorem ipsum');
  153. $propsAfter = $this->collection->getProperties();
  154. // no errors, gently
  155. $this->assertCount(count($propsBefore), $propsAfter);
  156. }
  157. }