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

Commit

Permalink
use public method that can be mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophWurst committed Apr 14, 2016
1 parent fd48f7b commit efd6f90
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
12 changes: 3 additions & 9 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@

namespace OCA\Mail\AppInfo;

use \OCP\AppFramework\App;
use OCP\AppFramework\App;
use OCP\Util;

class Application extends App {

public static $ispUrls = [
'https://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
'https://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
'https://autoconfig.thunderbird.net/v1.1/{DOMAIN}',
];

public function __construct(array $urlParams = []) {
parent::__construct('mail', $urlParams);

Expand All @@ -49,8 +44,7 @@ public function __construct(array $urlParams = []) {
$container->registerParameter("userFolder", $container->getServer()->getUserFolder($user));
$container->registerParameter("testSmtp", $testSmtp);
$container->registerParameter("referrer", isset($_SERVER['HTTP_REFERER']) ? : null);
$container->registerParameter("hostname", \OCP\Util::getServerHostName());
$container->registerParameter('ispUrls', self::$ispUrls);
$container->registerParameter("hostname", Util::getServerHostName());
}

}
17 changes: 14 additions & 3 deletions lib/service/autoconfig/ispdb.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @author Christoph Wurst <[email protected]>
*
Expand All @@ -17,22 +18,32 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Mail\Service\AutoConfig;

use Exception;
use OCA\Mail\Service\Logger;

class IspDb {

/** @var Logger */
private $logger;

/** @var string[] */
public function getUrls() {
return [
'https://autoconfig.{DOMAIN}/mail/config-v1.1.xml',
'https://{DOMAIN}/.well-known/autoconfig/mail/config-v1.1.xml',
'https://autoconfig.thunderbird.net/v1.1/{DOMAIN}',
];
}

/**
* @param Logger $logger
* @param string[] $ispUrls
*/
public function __construct(Logger $logger, $ispUrls) {
public function __construct(Logger $logger) {
$this->logger = $logger;
$this->urls = $ispUrls;
}

private function queryUrl($url) {
Expand Down Expand Up @@ -85,7 +96,7 @@ public function query($domain, $tryMx = true) {
}

$provider = [];
foreach ($this->urls as $url) {
foreach ($this->getUrls() as $url) {
$url = str_replace("{DOMAIN}", $domain, $url);
$this->logger->debug("IsbDb: querying <$domain> via <$url>");

Expand Down
23 changes: 18 additions & 5 deletions tests/service/autoconfig/ispdbtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@

namespace OCA\Mail\Tests\Service\Autoconfig;

use OCA\Mail\AppInfo\Application;
use OCA\Mail\Service\AutoConfig\IspDb;
use PHPUnit_Framework_TestCase;
use Test\TestCase;

class IspDbtest extends PHPUnit_Framework_TestCase {
class IspDbtest extends TestCase {

private $logger;

Expand All @@ -50,7 +49,7 @@ public function queryData() {
* @param string $domain
*/
public function testQueryRealServers($domain) {
$ispDb = new IspDb($this->logger, Application::$ispUrls);
$ispDb = new IspDb($this->logger);
$result = $ispDb->query($domain);
$this->assertContainsIspData($result);
}
Expand All @@ -69,7 +68,7 @@ public function testQueryFakeAutoconfig($domain, $shouldSucceed) {
$urls = [
dirname(__FILE__) . '/../../resources/autoconfig-freenet.xml',
];
$ispDb = new IspDb($this->logger, $urls);
$ispDb = $this->getIspDbMock($urls);

$result = $ispDb->query($domain);

Expand All @@ -80,6 +79,20 @@ public function testQueryFakeAutoconfig($domain, $shouldSucceed) {
}
}

private function getIspDbMock($urls) {
$mock = $this->getMockBuilder('\OCA\Mail\Service\AutoConfig\IspDb')
->setMethods(['getUrls'])
->setConstructorArgs([$this->logger])
->getMock();
$mock->expects($this->once())
->method('getUrls')
->will($this->returnValue($urls));
return $mock;
}

/**
* @todo check actual values
*/
private function assertContainsIspData($data) {
$this->assertArrayHasKey('imap', $data);
$this->assertTrue(count($data['imap']) >= 1, 'no isp imap data returned');
Expand Down

0 comments on commit efd6f90

Please sign in to comment.