Skip to content

Commit

Permalink
fix: Add unit test for analytics datasource
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jun 8, 2024
1 parent b181151 commit 41fa849
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Marcel Scherello <[email protected]>
*/

namespace OCA\Forms\Datasource;
namespace OCA\Forms\Analytics;

/** @psalm-suppress UndefinedClass */
use OCA\Analytics\Datasource\IDatasource;
Expand All @@ -20,21 +20,15 @@
use Psr\Log\LoggerInterface;

class AnalyticsDatasource implements IDatasource {
private LoggerInterface $logger;
private IL10N $l10n;
protected ?string $userId;

public function __construct(
IL10N $l10n,
LoggerInterface $logger,
?string $userId,
private FormMapper $formMapper,
private FormsService $formsService,
private SubmissionService $submissionService
protected ?string $userId,
private IL10N $l10n,
private LoggerInterface $logger,
private FormMapper $formMapper,
private FormsService $formsService,
private SubmissionService $submissionService,
) {
$this->l10n = $l10n;
$this->logger = $logger;
$this->userId = $userId;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/AnalyticsDatasourceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace OCA\Forms\Listener;

use OCA\Analytics\Datasource\DatasourceEvent;
use OCA\Forms\Datasource\AnalyticsDatasource;
use OCA\Forms\Analytics\AnalyticsDatasource;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

Expand Down
77 changes: 77 additions & 0 deletions tests/Unit/Analytics/AnalyticsDatasourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 Ferdinand Thiessen <[email protected]>
*
* @author Ferdinand Thiessen <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Forms\Tests\Unit\Analytics;

use OCA\Forms\Analytics\AnalyticsDatasource;
use OCA\Forms\Db\FormMapper;
use OCA\Forms\Service\FormsService;
use OCA\Forms\Service\SubmissionService;
use OCP\IL10N;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class AnalyticsDatasourceTest extends TestCase {

private AnalyticsDatasource $dataSource;

private IL10N|MockObject $l10n;
private LoggerInterface|MockObject $logger;
private FormMapper|MockObject $formMapper;
private FormsService|MockObject $formsService;
private SubmissionService|MockObject $submissionService;

public function setUp(): void {
parent::setUp();
$this->l10n = $this->createMock(IL10N::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->formMapper = $this->createMock(FormMapper::class);
$this->formsService = $this->createMock(FormsService::class);
$this->submissionService = $this->createMock(SubmissionService::class);

$this->dataSource = new AnalyticsDatasource(
null,
$this->l10n,
$this->logger,
$this->formMapper,
$this->formsService,
$this->submissionService,
);
}

public function testGetName() {
$this->l10n
->expects($this->any())
->method('t')
->willReturnCallback(fn (string $str) => $str);
$this->assertEquals('Nextcloud Forms', $this->dataSource->getName());
}

public function testGetId() {
$this->assertEquals(66, $this->dataSource->getId());
}

// TODO: Write tests for `getTemplate` and `readData`
}

0 comments on commit 41fa849

Please sign in to comment.