Skip to content
Snippets Groups Projects
Commit 55575b8b authored by Tahaa Karim's avatar Tahaa Karim
Browse files

aliases unit tests

parent 11f282c4
No related branches found
No related tags found
No related merge requests found
<?php
/**
* @author Tahaa Karim <tahaalibra@gmail.com>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
use OC\AppFramework\Http;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\JSONResponse;
use OCA\Mail\Controller\AliasesController;
use OCP\IUserSession;
use Test\TestCase;
class AliasesControllerTest extends TestCase {
private $controller;
private $appName = 'mail';
private $request;
private $aliasService;
private $userId = 'user12345';
private $userSession;
private $user;
private $alias;
public function setUp() {
$this->request = $this->getMockBuilder('OCP\IRequest')
->getMock();
$this->aliasService = $this->getMockBuilder('OCA\Mail\Service\AliasesService')
->disableOriginalConstructor()
->getMock();
$this->userSession = $this->getMockBuilder('OCP\IUserSession')
->getMock();
$this->user = $this->getMockBuilder('OCP\IUser')
->getMock();
$this->alias = $this->getMockBuilder('\OCA\Mail\Db\Alias')
->disableOriginalConstructor()
->getMock();
$this->userSession->expects($this->once())
->method('getUser')
->will($this->returnValue($this->user));
$this->controller = new AliasesController($this->appName, $this->request, $this->aliasService, $this->userSession);
}
public function testIndex() {
$accountId = 123;
$this->user->expects($this->once())
->method('getUID')
->will($this->returnValue($this->userId));
$this->aliasService->expects($this->once())
->method('findAll')
->with($accountId, $this->userId)
->will($this->returnValue([$this->alias]));
$response = $this->controller->index($accountId);
$expectedResponse = [
$this->alias
];
$this->assertEquals($expectedResponse, $response);
}
public function testDestroy() {
$aliasId = 123;
$this->user->expects($this->once())
->method('getUID')
->will($this->returnValue($this->userId));
$this->aliasService->expects($this->once())
->method('delete')
->with($this->equalTo($aliasId), $this->equalTo($this->userId))
->will($this->returnValue(new JSONResponse()));
$response = $this->controller->destroy($aliasId);
$expectedResponse = new JSONResponse();
$this->assertEquals($expectedResponse, $response);
}
public function testCreate() {
$accountId = 123;
$alias = "alias@marvel.com";
$aliasName = "Peter Parker";
$this->aliasService->expects($this->once())
->method('create')
->with($this->equalTo($accountId), $this->equalTo($alias), $this->equalTo($aliasName))
->will($this->returnValue([
'accountId' => $accountId,
'name' => $aliasName,
'alias' => $alias,
'id' => 123
]));
$response = $this->controller->create($accountId, $alias, $aliasName);
$expected = [
'accountId' => $accountId,
'name' => $aliasName,
'alias' => $alias,
'id' => 123
];
$this->assertEquals($expected, $response);
}
}
\ No newline at end of file
<?php
/**
* @author Tahaa Karim <tahaalibra@gmail.com>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Mail\Db;
use OC\AppFramework\Db\Db;
class AliasMapperTest extends \PHPUnit_Framework_TestCase {
/**
* @var AliasMapper
*/
private $mapper;
/**
* @var \OC\DB\Connection
*/
private $db;
/**
* @var Alias
*/
private $alias;
/**
* Initialize Mapper
*/
public function setup(){
$db = \OC::$server->getDatabaseConnection();
$this->db = new Db($db);
$this->mapper = new AliasMapper($this->db);
}
public function testFind(){
$accountMapper = new MailAccountMapper($this->db);
$account = new MailAccount();
$account->setName('Peter Parker');
$account->setInboundHost('mail.marvel.com');
$account->setInboundPort(159);
$account->setInboundUser('spiderman');
$account->setInboundPassword('xxxxxxxx');
$account->setInboundSslMode('tls');
$account->setEmail('peter.parker@marvel.com');
$account->setOutboundHost('smtp.marvel.com');
$account->setOutboundPort(458);
$account->setOutboundUser('spiderman');
$account->setOutboundPassword('xxxx');
$account->setOutboundSslMode('ssl');
$account->setUserId('user12345');
$a = $accountMapper->insert($account);
$this->alias = new Alias();
$this->alias->setAccountId($a->getId());
$this->alias->setAlias('alias@marvel.com');
$this->alias->setName('alias');
/** @var Alias $b */
$b = $this->mapper->insert($this->alias);
$result = $this->mapper->find($b->getId(), $account->getUserId());
$this->assertEquals(
[
'accountId' => $this->alias->getAccountId(),
'name' => $this->alias->getName(),
'alias' => $this->alias->getAlias(),
'id' => $this->alias->getId()
],
[
'accountId' => $result->getAccountId(),
'name' => $result->getName(),
'alias' => $result->getAlias(),
'id' => $result->getId()
]
);
}
protected function tearDown() {
parent::tearDown();
$sql = 'DELETE FROM *PREFIX*mail_aliases WHERE `id` = ?';
$stmt = $this->db->prepare($sql);
if (!empty($this->alias)) {
$stmt->execute([$this->alias->getId()]);
}
$sql = 'DELETE FROM *PREFIX*mail_accounts WHERE `user_id` = ?';
$stmt = $this->db->prepare($sql);
if (!empty($this->alias)) {
$stmt->execute(['user12345']);
}
}
}
\ No newline at end of file
<?php
/**
* @author Tahaa Karim <tahaalibra@gmail.com>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
use OCA\Mail\Db\Alias;
use Test\TestCase;
use OCA\Mail\Service\AliasesService;
class AliasesServiceTest extends TestCase {
private $service;
private $user = 'herbert';
private $mapper;
private $alias;
protected function setUp() {
parent::setUp();
$this->mapper = $this->getMockBuilder('OCA\Mail\Db\AliasMapper')
->disableOriginalConstructor()
->getMock();
$this->service = new AliasesService($this->mapper);
$this->alias = $this->getMockBuilder('OCA\Mail\Db\Alias')
->disableOriginalConstructor()
->getMock();
}
public function testFindAll() {
$accountId = 123;
$this->mapper->expects($this->once())
->method('findAll')
->with($accountId, $this->user)
->will($this->returnValue([$this->alias]));
$expected = [
$this->alias
];
$actual = $this->service->findAll($accountId, $this->user);
$this->assertEquals($expected, $actual);
}
public function testFind() {
$aliasId = 123;
$this->mapper->expects($this->once())
->method('find')
->with($aliasId, $this->user)
->will($this->returnValue($this->alias));
$expected = $this->alias;
$actual = $this->service->find($aliasId, $this->user);
$this->assertEquals($expected, $actual);
}
public function testCreate() {
$accountId = 123;
$alias = "alias@marvel.com";
$aliasName = "alias";
$aliasEntity = new Alias();
$aliasEntity->setAccountId($accountId);
$aliasEntity->setAlias($alias);
$aliasEntity->setName($aliasName);
$this->mapper->expects($this->once())
->method('insert')
->with($aliasEntity)
->will($this->returnValue($aliasEntity));
$result = $this->service->create($accountId, $alias, $aliasName);
$this->assertEquals(
[
'accountId' => $aliasEntity->getAccountId(),
'name' => $aliasEntity->getName(),
'alias' => $aliasEntity->getAlias(),
'id' => $aliasEntity->getId()
],
[
'accountId' => $result->getAccountId(),
'name' => $result->getName(),
'alias' => $result->getAlias(),
'id' => $result->getId()
]
);
}
public function testDelete() {
$aliasId = 123;
$this->mapper->expects($this->once())
->method('find')
->with($aliasId, $this->user)
->will($this->returnValue($this->alias));
$this->mapper->expects($this->once())
->method('delete')
->with($this->alias);
$this->service->delete($aliasId, $this->user);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment