Skip to content
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.

Commit

Permalink
update/add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophWurst committed Feb 8, 2016
1 parent e59e365 commit 5c86661
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 39 deletions.
2 changes: 1 addition & 1 deletion lib/controller/autocompletecontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct($appName, IRequest $request,
* @return array
*/
public function index($term) {
return $this->service->findMathes($term);
return $this->service->findMatches($term);
}

}
2 changes: 1 addition & 1 deletion lib/service/autocompletion/autocompleteservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(ContactsIntegration $ci, AddressCollector $ac) {
$this->addressCollector = $ac;
}

public function findMathes($term) {
public function findMatches($term) {
$recipientsFromContacts = $this->contactsIntegration->getMatchingRecipient($term);
$fromCollector = $this->addressCollector->searchAddress($term);

Expand Down
51 changes: 14 additions & 37 deletions tests/controller/accountscontrollertest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class AccountsControllerTest extends \Test\TestCase {
private $accountService;
private $userId;
private $userFolder;
private $contactsIntegration;
private $autoConfig;
private $logger;
private $l10n;
Expand All @@ -56,9 +55,6 @@ protected function setUp() {
$this->userFolder = $this->getMockBuilder('\OCP\Files\Folder')
->disableOriginalConstructor()
->getMock();
$this->contactsIntegration = $this->getMockBuilder('OCA\Mail\Service\ContactsIntegration')
->disableOriginalConstructor()
->getMock();
$this->autoConfig = $this->getMockBuilder('\OCA\Mail\Service\AutoConfig\AutoConfig')
->disableOriginalConstructor()
->getMock();
Expand All @@ -73,9 +69,8 @@ protected function setUp() {
->getMock();

$this->controller = new AccountsController($this->appName, $this->request,
$this->accountService, $this->userId, $this->userFolder,
$this->contactsIntegration, $this->autoConfig, $this->logger, $this->l10n,
$this->crypto);
$this->accountService, $this->userId, $this->userFolder, $this->autoConfig,
$this->logger, $this->l10n, $this->crypto);

$this->account = $this->getMockBuilder('\OCA\Mail\Account')
->disableOriginalConstructor()
Expand Down Expand Up @@ -165,24 +160,21 @@ public function testCreateAutoDetectSuccess() {
->will($this->returnValue(135));
$this->autoConfig->expects($this->once())
->method('createAutoDetected')
->with($this->equalTo($email),
$this->equalTo($password),
->with($this->equalTo($email), $this->equalTo($password),
$this->equalTo($accountName))
->will($this->returnValue($this->account));
$this->accountService->expects($this->once())
->method('save')
->with($this->equalTo($this->account));

$response = $this->controller->create($accountName, $email, $password,
null, null, null, null, null,
null, null, null, null, null,
true);
$response = $this->controller->create($accountName, $email, $password, null,
null, null, null, null, null, null, null, null, null, true);

$expectedResponse = new JSONResponse([
'data' => [
'id' => 135,
],
], Http::STATUS_CREATED);
'data' => [
'id' => 135,
],
], Http::STATUS_CREATED);
$this->assertEquals($expectedResponse, $response);
}

Expand All @@ -193,22 +185,19 @@ public function testCreateAutoDetectFailure() {

$this->autoConfig->expects($this->once())
->method('createAutoDetected')
->with($this->equalTo($email),
$this->equalTo($password),
->with($this->equalTo($email), $this->equalTo($password),
$this->equalTo($accountName))
->will($this->returnValue(null));
$this->l10n->expects($this->once())
->method('t')
->will($this->returnValue('fail'));

$response = $this->controller->create($accountName, $email, $password,
null, null, null, null, null,
null, null, null, null, null,
true);
$response = $this->controller->create($accountName, $email, $password, null,
null, null, null, null, null, null, null, null, null, true);

$expectedResponse = new JSONResponse([
'message' => 'fail',
], Http::STATUS_BAD_REQUEST);
'message' => 'fail',
], Http::STATUS_BAD_REQUEST);
$this->assertEquals($expectedResponse, $response);
}

Expand Down Expand Up @@ -403,16 +392,4 @@ public function testDraft($isUnifiedInbox, $withPreviousDraft) {
$this->assertEquals($expected, $actual);
}

public function testAutoComplete() {
$this->contactsIntegration->expects($this->once())
->method('getMatchingRecipient')
->with($this->equalTo('search term'))
->will($this->returnValue('test'));

$response = $this->controller->autoComplete('search term');

$expectedResponse = 'test';
$this->assertEquals($expectedResponse, $response);
}

}
56 changes: 56 additions & 0 deletions tests/controller/autoconfigcontrollertest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
*
* ownCloud - Mail app
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
use PHPUnit_Framework_TestCase;
use OCA\Mail\Controller\AutoCompleteController;

class AutoConfigControllerTest extends PHPUnit_Framework_TestCase {

private $request;
private $service;
private $controller;

protected function setUp() {
parent::setUp();

$this->request = $this->getMock('OCP\IRequest');
$this->service = $this->getMockBuilder('OCA\Mail\Service\AutoCompletion\AutoCompleteService')
->disableOriginalConstructor()
->getMock();
$this->controller = new AutoCompleteController('mail', $this->request,
$this->service);
}

public function testAutoComplete() {
$term = 'john d';
$result = 'johne doe';

$this->service->expects($this->once())
->method('findMatches')
->with($this->equalTo($term))
->will($this->returnValue($result));

$response = $this->controller->index($term);

$this->assertEquals($result, $response);
}

}
105 changes: 105 additions & 0 deletions tests/service/autocompletion/addresscollectortest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/**
* ownCloud - Mail
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @copyright Christoph Wurst 2016
*/

namespace OCA\Mail\Tests\Service\Autocompletion;

use PHPUnit_Framework_TestCase;
use OCA\Mail\Db\CollectedAddress;
use OCA\Mail\Service\AutoCompletion\AddressCollector;

class AddressCollectorTest extends PHPUnit_Framework_TestCase {

private $mapper;
private $userId = 'testuser';
private $logger;
private $collector;

protected function setUp() {
parent::setUp();

$this->mapper = $this->getMockBuilder('\OCA\Mail\Db\CollectedAddressMapper')
->disableOriginalConstructor()
->getMock();
$this->logger = $this->getMockBuilder('\OCA\Mail\Service\Logger')
->disableOriginalConstructor()
->getMock();

$this->collector = new AddressCollector($this->mapper, $this->userId,
$this->logger);
}

public function testAddAddresses() {
$addresses = [
'user@example.com',
'example@user.com',
];
$data = array_map(function($address) {
$ca = new CollectedAddress();
$ca->setEmail($address);
$ca->setUserId($this->userId);
return $ca;
}, $addresses);

$this->mapper->expects($this->at(0))
->method('exists')
->with($this->userId, $addresses[0])
->will($this->returnValue(false));
$this->mapper->expects($this->at(1))
->method('insert')
->with($data[0]);
$this->mapper->expects($this->at(2))
->method('exists')
->with($this->userId, $addresses[1])
->will($this->returnValue(false));
$this->mapper->expects($this->at(3))
->method('insert')
->with($data[1]);

$this->collector->addAddresses($addresses);
}

public function testAddDuplicateAddresses() {
$addresses = [
'user@example.com',
];
$data = array_map(function($address) {
$ca = new CollectedAddress();
$ca->setEmail($address);
$ca->setUserId($this->userId);
return $ca;
}, $addresses);

$this->mapper->expects($this->at(0))
->method('exists')
->with($this->userId, $addresses[0])
->will($this->returnValue(true));
$this->mapper->expects($this->never())
->method('insert');

$this->collector->addAddresses($addresses);
}

public function testSearchAddress() {
$term = 'john';
$mapperResult = ['some', 'data'];

$this->mapper->expects($this->once())
->method('findMatching')
->with($this->userId, $term)
->will($this->returnValue($mapperResult));

$result = $this->collector->searchAddress($term);

$this->assertequals($mapperResult, $result);
}

}
72 changes: 72 additions & 0 deletions tests/service/autocompletion/autocompleteservicetest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* ownCloud - Mail
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @copyright Christoph Wurst 2016
*/

namespace OCA\Mail\Tests\Service\Autocompletion;

use PHPUnit_Framework_TestCase;
use OCA\Mail\Service\AutoCompletion\AutoCompleteService;

class AutoCompleteServiceTest extends PHPUnit_Framework_TestCase {

private $contactsIntegration;
private $addressCollector;
private $service;

protected function setUp() {
parent::setUp();

$this->contactsIntegration = $this->getMockBuilder('\OCA\Mail\Service\ContactsIntegration')
->disableOriginalConstructor()
->getMock();
$this->addressCollector = $this->getMockBuilder('\OCA\Mail\Service\AutoCompletion\AddressCollector')
->disableOriginalConstructor()
->getMock();

$this->service = new AutoCompleteService($this->contactsIntegration,
$this->addressCollector);
}

public function testFindMatches() {
$term = 'jo';

$contactsResult = [
['id' => 12, 'label' => 'john doe', 'value' => 'john doe'],
['id' => 13, 'label' => 'joe doe', 'value' => 'joe doe'],
];
$john = new \OCA\Mail\Db\CollectedAddress();
$john->setId(1234);
$john->setEmail('john@doe.com');
$john->setUserId('testuser');
$collectedResult = [
$john,
];

$this->contactsIntegration->expects($this->once())
->method('getMatchingRecipient')
->with($term)
->will($this->returnValue($contactsResult));
$this->addressCollector->expects($this->once())
->method('searchAddress')
->with($term)
->will($this->returnValue($collectedResult));

$response = $this->service->findMatches($term);

$expected = [
['id' => 12, 'label' => 'john doe', 'value' => 'john doe'],
['id' => 13, 'label' => 'joe doe', 'value' => 'joe doe'],
['id' => 1234, 'label' => 'john@doe.com', 'value' => 'john@doe.com'],
];
$this->assertEquals($expected, $response);
}

}

0 comments on commit 5c86661

Please sign in to comment.