SyncFederationAddressbooksTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Federation\Tests;
  30. use OC\OCS\DiscoveryService;
  31. use OCA\Federation\DbHandler;
  32. use OCA\Federation\SyncFederationAddressBooks;
  33. class SyncFederationAddressbooksTest extends \Test\TestCase {
  34. /** @var array */
  35. private $callBacks = [];
  36. /** @var \PHPUnit\Framework\MockObject\MockObject | DiscoveryService */
  37. private $discoveryService;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->discoveryService = $this->getMockBuilder(DiscoveryService::class)
  41. ->disableOriginalConstructor()->getMock();
  42. $this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
  43. }
  44. public function testSync() {
  45. /** @var DbHandler | \PHPUnit\Framework\MockObject\MockObject $dbHandler */
  46. $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $dbHandler->method('getAllServer')
  50. ->willReturn([
  51. [
  52. 'url' => 'https://cloud.drop.box',
  53. 'url_hash' => 'sha1',
  54. 'shared_secret' => 'iloveowncloud',
  55. 'sync_token' => '0'
  56. ]
  57. ]);
  58. $dbHandler->expects($this->once())->method('setServerStatus')->
  59. with('https://cloud.drop.box', 1, '1');
  60. $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $syncService->expects($this->once())->method('syncRemoteAddressBook')
  64. ->willReturn('1');
  65. /** @var \OCA\DAV\CardDAV\SyncService $syncService */
  66. $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService);
  67. $s->syncThemAll(function ($url, $ex) {
  68. $this->callBacks[] = [$url, $ex];
  69. });
  70. $this->assertEquals('1', count($this->callBacks));
  71. }
  72. public function testException() {
  73. /** @var DbHandler | \PHPUnit\Framework\MockObject\MockObject $dbHandler */
  74. $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')->
  75. disableOriginalConstructor()->
  76. getMock();
  77. $dbHandler->method('getAllServer')->
  78. willReturn([
  79. [
  80. 'url' => 'https://cloud.drop.box',
  81. 'url_hash' => 'sha1',
  82. 'shared_secret' => 'iloveowncloud',
  83. 'sync_token' => '0'
  84. ]
  85. ]);
  86. $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $syncService->expects($this->once())->method('syncRemoteAddressBook')
  90. ->willThrowException(new \Exception('something did not work out'));
  91. /** @var \OCA\DAV\CardDAV\SyncService $syncService */
  92. $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService);
  93. $s->syncThemAll(function ($url, $ex) {
  94. $this->callBacks[] = [$url, $ex];
  95. });
  96. $this->assertEquals(2, count($this->callBacks));
  97. }
  98. }