NC app base

This commit is contained in:
Szabo David
2022-11-28 11:25:02 +01:00
parent 6e609d9715
commit cbcbda164c
37 changed files with 1774 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\Nezd\Tests\Integration\Controller;
use OCP\AppFramework\App;
use OCP\IRequest;
use PHPUnit\Framework\TestCase;
use OCA\Nezd\Db\Note;
use OCA\Nezd\Db\NoteMapper;
use OCA\Nezd\Controller\NoteController;
class NoteIntegrationTest extends TestCase {
private NoteController $controller;
private QBMapper $mapper;
private string $userId = 'john';
public function setUp(): void {
$app = new App('nezd');
$container = $app->getContainer();
// only replace the user id
$container->registerService('userId', function () {
return $this->userId;
});
// we do not care about the request but the controller needs it
$container->registerService(IRequest::class, function () {
return $this->createMock(IRequest::class);
});
$this->controller = $container->get(NoteController::class);
$this->mapper = $container->get(NoteMapper::class);
}
public function testUpdate(): void {
// create a new note that should be updated
$note = new Note();
$note->setTitle('old_title');
$note->setContent('old_content');
$note->setUserId($this->userId);
$id = $this->mapper->insert($note)->getId();
// fromRow does not set the fields as updated
$updatedNote = Note::fromRow([
'id' => $id,
'user_id' => $this->userId
]);
$updatedNote->setContent('content');
$updatedNote->setTitle('title');
$result = $this->controller->update($id, 'title', 'content');
$this->assertEquals($updatedNote, $result->getData());
// clean up
$this->mapper->delete($result->getData());
}
}

View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\Nezd\Tests\Unit\Controller;
use OCA\Nezd\Controller\NoteApiController;
class NoteApiControllerTest extends NoteControllerTest {
public function setUp(): void {
parent::setUp();
$this->controller = new NoteApiController($this->request, $this->service, $this->userId);
}
}

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\Nezd\Tests\Unit\Controller;
use PHPUnit\Framework\TestCase;
use OCP\AppFramework\Http;
use OCP\IRequest;
use OCA\Nezd\Service\NoteNotFound;
use OCA\Nezd\Service\NoteService;
use OCA\Nezd\Controller\NoteController;
class NoteControllerTest extends TestCase {
protected NoteController $controller;
protected string $userId = 'john';
protected $service;
protected $request;
public function setUp(): void {
$this->request = $this->getMockBuilder(IRequest::class)->getMock();
$this->service = $this->getMockBuilder(NoteService::class)
->disableOriginalConstructor()
->getMock();
$this->controller = new NoteController($this->request, $this->service, $this->userId);
}
public function testUpdate(): void {
$note = 'just check if this value is returned correctly';
$this->service->expects($this->once())
->method('update')
->with($this->equalTo(3),
$this->equalTo('title'),
$this->equalTo('content'),
$this->equalTo($this->userId))
->will($this->returnValue($note));
$result = $this->controller->update(3, 'title', 'content');
$this->assertEquals($note, $result->getData());
}
public function testUpdateNotFound(): void {
// test the correct status code if no note is found
$this->service->expects($this->once())
->method('update')
->will($this->throwException(new NoteNotFound()));
$result = $this->controller->update(3, 'title', 'content');
$this->assertEquals(Http::STATUS_NOT_FOUND, $result->getStatus());
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\Nezd\Tests\Unit\Controller;
use PHPUnit\Framework\TestCase;
use OCP\AppFramework\Http\TemplateResponse;
class PageControllerTest extends TestCase {
private PageController $controller;
public function setUp(): void {
$request = $this->getMockBuilder(\OCP\IRequest::class)->getMock();
$this->controller = new PageController($request);
}
public function testIndex(): void {
$result = $this->controller->index();
$this->assertEquals('main', $result->getTemplateName());
$this->assertTrue($result instanceof TemplateResponse);
}
}

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\Nezd\Tests\Unit\Service;
use OCA\Nezd\Service\NoteNotFound;
use PHPUnit\Framework\TestCase;
use OCP\AppFramework\Db\DoesNotExistException;
use OCA\Nezd\Db\Note;
use OCA\Nezd\Service\NoteService;
use OCA\Nezd\Db\NoteMapper;
class NoteServiceTest extends TestCase {
private NoteService $service;
private string $userId = 'john';
private $mapper;
public function setUp(): void {
$this->mapper = $this->getMockBuilder(NoteMapper::class)
->disableOriginalConstructor()
->getMock();
$this->service = new NoteService($this->mapper);
}
public function testUpdate(): void {
// the existing note
$note = Note::fromRow([
'id' => 3,
'title' => 'yo',
'content' => 'nope'
]);
$this->mapper->expects($this->once())
->method('find')
->with($this->equalTo(3))
->will($this->returnValue($note));
// the note when updated
$updatedNote = Note::fromRow(['id' => 3]);
$updatedNote->setTitle('title');
$updatedNote->setContent('content');
$this->mapper->expects($this->once())
->method('update')
->with($this->equalTo($updatedNote))
->will($this->returnValue($updatedNote));
$result = $this->service->update(3, 'title', 'content', $this->userId);
$this->assertEquals($updatedNote, $result);
}
public function testUpdateNotFound(): void {
$this->expectException(NoteNotFound::class);
// test the correct status code if no note is found
$this->mapper->expects($this->once())
->method('find')
->with($this->equalTo(3))
->will($this->throwException(new DoesNotExistException('')));
$this->service->update(3, 'title', 'content', $this->userId);
}
}

6
tests/bootstrap.php Normal file
View File

@ -0,0 +1,6 @@
<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
require_once __DIR__ . '/../../../tests/bootstrap.php';

View File

@ -0,0 +1,11 @@
<phpunit bootstrap="bootstrap.php" colors="true">
<!--
SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
SPDX-License-Identifier: AGPL-3.0-or-later
-->
<testsuites>
<testsuite name="integration">
<directory>./Integration</directory>
</testsuite>
</testsuites>
</phpunit>

11
tests/phpunit.xml Normal file
View File

@ -0,0 +1,11 @@
<phpunit bootstrap="bootstrap.php" colors="true">
<!--
SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
SPDX-License-Identifier: AGPL-3.0-or-later
-->
<testsuites>
<testsuite name="unit">
<directory>./Unit</directory>
</testsuite>
</testsuites>
</phpunit>

2
tests/psalm-baseline.xml Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.x-dev@">

View File

@ -0,0 +1,2 @@
SPDX-FileCopyrightText: Szabó Dávid <daevidt@gmail.com>
SPDX-License-Identifier: CC0-1.0