Skip to content

Commit

Permalink
fix(tests): Fix checking legacy events in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jun 2, 2023
1 parent 01f4507 commit d4f4997
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 30 deletions.
3 changes: 2 additions & 1 deletion apps/files_trashbin/tests/StorageTest.php
Expand Up @@ -31,6 +31,7 @@
*/
namespace OCA\Files_Trashbin\Tests;

use OC\EventDispatcher\SymfonyAdapter;
use OC\Files\Filesystem;
use OC\Files\Storage\Common;
use OC\Files\Storage\Local;
Expand Down Expand Up @@ -607,7 +608,7 @@ public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisab
$userManager->expects($this->any())
->method('userExists')->willReturn($userExists);
$logger = $this->getMockBuilder(ILogger::class)->getMock();
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher = $this->createMock(SymfonyAdapter::class);
$rootFolder = $this->createMock(IRootFolder::class);
$userFolder = $this->createMock(Folder::class);
$node = $this->getMockBuilder(Node::class)->disableOriginalConstructor()->getMock();
Expand Down
3 changes: 2 additions & 1 deletion apps/settings/tests/Controller/CheckSetupControllerTest.php
Expand Up @@ -38,6 +38,7 @@
use Doctrine\DBAL\Platforms\SqlitePlatform;
use OC;
use OC\DB\Connection;
use OC\EventDispatcher\SymfonyAdapter;
use OC\IntegrityCheck\Checker;
use OC\MemoryInfo;
use OC\Security\SecureRandom;
Expand Down Expand Up @@ -137,7 +138,7 @@ protected function setUp(): void {
->willReturnCallback(function ($message, array $replace) {
return vsprintf($message, $replace);
});
$this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
$this->dispatcher = $this->getMockBuilder(SymfonyAdapter::class)
->disableOriginalConstructor()->getMock();
$this->checker = $this->getMockBuilder('\OC\IntegrityCheck\Checker')
->disableOriginalConstructor()->getMock();
Expand Down
6 changes: 4 additions & 2 deletions apps/workflowengine/tests/ManagerTest.php
Expand Up @@ -26,6 +26,7 @@
*/
namespace OCA\WorkflowEngine\Tests;

use OC\EventDispatcher\SymfonyAdapter;
use OC\L10N\L10N;
use OCA\WorkflowEngine\Entity\File;
use OCA\WorkflowEngine\Helper\ScopeContext;
Expand Down Expand Up @@ -94,7 +95,7 @@ protected function setUp(): void {
return vsprintf($text, $parameters);
});

