diff --git a/lib/Service/SocialApiService.php b/lib/Service/SocialApiService.php index a4db7c593..8e948c9ab 100644 --- a/lib/Service/SocialApiService.php +++ b/lib/Service/SocialApiService.php @@ -11,10 +11,7 @@ use OCA\Contacts\AppInfo\Application; use OCA\Contacts\Service\Social\CompositeSocialProvider; - -use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\ContactsManager; -use OCA\DAV\Db\PropertyMapper; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Utility\ITimeFactory; @@ -24,21 +21,21 @@ use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use Psr\Container\ContainerInterface; class SocialApiService { private $appName; public function __construct( private CompositeSocialProvider $socialProvider, + private ContainerInterface $serverContainer, private IManager $manager, private IConfig $config, private IClientService $clientService, private IL10N $l10n, private IURLGenerator $urlGen, - private CardDavBackend $davBackend, private ITimeFactory $timeFactory, private ImageResizer $imageResizer, - private PropertyMapper $propertyMapper, ) { $this->appName = Application::APP_ID; } @@ -116,7 +113,7 @@ protected function getAddressBook(string $addressbookId, ?IManager $manager = nu * @param {IManager} the contact manager to load */ protected function registerAddressbooks($userId, IManager $manager) { - $coma = new ContactsManager($this->davBackend, $this->l10n, $this->propertyMapper); + $coma = $this->serverContainer->get(ContactsManager::class); $coma->setupContactsProvider($manager, $userId, $this->urlGen); $this->manager = $manager; } @@ -228,9 +225,8 @@ public function updateContact(string $addressbookId, string $contactId, ?string */ public function existsAddressBook(string $searchBookId, string $userId): bool { $manager = $this->manager; - $coma = new ContactsManager($this->davBackend, $this->l10n, $this->propertyMapper); + $coma = $this->serverContainer->get(ContactsManager::class); $coma->setupContactsProvider($manager, $userId, $this->urlGen); - $addressBooks = $manager->getUserAddressBooks(); return $this->getAddressBook($searchBookId, $manager) !== null; } @@ -246,7 +242,7 @@ public function existsAddressBook(string $searchBookId, string $userId): bool { public function existsContact(string $searchContactId, string $searchBookId, string $userId): bool { // load address books for the user $manager = $this->manager; - $coma = new ContactsManager($this->davBackend, $this->l10n, $this->propertyMapper); + $coma = $this->serverContainer->get(ContactsManager::class); $coma->setupContactsProvider($manager, $userId, $this->urlGen); $addressBook = $this->getAddressBook($searchBookId, $manager); if ($addressBook == null) { diff --git a/tests/unit/Service/SocialApiServiceTest.php b/tests/unit/Service/SocialApiServiceTest.php index 7873ac0e6..57b2fb3f3 100644 --- a/tests/unit/Service/SocialApiServiceTest.php +++ b/tests/unit/Service/SocialApiServiceTest.php @@ -8,11 +8,11 @@ namespace OCA\Contacts\Service; use ChristophWurst\Nextcloud\Testing\TestCase; -use OCA\Contacts\Service\Social\CompositeSocialProvider; +use OCA\Contacts\Service\Social\CompositeSocialProvider; use OCA\Contacts\Service\Social\ISocialProvider; -use OCA\DAV\CardDAV\CardDavBackend; -use OCA\DAV\Db\PropertyMapper; +use OCA\DAV\CardDAV\ContactsManager; + use OCP\AppFramework\Http; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Contacts\IManager; @@ -23,9 +23,10 @@ use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; - use OCP\Util; + use PHPUnit\Framework\MockObject\MockObject; +use Psr\Container\ContainerInterface; class SocialApiServiceTest extends TestCase { private SocialApiService $service; @@ -42,14 +43,12 @@ class SocialApiServiceTest extends TestCase { private $l10n; /** @var IURLGenerator&MockObject */ private $urlGen; - /** @var CardDavBackend&MockObject */ - private $davBackend; /** @var ITimeFactory&MockObject */ private $timeFactory; /** @var ImageResizer&MockObject */ private $imageResizer; - /** @var PropertyMapper&MockObject */ - private $propertyMapper; + /** @var ContainerInterface&MockObject */ + private $container; public function allSocialProfileProviders(): array { $body = 'the body'; @@ -113,21 +112,22 @@ protected function setUp(): void { $this->clientService = $this->createMock(IClientService::class); $this->l10n = $this->createMock(IL10N::class); $this->urlGen = $this->createMock(IURLGenerator::class); - $this->davBackend = $this->createMock(CardDavBackend::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->imageResizer = $this->createMock(ImageResizer::class); - $this->propertyMapper = $this->createMock(PropertyMapper::class); + $this->container = $this->createMock(ContainerInterface::class); + $this->container + ->method('get') + ->willReturn($this->createMock(ContactsManager::class)); $this->service = new SocialApiService( $this->socialProvider, + $this->container, $this->manager, $this->config, $this->clientService, $this->l10n, $this->urlGen, - $this->davBackend, $this->timeFactory, $this->imageResizer, - $this->propertyMapper, ); }