AccountPropertyCollectionTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace lib\Accounts;
  25. use InvalidArgumentException;
  26. use OC\Accounts\AccountPropertyCollection;
  27. use OCP\Accounts\IAccountProperty;
  28. use OCP\Accounts\IAccountPropertyCollection;
  29. use PHPUnit\Framework\MockObject\MockObject;
  30. use Test\TestCase;
  31. class AccountPropertyCollectionTest extends TestCase {
  32. /** @var IAccountPropertyCollection */
  33. protected $collection;
  34. protected const COLLECTION_NAME = 'my_multivalue_property';
  35. public function setUp(): void {
  36. parent::setUp();
  37. $this->collection = new AccountPropertyCollection(self::COLLECTION_NAME);
  38. }
  39. /**
  40. * @return IAccountProperty|MockObject
  41. */
  42. protected function makePropertyMock(string $propertyName): MockObject {
  43. $mock = $this->createMock(IAccountProperty::class);
  44. $mock->expects($this->any())
  45. ->method('getName')
  46. ->willReturn($propertyName);
  47. return $mock;
  48. }
  49. public function testSetAndGetProperties() {
  50. $propsBefore = $this->collection->getProperties();
  51. $this->assertIsArray($propsBefore);
  52. $this->assertEmpty($propsBefore);
  53. $props = [
  54. $this->makePropertyMock(self::COLLECTION_NAME),
  55. $this->makePropertyMock(self::COLLECTION_NAME),
  56. $this->makePropertyMock(self::COLLECTION_NAME),
  57. ];
  58. $this->collection->setProperties($props);
  59. $propsAfter = $this->collection->getProperties();
  60. $this->assertIsArray($propsAfter);
  61. $this->assertCount(count($props), $propsAfter);
  62. }
  63. public function testSetPropertiesMixedInvalid() {
  64. $props = [
  65. $this->makePropertyMock(self::COLLECTION_NAME),
  66. $this->makePropertyMock('sneaky_property'),
  67. $this->makePropertyMock(self::COLLECTION_NAME),
  68. ];
  69. $this->expectException(InvalidArgumentException::class);
  70. $this->collection->setProperties($props);
  71. }
  72. public function testAddProperty() {
  73. $props = [
  74. $this->makePropertyMock(self::COLLECTION_NAME),
  75. $this->makePropertyMock(self::COLLECTION_NAME),
  76. $this->makePropertyMock(self::COLLECTION_NAME),
  77. ];
  78. $this->collection->setProperties($props);
  79. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  80. $this->collection->addProperty($additionalProperty);
  81. $propsAfter = $this->collection->getProperties();
  82. $this->assertCount(count($props) + 1, $propsAfter);
  83. $this->assertNotFalse(array_search($additionalProperty, $propsAfter, true));
  84. }
  85. public function testAddPropertyInvalid() {
  86. $props = [
  87. $this->makePropertyMock(self::COLLECTION_NAME),
  88. $this->makePropertyMock(self::COLLECTION_NAME),
  89. $this->makePropertyMock(self::COLLECTION_NAME),
  90. ];
  91. $this->collection->setProperties($props);
  92. $additionalProperty = $this->makePropertyMock('sneaky_property');
  93. $exceptionThrown = false;
  94. try {
  95. $this->collection->addProperty($additionalProperty);
  96. } catch (\InvalidArgumentException $e) {
  97. $exceptionThrown = true;
  98. } finally {
  99. $propsAfter = $this->collection->getProperties();
  100. $this->assertCount(count($props), $propsAfter);
  101. $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
  102. $this->assertTrue($exceptionThrown);
  103. }
  104. }
  105. public function testRemoveProperty() {
  106. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  107. $props = [
  108. $this->makePropertyMock(self::COLLECTION_NAME),
  109. $this->makePropertyMock(self::COLLECTION_NAME),
  110. $additionalProperty,
  111. $this->makePropertyMock(self::COLLECTION_NAME),
  112. ];
  113. $this->collection->setProperties($props);
  114. $propsBefore = $this->collection->getProperties();
  115. $this->collection->removeProperty($additionalProperty);
  116. $propsAfter = $this->collection->getProperties();
  117. $this->assertTrue(count($propsBefore) > count($propsAfter));
  118. $this->assertCount(count($propsBefore) - 1, $propsAfter);
  119. $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
  120. }
  121. public function testRemovePropertyNotFound() {
  122. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  123. $props = [
  124. $this->makePropertyMock(self::COLLECTION_NAME),
  125. $this->makePropertyMock(self::COLLECTION_NAME),
  126. $this->makePropertyMock(self::COLLECTION_NAME),
  127. ];
  128. $this->collection->setProperties($props);
  129. $propsBefore = $this->collection->getProperties();
  130. $this->collection->removeProperty($additionalProperty);
  131. $propsAfter = $this->collection->getProperties();
  132. // no errors, gently
  133. $this->assertCount(count($propsBefore), $propsAfter);
  134. }
  135. public function testRemovePropertyByValue() {
  136. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  137. $additionalProperty->expects($this->any())
  138. ->method('getValue')
  139. ->willReturn('Lorem ipsum');
  140. $additionalPropertyTwo = clone $additionalProperty;
  141. $props = [
  142. $this->makePropertyMock(self::COLLECTION_NAME),
  143. $this->makePropertyMock(self::COLLECTION_NAME),
  144. $additionalProperty,
  145. $this->makePropertyMock(self::COLLECTION_NAME),
  146. $additionalPropertyTwo
  147. ];
  148. $this->collection->setProperties($props);
  149. $propsBefore = $this->collection->getProperties();
  150. $this->collection->removePropertyByValue('Lorem ipsum');
  151. $propsAfter = $this->collection->getProperties();
  152. $this->assertTrue(count($propsBefore) > count($propsAfter));
  153. $this->assertCount(count($propsBefore) - 2, $propsAfter);
  154. $this->assertFalse(array_search($additionalProperty, $propsAfter, true));
  155. $this->assertFalse(array_search($additionalPropertyTwo, $propsAfter, true));
  156. }
  157. public function testRemovePropertyByValueNotFound() {
  158. $additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
  159. $additionalProperty->expects($this->any())
  160. ->method('getValue')
  161. ->willReturn('Lorem ipsum');
  162. $props = [
  163. $this->makePropertyMock(self::COLLECTION_NAME),
  164. $this->makePropertyMock(self::COLLECTION_NAME),
  165. $this->makePropertyMock(self::COLLECTION_NAME),
  166. ];
  167. $this->collection->setProperties($props);
  168. $propsBefore = $this->collection->getProperties();
  169. $this->collection->removePropertyByValue('Lorem ipsum');
  170. $propsAfter = $this->collection->getProperties();
  171. // no errors, gently
  172. $this->assertCount(count($propsBefore), $propsAfter);
  173. }
  174. }