NC app base
This commit is contained in:
15
tests/Unit/Controller/NoteApiControllerTest.php
Normal file
15
tests/Unit/Controller/NoteApiControllerTest.php
Normal 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);
|
||||
}
|
||||
}
|
57
tests/Unit/Controller/NoteControllerTest.php
Normal file
57
tests/Unit/Controller/NoteControllerTest.php
Normal 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());
|
||||
}
|
||||
}
|
27
tests/Unit/Controller/PageControllerTest.php
Normal file
27
tests/Unit/Controller/PageControllerTest.php
Normal 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);
|
||||
}
|
||||
}
|
65
tests/Unit/Service/NoteServiceTest.php
Normal file
65
tests/Unit/Service/NoteServiceTest.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user