1
0

RefreshWebcalJobRegistrarTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\DAV\Migration;
  7. use OCA\DAV\BackgroundJob\RefreshWebcalJob;
  8. use OCA\DAV\Migration\RefreshWebcalJobRegistrar;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\DB\IResult;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\IDBConnection;
  13. use OCP\Migration\IOutput;
  14. use Test\TestCase;
  15. class RefreshWebcalJobRegistrarTest extends TestCase {
  16. /** @var IDBConnection | \PHPUnit\Framework\MockObject\MockObject */
  17. private $db;
  18. /** @var IJobList | \PHPUnit\Framework\MockObject\MockObject */
  19. private $jobList;
  20. /** @var RefreshWebcalJobRegistrar */
  21. private $migration;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->db = $this->createMock(IDBConnection::class);
  25. $this->jobList = $this->createMock(IJobList::class);
  26. $this->migration = new RefreshWebcalJobRegistrar($this->db, $this->jobList);
  27. }
  28. public function testGetName(): void {
  29. $this->assertEquals($this->migration->getName(), 'Registering background jobs to update cache for webcal calendars');
  30. }
  31. public function testRun(): void {
  32. $output = $this->createMock(IOutput::class);
  33. $queryBuilder = $this->createMock(IQueryBuilder::class);
  34. $statement = $this->createMock(IResult::class);
  35. $this->db->expects($this->once())
  36. ->method('getQueryBuilder')
  37. ->willReturn($queryBuilder);
  38. $queryBuilder->expects($this->once())
  39. ->method('select')
  40. ->with(['principaluri', 'uri'])
  41. ->willReturn($queryBuilder);
  42. $queryBuilder->expects($this->once())
  43. ->method('from')
  44. ->with('calendarsubscriptions')
  45. ->willReturn($queryBuilder);
  46. $queryBuilder->expects($this->once())
  47. ->method('execute')
  48. ->willReturn($statement);
  49. $statement->expects($this->exactly(4))
  50. ->method('fetch')
  51. ->with(\PDO::FETCH_ASSOC)
  52. ->willReturnOnConsecutiveCalls(
  53. [
  54. 'principaluri' => 'foo1',
  55. 'uri' => 'bar1',
  56. ],
  57. [
  58. 'principaluri' => 'foo2',
  59. 'uri' => 'bar2',
  60. ],
  61. [
  62. 'principaluri' => 'foo3',
  63. 'uri' => 'bar3',
  64. ],
  65. null
  66. );
  67. $this->jobList->expects($this->exactly(3))
  68. ->method('has')
  69. ->withConsecutive(
  70. [RefreshWebcalJob::class, [
  71. 'principaluri' => 'foo1',
  72. 'uri' => 'bar1',
  73. ]],
  74. [RefreshWebcalJob::class, [
  75. 'principaluri' => 'foo2',
  76. 'uri' => 'bar2',
  77. ]],
  78. [RefreshWebcalJob::class, [
  79. 'principaluri' => 'foo3',
  80. 'uri' => 'bar3',
  81. ]])
  82. ->willReturnOnConsecutiveCalls(
  83. false,
  84. true,
  85. false,
  86. );
  87. $this->jobList->expects($this->exactly(2))
  88. ->method('add')
  89. ->withConsecutive(
  90. [RefreshWebcalJob::class, [
  91. 'principaluri' => 'foo1',
  92. 'uri' => 'bar1',
  93. ]],
  94. [RefreshWebcalJob::class, [
  95. 'principaluri' => 'foo3',
  96. 'uri' => 'bar3',
  97. ]]);
  98. $output->expects($this->once())
  99. ->method('info')
  100. ->with('Added 2 background jobs to update webcal calendars');
  101. $this->migration->run($output);
  102. }
  103. }