RefreshWebcalJobRegistrarTest.php 4.1 KB

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