$this->legacyDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->legacyDispatcher = $this->createMock(SymfonyAdapter::class);
$this->logger = $this->createMock(ILogger::class);
$this->session = $this->createMock(IUserSession::class);
$this->dispatcher = $this->createMock(IEventDispatcher::class);
Expand Down Expand Up @@ -535,8 +536,9 @@ public function testGetEntitiesList() {
$this->legacyDispatcher->expects($this->once())
->method('dispatch')
->with('OCP\WorkflowEngine::registerEntities', $this->anything())
->willReturnCallback(function () use ($extraEntity) {
->willReturnCallback(function ($eventName, $event) use ($extraEntity) {
$this->manager->registerEntity($extraEntity);
return $event;
});

$entities = $this->manager->getEntitiesList();
Expand Down
2 changes: 1 addition & 1 deletion lib/private/EventDispatcher/SymfonyAdapter.php
Expand Up @@ -113,7 +113,7 @@ public function dispatch($eventName, $event = null): object {

// Event with no payload (object) need special handling
if ($newEvent === null) {
$this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName);
$this->eventDispatcher->getSymfonyDispatcher()->dispatch(new GenericEvent(), $eventName);
return new Event();
}

Expand Down
6 changes: 4 additions & 2 deletions tests/lib/Accounts/AccountManagerTest.php
Expand Up @@ -26,6 +26,7 @@

use OC\Accounts\Account;
use OC\Accounts\AccountManager;
use OC\EventDispatcher\SymfonyAdapter;
use OCA\Settings\BackgroundJobs\VerifyUserData;
use OCP\Accounts\IAccountManager;
use OCP\BackgroundJob\IJobList;
Expand Down Expand Up @@ -86,7 +87,7 @@ class AccountManagerTest extends TestCase {

protected function setUp(): void {
parent::setUp();
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->eventDispatcher = $this->createMock(SymfonyAdapter::class);
$this->connection = \OC::$server->get(IDBConnection::class);
$this->config = $this->createMock(IConfig::class);
$this->jobList = $this->createMock(IJobList::class);
Expand Down Expand Up @@ -511,11 +512,12 @@ function ($eventName, $event) use ($user, $newData) {
$this->assertInstanceOf(GenericEvent::class, $event);
$this->assertSame($user, $event->getSubject());
$this->assertSame($newData, $event->getArguments());
return $event;
}
);
}

$this->invokePrivate($accountManager, 'updateUser', [$user, $newData, $oldData]);
self::invokePrivate($accountManager, 'updateUser', [$user, $newData, $oldData]);
}

public function dataTrueFalse(): array {
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/App/AppManagerTest.php
Expand Up @@ -13,6 +13,7 @@

use OC\App\AppManager;
use OC\AppConfig;
use OC\EventDispatcher\SymfonyAdapter;
use OCP\App\AppPathNotFoundException;
use OCP\App\Events\AppDisableEvent;
use OCP\App\Events\AppEnableEvent;
Expand Down Expand Up @@ -114,7 +115,7 @@ protected function setUp(): void {
$this->appConfig = $this->getAppConfig();
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->cache = $this->createMock(ICache::class);
$this->legacyEventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->legacyEventDispatcher = $this->createMock(SymfonyAdapter::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->cacheFactory->expects($this->any())
Expand Down
Expand Up @@ -26,12 +26,14 @@
namespace Test\AppFramework\Middleware;

use OC\AppFramework\Middleware\AdditionalScriptsMiddleware;
use OC\EventDispatcher\SymfonyAdapter;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\PublicShareController;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -54,7 +56,7 @@ class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
protected function setUp(): void {
parent::setUp();

$this->legacyDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->legacyDispatcher = $this->createMock(SymfonyAdapter::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->dispatcher = $this->createMock(IEventDispatcher::class);
$this->middleWare = new AdditionalScriptsMiddleware(
Expand Down Expand Up @@ -93,7 +95,7 @@ public function testStandaloneTemplateResponse() {
->method('dispatch')
->willReturnCallback(function ($eventName) {
if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS) {
return;
return new Event();
}

$this->fail('Wrong event dispatched');
Expand All @@ -118,7 +120,7 @@ public function testTemplateResponseNotLoggedIn() {
->method('dispatch')
->willReturnCallback(function ($eventName) {
if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS) {
return;
return new Event();
}

$this->fail('Wrong event dispatched');
Expand Down Expand Up @@ -147,7 +149,7 @@ public function testTemplateResponseLoggedIn() {
if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS ||
$eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN) {
$events[] = $eventName;
return;
return new Event();
}

$this->fail('Wrong event dispatched');
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
Expand Up @@ -27,6 +27,7 @@
use OC\Authentication\TwoFactorAuth\Manager;
use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
use OC\Authentication\TwoFactorAuth\ProviderLoader;
use OC\EventDispatcher\SymfonyAdapter;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down Expand Up @@ -103,7 +104,7 @@ protected function setUp(): void {
$this->tokenProvider = $this->createMock(TokenProvider::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->newDispatcher = $this->createMock(IEventDispatcher::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->eventDispatcher = $this->createMock(SymfonyAdapter::class);

$this->manager = new Manager(
$this->providerLoader,
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/EventDispatcher/SymfonyAdapterTest.php
Expand Up @@ -32,7 +32,7 @@
use OCP\EventDispatcher\GenericEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
use Symfony\Contracts\EventDispatcher\Event as SymfonyEvent;
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent as SymfonyGenericEvent;
Expand Down Expand Up @@ -179,6 +179,7 @@ public function testDispatchEventWithoutPayload(): void {
$symfonyDispatcher->expects(self::once())
->method('dispatch')
->with(
$this->anything(),
$eventName
)
->willReturnArgument(0);
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Group/GroupTest.php
Expand Up @@ -9,6 +9,7 @@

namespace Test\Group;

use OC\EventDispatcher\SymfonyAdapter;
use OC\User\User;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -20,7 +21,7 @@ class GroupTest extends \Test\TestCase {

protected function setUp(): void {
parent::setUp();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->dispatcher = $this->createMock(SymfonyAdapter::class);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Group/ManagerTest.php
Expand Up @@ -23,6 +23,7 @@

namespace Test\Group;

use OC\EventDispatcher\SymfonyAdapter;
use OC\Group\Database;
use OC\User\User;
use OC\User\Manager;
Expand Down Expand Up @@ -52,7 +53,7 @@ protected function setUp(): void {
parent::setUp();

$this->userManager = $this->createMock(Manager::class);
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->dispatcher = $this->createMock(SymfonyAdapter::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->cache = $this->createMock(ICacheFactory::class);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Preview/GeneratorTest.php
Expand Up @@ -23,6 +23,7 @@

namespace Test\Preview;

use OC\EventDispatcher\SymfonyAdapter;
use OC\Preview\Generator;
use OC\Preview\GeneratorHelper;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down Expand Up @@ -68,7 +69,7 @@ protected function setUp(): void {
$this->appData = $this->createMock(IAppData::class);
$this->helper = $this->createMock(GeneratorHelper::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->legacyEventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->legacyEventDispatcher = $this->createMock(SymfonyAdapter::class);

$this->generator = new Generator(
$this->config,
Expand Down
13 changes: 8 additions & 5 deletions tests/lib/Share20/ManagerTest.php
Expand Up @@ -21,6 +21,7 @@

namespace Test\Share20;

use OC\EventDispatcher\SymfonyAdapter;
use OC\Files\Mount\MoveableMount;
use OC\KnownUser\KnownUserService;
use OC\Share20\DefaultShareProvider;
Expand Down Expand Up @@ -120,7 +121,7 @@ protected function setUp(): void {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->eventDispatcher = $this->createMock(SymfonyAdapter::class);
$this->mailer = $this->createMock(IMailer::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->defaults = $this->createMock(\OC_Defaults::class);
Expand Down Expand Up @@ -258,7 +259,7 @@ public function testDelete($shareType, $sharedWith) {
$this->callBack(function (GenericEvent $e) use ($share) {
return $e->getSubject() === $share &&
$e->getArgument('deletedShares') === [$share];
})]
})],
);

$manager->deleteShare($share);
Expand Down Expand Up @@ -367,7 +368,7 @@ public function testDeleteNested() {
$this->callBack(function (GenericEvent $e) use ($share1, $share2, $share3) {
return $e->getSubject() === $share1 &&
$e->getArgument('deletedShares') === [$share3, $share2, $share1];
})]
})],
);

$manager->deleteShare($share1);
Expand Down Expand Up @@ -401,7 +402,7 @@ public function testDeleteFromSelf() {
'OCP\Share::postUnshareFromSelf',
$this->callBack(function (GenericEvent $e) use ($share) {
return $e->getSubject() === $share;
})
}),
);

$manager->deleteFromSelf($share, $recipientId);
Expand Down Expand Up @@ -550,6 +551,7 @@ public function testVerifyPasswordHook() {
$this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
/** @var ValidatePasswordPolicyEvent $event */
$this->assertSame('password', $event->getPassword());
return $event;
}
);

Expand Down Expand Up @@ -2525,10 +2527,11 @@ public function testCreateShareHookError() {
->method('dispatch')
->with(
$this->equalTo('OCP\Share::preShare'),
$this->isInstanceOf(GenericEvent::class)
$this->isInstanceOf(GenericEvent::class),
)->willReturnCallback(function ($name, GenericEvent $e) {
$e->setArgument('error', 'I won\'t let you share!');
$e->stopPropagation();
return $e;
}
);

Expand Down
4 changes: 2 additions & 2 deletions tests/lib/SystemTag/SystemTagManagerTest.php
Expand Up @@ -10,6 +10,7 @@

namespace Test\SystemTag;

use OC\EventDispatcher\SymfonyAdapter;
use OC\SystemTag\SystemTagManager;
use OC\SystemTag\SystemTagObjectMapper;
use OCP\IDBConnection;
Expand Down Expand Up @@ -52,8 +53,7 @@ protected function setUp(): void {

$this->connection = \OC::$server->getDatabaseConnection();

$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
->getMock();
$this->dispatcher = $this->createMock(SymfonyAdapter::class);

$this->groupManager = $this->getMockBuilder(IGroupManager::class)->getMock();

Expand Down
4 changes: 2 additions & 2 deletions tests/lib/SystemTag/SystemTagObjectMapperTest.php
Expand Up @@ -10,6 +10,7 @@

namespace Test\SystemTag;

use OC\EventDispatcher\SymfonyAdapter;
use OC\SystemTag\SystemTag;
use OC\SystemTag\SystemTagManager;
use OC\SystemTag\SystemTagObjectMapper;
Expand Down Expand Up @@ -72,8 +73,7 @@ protected function setUp(): void {
$this->tagManager = $this->getMockBuilder('OCP\SystemTag\ISystemTagManager')
->getMock();

$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
->getMock();
$this->dispatcher = $this->createMock(SymfonyAdapter::class);

$this->tagMapper = new SystemTagObjectMapper(
$this->connection,
Expand Down
5 changes: 3 additions & 2 deletions tests/lib/User/DatabaseTest.php
Expand Up @@ -22,6 +22,7 @@

namespace Test\User;

use OC\EventDispatcher\SymfonyAdapter;
use OC\User\User;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down Expand Up @@ -130,8 +131,8 @@ public function testSearch() {
$user2 = $this->getUser();
$this->backend->createUser($user2, 'pass1');

$user1Obj = new User($user1, $this->backend, $this->createMock(EventDispatcherInterface::class));
$user2Obj = new User($user2, $this->backend, $this->createMock(EventDispatcherInterface::class));
$user1Obj = new User($user1, $this->backend, $this->createMock(SymfonyAdapter::class));
$user2Obj = new User($user2, $this->backend, $this->createMock(SymfonyAdapter::class));
$emailAddr1 = "$user1@nextcloud.com";
$emailAddr2 = "$user2@nextcloud.com";

Expand Down
3 changes: 2 additions & 1 deletion tests/lib/User/ManagerTest.php
Expand Up @@ -10,6 +10,7 @@
namespace Test\User;

use OC\AllConfig;
use OC\EventDispatcher\SymfonyAdapter;
use OC\User\Database;
use OC\User\Manager;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down Expand Up @@ -43,7 +44,7 @@ protected function setUp(): void {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->oldDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->oldDispatcher = $this->createMock(SymfonyAdapter::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->cache = $this->createMock(ICache::class);
Expand Down

0 comments on commit d4f4997

Please sign in to comment.