From fb766fb1a5f8df9ed1b9842c50bfb98dec8a35b4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 29 Sep 2017 11:56:22 +0200 Subject: [PATCH] Fix unit tests Signed-off-by: Joas Schilling --- lib/Config.php | 8 +- tests/php/ConfigTest.php | 64 ++++++++++------ tests/php/Settings/Admin/SectionTest.php | 75 +++++++++++++++++++ .../StunServerTest.php} | 13 ++-- tests/php/Settings/Admin/TurnServerTest.php | 61 +++++++++++++++ 5 files changed, 187 insertions(+), 34 deletions(-) create mode 100644 tests/php/Settings/Admin/SectionTest.php rename tests/php/Settings/{AdminTest.php => Admin/StunServerTest.php} (82%) create mode 100644 tests/php/Settings/Admin/TurnServerTest.php diff --git a/lib/Config.php b/lib/Config.php index b87e18f0c..c3bf358c5 100644 --- a/lib/Config.php +++ b/lib/Config.php @@ -47,8 +47,8 @@ class Config { * @return string */ public function getStunServer() { - $config = $this->config->getAppValue('spreed', 'stun_servers', 'stun.nextcloud.com:443'); - $servers = json_decode($config); + $config = $this->config->getAppValue('spreed', 'stun_servers', json_encode(['stun.nextcloud.com:443'])); + $servers = json_decode($config, true); if ($servers === null) { return $config ?: 'stun.nextcloud.com:443'; @@ -69,9 +69,9 @@ class Config { */ public function getTurnSettings() { $config = $this->config->getAppValue('spreed', 'turn_servers'); - $servers = json_decode($config); + $servers = json_decode($config, true); - if ($servers === null || !empty($servers) || !is_array($servers)) { + if ($servers === null || empty($servers) || !is_array($servers)) { return [ 'server' => '', 'username' => '', diff --git a/tests/php/ConfigTest.php b/tests/php/ConfigTest.php index 67ad51670..aaeedea26 100644 --- a/tests/php/ConfigTest.php +++ b/tests/php/ConfigTest.php @@ -28,6 +28,11 @@ use Test\TestCase; class ConfigTest extends TestCase { public function testGetStunServer() { + $servers = [ + 'stun1.example.com:443', + 'stun2.example.com:129', + ]; + /** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory $timeFactory */ $timeFactory = $this->createMock(ITimeFactory::class); /** @var \PHPUnit_Framework_MockObject_MockObject|IConfig $config */ @@ -35,31 +40,35 @@ class ConfigTest extends TestCase { $config ->expects($this->once()) ->method('getAppValue') - ->with('spreed', 'stun_server', 'stun.nextcloud.com:443') - ->willReturn('88.198.160.129'); + ->with('spreed', 'stun_servers', json_encode(['stun.nextcloud.com:443'])) + ->willReturn(json_encode($servers)); $helper = new Config($config, $timeFactory); - $this->assertSame('88.198.160.129', $helper->getStunServer()); + + $this->assertTrue(in_array($helper->getStunServer(), $servers, true)); } public function testGenerateTurnSettings() { + /** @var \PHPUnit_Framework_MockObject_MockObject|IConfig $config */ $config = $this->createMock(IConfig::class); $config - ->expects($this->at(0)) + ->expects($this->once()) ->method('getAppValue') - ->with('spreed', 'turn_server', '') - ->willReturn('turn.example.org'); - $config - ->expects($this->at(1)) - ->method('getAppValue') - ->with('spreed', 'turn_server_secret', '') - ->willReturn('thisisasupersecretsecret'); - $config - ->expects($this->at(2)) - ->method('getAppValue') - ->with('spreed', 'turn_server_protocols', '') - ->willReturn('udp,tcp'); + ->with('spreed', 'turn_servers', '') + ->willReturn(json_encode([ + [ + 'server' => 'turn.example.org', + 'secret' => 'thisisasupersecretsecret', + 'protocols' => 'udp,tcp', + ], + [ + 'server' => 'turn2.example.com', + 'secret' => 'ThisIsAlsoSuperSecret', + 'protocols' => 'tcp', + ], + ])); + /** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory $timeFactory */ $timeFactory = $this->createMock(ITimeFactory::class); $timeFactory @@ -69,11 +78,22 @@ class ConfigTest extends TestCase { $helper = new Config($config, $timeFactory); - $this->assertSame(array( - 'server' => 'turn.example.org', - 'username' => '1479829425', - 'password' => 'ZY8fZQxAw/24gT0XYnMlcepUFlI=', - 'protocols' => 'udp,tcp', - ), $helper->getTurnSettings()); + // + $server = $helper->getTurnSettings(); + if ($server['server'] === 'turn.example.org') { + $this->assertSame([ + 'server' => 'turn.example.org', + 'username' => '1479829425', + 'password' => 'ZY8fZQxAw/24gT0XYnMlcepUFlI=', + 'protocols' => 'udp,tcp', + ], $server); + } else { + $this->assertSame([ + 'server' => 'turn2.example.com', + 'username' => '1479829425', + 'password' => 'VoqRpE4ktQ85TqFps8Qt+scEEvE=', + 'protocols' => 'tcp', + ], $server); + } } } diff --git a/tests/php/Settings/Admin/SectionTest.php b/tests/php/Settings/Admin/SectionTest.php new file mode 100644 index 000000000..43e94972d --- /dev/null +++ b/tests/php/Settings/Admin/SectionTest.php @@ -0,0 +1,75 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + * + */ + +namespace OCA\Spreed\Tests\php\Settings\Admin; + +use OCA\Spreed\Settings\Admin\Section; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IURLGenerator; + +class SectionTest extends \Test\TestCase { + + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + protected $url; + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + protected $l; + /** @var Section */ + protected $admin; + + public function setUp() { + parent::setUp(); + + $this->url = $this->createMock(IURLGenerator::class); + $this->l = $this->createMock(IL10N::class); + + $this->admin = new Section($this->url, $this->l); + } + + public function testGetID() { + $this->assertInternalType('string', $this->admin->getID()); + $this->assertNotEmpty($this->admin->getID()); + } + + public function testGetName() { + $this->l->expects($this->exactly(2)) + ->method('t') + ->with('Video calls') + ->willReturnArgument(0); + $this->assertInternalType('string', $this->admin->getName()); + $this->assertNotEmpty($this->admin->getName()); + } + + public function testGetIcon() { + $this->url->expects($this->exactly(2)) + ->method('imagePath') + ->with('spreed', 'app-dark.svg') + ->willReturn('apps/spreed/img/app-dark.svg'); + $this->assertInternalType('string', $this->admin->getIcon()); + $this->assertNotEmpty($this->admin->getIcon()); + } + + public function testGetPriority() { + $this->assertInternalType('int', $this->admin->getPriority()); + $this->assertGreaterThan(0, $this->admin->getPriority()); + } +} diff --git a/tests/php/Settings/AdminTest.php b/tests/php/Settings/Admin/StunServerTest.php similarity index 82% rename from tests/php/Settings/AdminTest.php rename to tests/php/Settings/Admin/StunServerTest.php index e0cf1777b..8c021fafb 100644 --- a/tests/php/Settings/AdminTest.php +++ b/tests/php/Settings/Admin/StunServerTest.php @@ -19,17 +19,17 @@ * */ -namespace OCA\Spreed\Tests\php\Settings; +namespace OCA\Spreed\Tests\php\Settings\Admin; -use OCA\Spreed\Settings\Admin; +use OCA\Spreed\Settings\Admin\StunServer; use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; -class AdminTest extends \Test\TestCase { +class StunServerTest extends \Test\TestCase { /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ protected $config; - /** @var Admin */ + /** @var StunServer */ protected $admin; public function setUp() { @@ -37,7 +37,7 @@ class AdminTest extends \Test\TestCase { $this->config = $this->createMock(IConfig::class); - $this->admin = new Admin($this->config); + $this->admin = new StunServer($this->config); } public function testGetSection() { @@ -57,8 +57,5 @@ class AdminTest extends \Test\TestCase { $params = $form->getParams(); $this->assertArrayHasKey('stunServer', $params); - $this->assertArrayHasKey('turnServer', $params); - $this->assertArrayHasKey('turnServerSecret', $params); - $this->assertArrayHasKey('turnServerProtocols', $params); } } diff --git a/tests/php/Settings/Admin/TurnServerTest.php b/tests/php/Settings/Admin/TurnServerTest.php new file mode 100644 index 000000000..b13e36ce2 --- /dev/null +++ b/tests/php/Settings/Admin/TurnServerTest.php @@ -0,0 +1,61 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + * + */ + +namespace OCA\Spreed\Tests\php\Settings\Admin; + +use OCA\Spreed\Settings\Admin\TurnServer; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; + +class TurnServerTest extends \Test\TestCase { + + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var TurnServer */ + protected $admin; + + public function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + + $this->admin = new TurnServer($this->config); + } + + public function testGetSection() { + $this->assertInternalType('string', $this->admin->getSection()); + $this->assertNotEmpty($this->admin->getSection()); + } + + public function testGetPriority() { + $this->assertInternalType('int', $this->admin->getPriority()); + $this->assertGreaterThan(0, $this->admin->getPriority()); + } + + public function testGetForm() { + $form = $this->admin->getForm(); + $this->assertInstanceOf(TemplateResponse::class, $form); + $this->assertSame('', $form->getRenderAs()); + + $params = $form->getParams(); + $this->assertArrayHasKey('turnServer', $params); + } +}