Browse Source

Adjust tests to the new executor

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/1453/head
Joas Schilling 7 years ago
parent
commit
9c9dac5ff5
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 8
      tests/php/Chat/ChatManagerTest.php
  2. 92
      tests/php/Chat/Command/ExecutorTest.php
  3. 112
      tests/php/Chat/Command/ShellExecutorTest.php
  4. 42
      tests/php/Controller/ChatControllerTest.php

8
tests/php/Chat/ChatManagerTest.php

@ -26,6 +26,7 @@ namespace OCA\Spreed\Tests\php\Chat;
use OCA\Spreed\Chat\ChatManager;
use OCA\Spreed\Chat\CommentsManager;
use OCA\Spreed\Chat\Notifier;
use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\IComment;
@ -33,8 +34,9 @@ use OCP\Comments\ICommentsManager;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase;
class ChatManagerTest extends \Test\TestCase {
class ChatManagerTest extends TestCase {
/** @var CommentsManager|ICommentsManager|MockObject */
protected $commentsManager;
@ -114,7 +116,9 @@ class ChatManagerTest extends \Test\TestCase {
->method('notifyMentionedUsers')
->with($chat, $comment);
$this->chatManager->sendMessage($chat, 'users', 'testUser', 'testMessage', $creationDateTime);
$participant = $this->createMock(Participant::class);
$this->chatManager->sendMessage($chat, $participant, 'users', 'testUser', 'testMessage', $creationDateTime);
}
public function testGetHistory() {

92
tests/php/Chat/Command/ExecutorTest.php

@ -24,18 +24,35 @@
namespace OCA\Spreed\Tests\php\Chat\Command;
use OCA\Spreed\Chat\Command\Executor;
use OCA\Spreed\Chat\Command\ShellExecutor;
use OCA\Spreed\Model\Command;
use OCA\Spreed\Room;
use OCA\Spreed\Service\CommandService;
use OCP\Comments\IComment;
use OCP\IL10N;
use OCP\ILogger;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;
class ExecutorTest extends \Test\TestCase {
class ExecutorTest extends TestCase {
/** @var EventDispatcherInterface|MockObject */
protected $dispatcher;
/** @var ShellExecutor|MockObject */
protected $shellExecutor;
/** @var CommandService|MockObject */
protected $commandService;
/** @var ILogger|MockObject */
protected $logger;
/** @var IL10N|MockObject */
protected $l10n;
/** @var Executor */
protected $executor;
@ -43,7 +60,17 @@ class ExecutorTest extends \Test\TestCase {
parent::setUp();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->executor = new Executor($this->dispatcher);
$this->shellExecutor = $this->createMock(ShellExecutor::class);
$this->commandService = $this->createMock(CommandService::class);
$this->logger = $this->createMock(ILogger::class);
$this->l10n = $this->createMock(IL10N::class);
$this->executor = new Executor(
$this->dispatcher,
$this->shellExecutor,
$this->commandService,
$this->logger,
$this->l10n
);
}
public function dataExecApp(): array {
@ -78,7 +105,13 @@ class ExecutorTest extends \Test\TestCase {
->willReturn($expected);
$executor = $this->getMockBuilder(Executor::class)
->setConstructorArgs([$this->dispatcher])
->setConstructorArgs([
$this->dispatcher,
$this->shellExecutor,
$this->commandService,
$this->logger,
$this->l10n,
])
->setMethods(['createEvent'])
->getMock();
$executor->expects($this->once())
@ -95,9 +128,9 @@ class ExecutorTest extends \Test\TestCase {
public function dataExecShell(): array {
return [
['admin', 'token', '', '', '', ''],
['admin', 'token', '/var/www/nextcloud/script.sh {USER} {ROOM} {ARGUMENTS}', 'foo bar "hello bear"', "/var/www/nextcloud/script.sh 'admin' 'token' 'foo' 'bar' \"hello bear\"", 'output1'],
['admin', 'token', '/var/www/nextcloud/script.sh {USER} {ROOM} --arguments="{ARGUMENTS_DOUBLEQUOTE_ESCAPED}"', 'foo bar "hello bear"', "/var/www/nextcloud/script.sh 'admin' 'token' --arguments=\"foo bar \\\"hello bear\\\"\"", "out\nput\n2"],
['admin', 'token', '', '', ''],
['admin', 'token', '/var/www/nextcloud/script.sh {USER} {ROOM} {ARGUMENTS}', 'foo bar "hello bear"', 'output1'],
['admin', 'token', '/var/www/nextcloud/script.sh {USER} {ROOM} --arguments="{ARGUMENTS_DOUBLEQUOTE_ESCAPED}"', 'foo bar "hello bear"', "out\nput\n2"],
];
}
@ -107,10 +140,10 @@ class ExecutorTest extends \Test\TestCase {
* @param string $roomToken
* @param string $script
* @param string $arguments
* @param string $expected
* @param string $output
*/
public function testExecShell(?string $actorId, string $roomToken, string $script, string $arguments, string $expected, string $output): void {
public function testExecShell(?string $actorId, string $roomToken, string $script, string $arguments, string $output): void {
/** @var IComment|MockObject $message */
$message = $this->createMock(IComment::class);
if ($actorId === null) {
$message->expects($this->once())
@ -127,49 +160,28 @@ class ExecutorTest extends \Test\TestCase {
->willReturn($actorId);
}
/** @var Room|MockObject $room */
$room = $this->createMock(Room::class);
$room->expects($this->once())
->method('getToken')
->willReturn($roomToken);
/** @var Command $command */
$command = Command::fromParams([
'script' => $script,
]);
$executor = $this->getMockBuilder(Executor::class)
->setConstructorArgs([$this->dispatcher])
->setMethods(['wrapExec'])
->getMock();
$executor->expects($this->once())
->method('wrapExec')
->with($expected)
$this->shellExecutor->expects($this->once())
->method('execShell')
->with(
$script,
$arguments,
$roomToken,
(string) $actorId
)
->willReturn($output);
$this->assertSame($output, self::invokePrivate($executor, 'execShell', [$room, $message, $command, $arguments]));
$this->assertSame($output, $this->executor->execShell($room, $message, $command, $arguments));
}
public function dataEscapeArguments(): array {
return [
['foobar', "'foobar'"],
['foo bar', "'foo' 'bar'"],
['"foo" bar', "\"foo\" 'bar'"],
['"foo"bar', "'\"foo\"bar'"],
['"foo bar"', '"foo bar"'],
['"foo foo"bar bar"', '"foo foo\\"bar bar"'],
['"foo foo\"bar bar"', '"foo foo\\\\"bar bar"'],
['" foo bar "', '" foo bar "'],
['" foo bar ', "'\" foo bar '"],
];
}
/**
* @dataProvider dataEscapeArguments
* @param string $arguments
* @param string $expected
*/
public function testEscapeArguments(string $arguments, string $expected): void {
$this->assertSame($expected, self::invokePrivate($this->executor, 'escapeArguments', [$arguments]));
}
}

112
tests/php/Chat/Command/ShellExecutorTest.php

@ -0,0 +1,112 @@
<?php
/**
*
* @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Spreed\Tests\php\Chat\Command;
use OCA\Spreed\Chat\Command\Executor;
use OCA\Spreed\Chat\Command\ShellExecutor;
use OCA\Spreed\Model\Command;
use OCA\Spreed\Room;
use OCA\Spreed\Service\CommandService;
use OCP\Comments\IComment;
use OCP\IL10N;
use OCP\ILogger;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;
class ShellExecutorTest extends TestCase {
/** @var EventDispatcherInterface|MockObject */
protected $dispatcher;
/** @var ShellExecutor|MockObject */
protected $shellExecutor;
/** @var CommandService|MockObject */
protected $commandService;
/** @var ILogger|MockObject */
protected $logger;
/** @var IL10N|MockObject */
protected $l10n;
/** @var Executor */
protected $executor;
public function dataExecShell(): array {
return [
['admin', 'token', '', '', '', ''],
['admin', 'token', '/var/www/nextcloud/script.sh {USER} {ROOM} {ARGUMENTS}', 'foo bar "hello bear"', "/var/www/nextcloud/script.sh 'admin' 'token' 'foo' 'bar' \"hello bear\"", 'output1'],
['admin', 'token', '/var/www/nextcloud/script.sh {USER} {ROOM} --arguments="{ARGUMENTS_DOUBLEQUOTE_ESCAPED}"', 'foo bar "hello bear"', "/var/www/nextcloud/script.sh 'admin' 'token' --arguments=\"foo bar \\\"hello bear\\\"\"", "out\nput\n2"],
];
}
/**
* @dataProvider dataExecShell
* @param string|null $actorId
* @param string $roomToken
* @param string $cmd
* @param string $arguments
* @param string $expected
* @param string $output
*/
public function testExecShell(?string $actorId, string $roomToken, string $cmd, string $arguments, string $expected, string $output): void {
$executor = $this->getMockBuilder(ShellExecutor::class)
->setMethods(['wrapExec'])
->getMock();
$executor->expects($this->once())
->method('wrapExec')
->with($expected)
->willReturn($output);
$this->assertSame($output, self::invokePrivate($executor, 'execShell', [$cmd, $arguments, $roomToken, $actorId]));
}
public function dataEscapeArguments(): array {
return [
['foobar', "'foobar'"],
['foo bar', "'foo' 'bar'"],
['"foo" bar', "\"foo\" 'bar'"],
['"foo"bar', "'\"foo\"bar'"],
['"foo bar"', '"foo bar"'],
['"foo foo"bar bar"', '"foo foo\\"bar bar"'],
['"foo foo\"bar bar"', '"foo foo\\\\"bar bar"'],
['" foo bar "', '" foo bar "'],
['" foo bar ', "'\" foo bar '"],
];
}
/**
* @dataProvider dataEscapeArguments
* @param string $arguments
* @param string $expected
*/
public function testEscapeArguments(string $arguments, string $expected): void {
$this->assertSame($expected, self::invokePrivate(new ShellExecutor(), 'escapeArguments', [$arguments]));
}
}

42
tests/php/Controller/ChatControllerTest.php

@ -31,6 +31,7 @@ use OCA\Spreed\Exceptions\ParticipantNotFoundException;
use OCA\Spreed\Exceptions\RoomNotFoundException;
use OCA\Spreed\GuestManager;
use OCA\Spreed\Manager;
use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use OCA\Spreed\TalkSession;
use OCP\AppFramework\Http;
@ -45,8 +46,9 @@ use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class ChatControllerTest extends \Test\TestCase {
class ChatControllerTest extends TestCase {
/** @var string */
private $userId;
@ -76,7 +78,7 @@ class ChatControllerTest extends \Test\TestCase {
/** @var Room|MockObject */
protected $room;
/** @var \OCA\Spreed\Controller\ChatController */
/** @var ChatController */
private $controller;
/** @var Callback */
@ -152,6 +154,12 @@ class ChatControllerTest extends \Test\TestCase {
->with($this->userId, 'testSpreedSession')
->willReturn($this->room);
$participant = $this->createMock(Participant::class);
$this->room->expects($this->once())
->method('getParticipantBySession')
->with('testSpreedSession')
->willReturn($participant);
$this->manager->expects($this->never())
->method('getRoomForParticipantByToken');
@ -164,6 +172,7 @@ class ChatControllerTest extends \Test\TestCase {
$this->chatManager->expects($this->once())
->method('sendMessage')
->with($this->room,
$participant,
'users',
$this->userId,
'testMessage',
@ -217,6 +226,12 @@ class ChatControllerTest extends \Test\TestCase {
->with('testToken', $this->userId)
->willReturn($this->room);
$participant = $this->createMock(Participant::class);
$this->room->expects($this->once())
->method('getParticipant')
->with($this->userId)
->willReturn($participant);
$this->room->expects($this->once())
->method('getParticipant')
->with($this->userId);
@ -230,6 +245,7 @@ class ChatControllerTest extends \Test\TestCase {
$this->chatManager->expects($this->once())
->method('sendMessage')
->with($this->room,
$participant,
'users',
$this->userId,
'testMessage',
@ -311,8 +327,11 @@ class ChatControllerTest extends \Test\TestCase {
->with($this->userId, 'testSpreedSession')
->willReturn($this->room);
$this->manager->expects($this->never())
->method('getRoomForParticipantByToken');
$participant = $this->createMock(Participant::class);
$this->room->expects($this->once())
->method('getParticipantBySession')
->with('testSpreedSession')
->willReturn($participant);
$date = new \DateTime();
$this->timeFactory->expects($this->once())
@ -323,6 +342,7 @@ class ChatControllerTest extends \Test\TestCase {
$this->chatManager->expects($this->once())
->method('sendMessage')
->with($this->room,
$participant,
'guests',
sha1('testSpreedSession'),
'testMessage',
@ -408,7 +428,7 @@ class ChatControllerTest extends \Test\TestCase {
}
public function testReceiveHistoryByUser() {
$this->session->expects($this->exactly(2))
$this->session->expects($this->once())
->method('getSessionForRoom')
->with('testToken')
->willReturn('testSpreedSession');
@ -466,7 +486,7 @@ class ChatControllerTest extends \Test\TestCase {
}
public function testReceiveMessagesByUserNotJoinedButInRoom() {
$this->session->expects($this->exactly(2))
$this->session->expects($this->once())
->method('getSessionForRoom')
->with('testToken')
->willReturn(null);
@ -481,9 +501,11 @@ class ChatControllerTest extends \Test\TestCase {
->with('testToken', $this->userId)
->willReturn($this->room);
$participant = $this->createMock(Participant::class);
$this->room->expects($this->once())
->method('getParticipant')
->with($this->userId);
->with($this->userId)
->willReturn($participant);
$offset = 23;
$limit = 4;
@ -676,7 +698,7 @@ class ChatControllerTest extends \Test\TestCase {
}
public function testWaitForNewMessagesByUser() {
$this->session->expects($this->exactly(2))
$this->session->expects($this->once())
->method('getSessionForRoom')
->with('testToken')
->willReturn('testSpreedSession');
@ -740,7 +762,7 @@ class ChatControllerTest extends \Test\TestCase {
}
public function testWaitForNewMessagesTimeoutExpired() {
$this->session->expects($this->exactly(2))
$this->session->expects($this->once())
->method('getSessionForRoom')
->with('testToken')
->willReturn('testSpreedSession');
@ -778,7 +800,7 @@ class ChatControllerTest extends \Test\TestCase {
}
public function testWaitForNewMessagesTimeoutTooLarge() {
$this->session->expects($this->exactly(2))
$this->session->expects($this->once())
->method('getSessionForRoom')
->with('testToken')
->willReturn('testSpreedSession');

Loading…
Cancel
Save