RefreshWebcalJobRegistrarTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, Georg Ehrke
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\DAV\Migration;
  28. use OCA\DAV\BackgroundJob\RefreshWebcalJob;
  29. use OCA\DAV\Migration\RefreshWebcalJobRegistrar;
  30. use OCP\BackgroundJob\IJobList;
  31. use OCP\DB\IResult;
  32. use OCP\DB\QueryBuilder\IQueryBuilder;
  33. use OCP\IDBConnection;
  34. use OCP\Migration\IOutput;
  35. use Test\TestCase;
  36. class RefreshWebcalJobRegistrarTest extends TestCase {
  37. /** @var IDBConnection | \PHPUnit\Framework\MockObject\MockObject */
  38. private $db;
  39. /** @var IJobList | \PHPUnit\Framework\MockObject\MockObject */
  40. private $jobList;
  41. /** @var RefreshWebcalJobRegistrar */
  42. private $migration;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->db = $this->createMock(IDBConnection::class);
  46. $this->jobList = $this->createMock(IJobList::class);
  47. $this->migration = new RefreshWebcalJobRegistrar($this->db, $this->jobList);
  48. }
  49. public function testGetName(): void {
  50. $this->assertEquals($this->migration->getName(), 'Registering background jobs to update cache for webcal calendars');
  51. }
  52. public function testRun(): void {
  53. $output = $this->createMock(IOutput::class);
  54. $queryBuilder = $this->createMock(IQueryBuilder::class);
  55. $statement = $this->createMock(IResult::class);
  56. $this->db->expects($this->once())
  57. ->method('getQueryBuilder')
  58. ->willReturn($queryBuilder);
  59. $queryBuilder->expects($this->once())
  60. ->method('select')
  61. ->with(['principaluri', 'uri'])
  62. ->willReturn($queryBuilder);
  63. $queryBuilder->expects($this->once())
  64. ->method('from')
  65. ->with('calendarsubscriptions')
  66. ->willReturn($queryBuilder);
  67. $queryBuilder->expects($this->once())
  68. ->method('execute')
  69. ->willReturn($statement);
  70. $statement->expects($this->exactly(4))
  71. ->method('fetch')
  72. ->with(\PDO::FETCH_ASSOC)
  73. ->willReturnOnConsecutiveCalls(
  74. [
  75. 'principaluri' => 'foo1',
  76. 'uri' => 'bar1',
  77. ],
  78. [
  79. 'principaluri' => 'foo2',
  80. 'uri' => 'bar2',
  81. ],
  82. [
  83. 'principaluri' => 'foo3',
  84. 'uri' => 'bar3',
  85. ],
  86. null
  87. );
  88. $this->jobList->expects($this->exactly(3))
  89. ->method('has')
  90. ->withConsecutive(
  91. [RefreshWebcalJob::class, [
  92. 'principaluri' => 'foo1',
  93. 'uri' => 'bar1',
  94. ]],
  95. [RefreshWebcalJob::class, [
  96. 'principaluri' => 'foo2',
  97. 'uri' => 'bar2',
  98. ]],
  99. [RefreshWebcalJob::class, [
  100. 'principaluri' => 'foo3',
  101. 'uri' => 'bar3',
  102. ]])
  103. ->willReturnOnConsecutiveCalls(
  104. false,
  105. true,
  106. false,
  107. );
  108. $this->jobList->expects($this->exactly(2))
  109. ->method('add')
  110. ->withConsecutive(
  111. [RefreshWebcalJob::class, [
  112. 'principaluri' => 'foo1',
  113. 'uri' => 'bar1',
  114. ]],
  115. [RefreshWebcalJob::class, [
  116. 'principaluri' => 'foo3',
  117. 'uri' => 'bar3',
  118. ]]);
  119. $output->expects($this->once())
  120. ->method('info')
  121. ->with('Added 2 background jobs to update webcal calendars');
  122. $this->migration->run($output);
  123. }
  124. }