Skip to content

Commit

Permalink
test: Add tests for Listeners
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Oct 1, 2024
1 parent d973740 commit a1ebcef
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .github/workflows/phpunit-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ jobs:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}

- name: Checkout Circles
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: nextcloud/circles
ref: master
path: apps/circles

- name: Checkout app
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand All @@ -113,6 +120,10 @@ jobs:
echo "SET GLOBAL sql_mode=(SELECT CONCAT(@@sql_mode,',ONLY_FULL_GROUP_BY'));" | mysql -h 127.0.0.1 -P 4444 -u root -prootpassword
echo 'SELECT @@sql_mode;' | mysql -h 127.0.0.1 -P 4444 -u root -prootpassword
- name: Set up Circles dependencies
working-directory: apps/circles
run: composer i

- name: Check composer file existence
id: check_composer
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
Expand All @@ -131,6 +142,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=mysql --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ app:enable --force circles
./occ app:enable --force ${{ env.APP_NAME }}
- name: Check PHPUnit script is defined
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/phpunit-oci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ jobs:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}

- name: Checkout Circles
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: nextcloud/circles
ref: master
path: apps/circles

- name: Checkout app
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand All @@ -121,6 +128,10 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Circles dependencies
working-directory: apps/circles
run: composer i

- name: Check composer file existence
id: check_composer
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
Expand All @@ -139,6 +150,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=oci --database-name=XE --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=autotest --database-pass=owncloud --admin-user admin --admin-pass admin
./occ app:enable --force circles
./occ app:enable --force ${{ env.APP_NAME }}
- name: Check PHPUnit script is defined
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/phpunit-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ jobs:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}

- name: Checkout Circles
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: nextcloud/circles
ref: master
path: apps/circles

- name: Checkout app
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand All @@ -111,6 +118,10 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Circles dependencies
working-directory: apps/circles
run: composer i

- name: Check composer file existence
id: check_composer
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
Expand All @@ -129,6 +140,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=pgsql --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ app:enable --force circles
./occ app:enable --force ${{ env.APP_NAME }}
- name: Check PHPUnit script is defined
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/phpunit-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ jobs:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}

- name: Checkout Circles
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: nextcloud/circles
ref: master
path: apps/circles

- name: Checkout app
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand All @@ -100,6 +107,10 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Circles dependencies
working-directory: apps/circles
run: composer i

- name: Check composer file existence
id: check_composer
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
Expand All @@ -118,6 +129,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=sqlite --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ app:enable --force circles
./occ app:enable --force ${{ env.APP_NAME }}
- name: Check PHPUnit script is defined
Expand Down
63 changes: 63 additions & 0 deletions tests/Listeners/CircleDestroyedEventListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\GroupFolders\Tests\Listeners;

use OCA\Circles\Events\CircleDestroyedEvent;
use OCA\Circles\Model\Circle;
use OCA\GroupFolders\Folder\FolderManager;
use OCA\GroupFolders\Listeners\CircleDestroyedEventListener;
use OCP\EventDispatcher\Event;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class CircleDestroyedEventListenerTest extends TestCase {
private FolderManager&MockObject $folderManager;
private CircleDestroyedEventListener $listener;

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

$this->folderManager = $this->createMock(FolderManager::class);

$this->listener = new CircleDestroyedEventListener($this->folderManager);
}

public function testHandleInvalid(): void {
$event = $this->createMock(Event::class);

$this->folderManager
->expects($this->never())
->method('deleteCircle');

/** @psalm-suppress InvalidArgument on purpose */
$this->listener->handle($event);
}

public function testHandle(): void {
$circle = $this->createMock(Circle::class);
$circle
->expects($this->once())
->method('getSingleId')
->willReturn('123');

$event = $this->createMock(CircleDestroyedEvent::class);
$event
->expects($this->once())
->method('getCircle')
->willReturn($circle);

$this->folderManager
->expects($this->once())
->method('deleteCircle')
->with('123');

$this->listener->handle($event);
}
}
51 changes: 51 additions & 0 deletions tests/Listeners/LoadAdditionalScriptsListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\GroupFolders\Tests\Listeners;

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\GroupFolders\Listeners\LoadAdditionalScriptsListener;
use OCP\EventDispatcher\Event;
use OCP\Util;
use Test\TestCase;

class LoadAdditionalScriptsListenerTest extends TestCase {
private LoadAdditionalScriptsListener $listener;

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

$this->listener = new LoadAdditionalScriptsListener();
}

public static function handleProvider(): array {
$expectedScripts = [
'groupfolders/l10n/en',
'groupfolders/js/groupfolders-init',
'groupfolders/js/groupfolders-files',
];
return [
[Event::class, []],
[LoadAdditionalScriptsEvent::class, $expectedScripts],
[BeforeTemplateRenderedEvent::class, $expectedScripts],
];
}

/**
* @dataProvider handleProvider
* @param class-string $class
*/
public function testHandle(string $class, array $expectedScripts): void {
$event = $this->createMock($class);

$this->listener->handle($event);

Check failure on line 48 in tests/Listeners/LoadAdditionalScriptsListenerTest.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

ArgumentTypeCoercion

tests/Listeners/LoadAdditionalScriptsListenerTest.php:48:27: ArgumentTypeCoercion: Argument 1 of OCA\GroupFolders\Listeners\LoadAdditionalScriptsListener::handle expects OCA\Files\Event\LoadAdditionalScriptsEvent|OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent, but parent type PHPUnit\Framework\MockObject\MockObject&(RealInstanceType:fn-phpunit\framework\testcase::createmock as object) provided (see https://psalm.dev/193)
$this->assertEquals($expectedScripts, array_values(Util::getScripts()));
}
}
Loading

0 comments on commit a1ebcef

Please sign in to comment.