From 51466714b5ba2506c22c2d50ad0d7f3951a358ce Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 17 Dec 2024 07:45:15 +0100 Subject: [PATCH 01/12] test: add boilerplate testing infra --- .gitignore | 1 + composer.json | 23 ++++++++++++++++++++--- tests/fixtures/.gitkeep | 0 tests/integration/setup.php | 9 +++++++++ tests/phpunit.integration.xml | 25 +++++++++++++++++++++++++ tests/phpunit.unit.xml | 27 +++++++++++++++++++++++++++ tests/unit/.gitkeep | 0 7 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/.gitkeep create mode 100644 tests/integration/setup.php create mode 100644 tests/phpunit.integration.xml create mode 100644 tests/phpunit.unit.xml create mode 100644 tests/unit/.gitkeep diff --git a/.gitignore b/.gitignore index d34e123..528d586 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules vendor composer.lock js/dist +.phpunit.result.cache diff --git a/composer.json b/composer.json index 5150fb2..4563316 100644 --- a/composer.json +++ b/composer.json @@ -71,13 +71,30 @@ "flarum/subscriptions": "*", "flarum/phpstan": "*", "flarum/mentions": "*", - "flarum/gdpr": "dev-main" + "flarum/gdpr": "dev-main", + "flarum/testing": "^1.0.0" + }, + "autoload-dev": { + "psr-4": { + "FoF\\FollowTags\\": "tests/" + } }, "scripts": { "analyse:phpstan": "phpstan analyse", - "clear-cache:phpstan": "phpstan clear-result-cache" + "clear-cache:phpstan": "phpstan clear-result-cache", + "test": [ + "@test:unit", + "@test:integration" + ], + "test:unit": "phpunit -c tests/phpunit.unit.xml", + "test:integration": "phpunit -c tests/phpunit.integration.xml", + "test:setup": "@php tests/integration/setup.php" }, "scripts-descriptions": { - "analyse:phpstan": "Run static analysis" + "analyse:phpstan": "Run static analysis", + "test": "Runs all tests.", + "test:unit": "Runs all unit tests.", + "test:integration": "Runs all integration tests.", + "test:setup": "Sets up a database for use with integration tests. Execute this only once." } } diff --git a/tests/fixtures/.gitkeep b/tests/fixtures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/setup.php b/tests/integration/setup.php new file mode 100644 index 0000000..5f24462 --- /dev/null +++ b/tests/integration/setup.php @@ -0,0 +1,9 @@ +run(); diff --git a/tests/phpunit.integration.xml b/tests/phpunit.integration.xml new file mode 100644 index 0000000..90fbbff --- /dev/null +++ b/tests/phpunit.integration.xml @@ -0,0 +1,25 @@ + + + + + ../src/ + + + + + ./integration + ./integration/tmp + + + diff --git a/tests/phpunit.unit.xml b/tests/phpunit.unit.xml new file mode 100644 index 0000000..d3a4a3e --- /dev/null +++ b/tests/phpunit.unit.xml @@ -0,0 +1,27 @@ + + + + + ../src/ + + + + + ./unit + + + + + + diff --git a/tests/unit/.gitkeep b/tests/unit/.gitkeep new file mode 100644 index 0000000..e69de29 From dbed6a1bea5cc557057710e8ab3c45e10126855f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 17 Dec 2024 06:46:55 +0000 Subject: [PATCH 02/12] Apply fixes from StyleCI --- tests/integration/setup.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/integration/setup.php b/tests/integration/setup.php index 5f24462..90eee37 100644 --- a/tests/integration/setup.php +++ b/tests/integration/setup.php @@ -1,5 +1,14 @@ Date: Tue, 17 Dec 2024 07:55:58 +0100 Subject: [PATCH 03/12] test: add reusable traits --- tests/integration/ExtensionDepsTrait.php | 13 ++++++++++++ tests/integration/NotificationsTest.php | 24 +++++++++++++++++++++++ tests/integration/TagsDefinitionTrait.php | 15 ++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/integration/ExtensionDepsTrait.php create mode 100644 tests/integration/NotificationsTest.php create mode 100644 tests/integration/TagsDefinitionTrait.php diff --git a/tests/integration/ExtensionDepsTrait.php b/tests/integration/ExtensionDepsTrait.php new file mode 100644 index 0000000..a071d78 --- /dev/null +++ b/tests/integration/ExtensionDepsTrait.php @@ -0,0 +1,13 @@ +extension('flarum-tags'); + $this->extension('fof-extend'); + $this->extension('fof-follow-tags'); + } +} diff --git a/tests/integration/NotificationsTest.php b/tests/integration/NotificationsTest.php new file mode 100644 index 0000000..908fd07 --- /dev/null +++ b/tests/integration/NotificationsTest.php @@ -0,0 +1,24 @@ +extensionDeps(); + + $this->prepareDatabase([ + 'tags' => $this->tags(), + ]); + } +} diff --git a/tests/integration/TagsDefinitionTrait.php b/tests/integration/TagsDefinitionTrait.php new file mode 100644 index 0000000..ee73951 --- /dev/null +++ b/tests/integration/TagsDefinitionTrait.php @@ -0,0 +1,15 @@ + 1, 'name' => 'General', 'slug' => 'general', 'position' => 0, 'parent_id' => null], + ['id' => 2, 'name' => 'Testing', 'slug' => 'testing', 'position' => 1, 'parent_id' => null], + ['id' => 3, 'name' => 'Archive', 'slug' => 'archive', 'position' => 2, 'parent_id' => null, 'is_restricted' => true], + ]; + } +} From 5adcea53b8b3ffdcb64d95469be33072230dd181 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 17 Dec 2024 06:56:11 +0000 Subject: [PATCH 04/12] Apply fixes from StyleCI --- tests/integration/ExtensionDepsTrait.php | 9 +++++++++ tests/integration/NotificationsTest.php | 11 +++++++++-- tests/integration/TagsDefinitionTrait.php | 9 +++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/integration/ExtensionDepsTrait.php b/tests/integration/ExtensionDepsTrait.php index a071d78..404f95b 100644 --- a/tests/integration/ExtensionDepsTrait.php +++ b/tests/integration/ExtensionDepsTrait.php @@ -1,5 +1,14 @@ Date: Tue, 17 Dec 2024 07:58:19 +0100 Subject: [PATCH 05/12] chore: change colocation --- tests/integration/{ => notifications}/NotificationsTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename tests/integration/{ => notifications}/NotificationsTest.php (76%) diff --git a/tests/integration/NotificationsTest.php b/tests/integration/notifications/NotificationsTest.php similarity index 76% rename from tests/integration/NotificationsTest.php rename to tests/integration/notifications/NotificationsTest.php index 65bb139..f79df0c 100644 --- a/tests/integration/NotificationsTest.php +++ b/tests/integration/notifications/NotificationsTest.php @@ -9,9 +9,11 @@ * file that was distributed with this source code. */ -namespace FoF\FollowTags\tests\integration; +namespace FoF\FollowTags\tests\integration\notifications; use Flarum\Testing\integration\TestCase; +use FoF\FollowTags\tests\integration\ExtensionDepsTrait; +use FoF\FollowTags\tests\integration\TagsDefinitionTrait; class NotificationsTest extends TestCase { From af273755201e0fa808be34281f8fdaa4e3245b8b Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 17 Dec 2024 08:14:18 +0100 Subject: [PATCH 06/12] test: add boilerplate data --- .../notifications/NotificationsTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/integration/notifications/NotificationsTest.php b/tests/integration/notifications/NotificationsTest.php index f79df0c..19d7b27 100644 --- a/tests/integration/notifications/NotificationsTest.php +++ b/tests/integration/notifications/NotificationsTest.php @@ -11,12 +11,15 @@ namespace FoF\FollowTags\tests\integration\notifications; +use Carbon\Carbon; use Flarum\Testing\integration\TestCase; +use Flarum\Testing\integration\RetrievesAuthorizedUsers; use FoF\FollowTags\tests\integration\ExtensionDepsTrait; use FoF\FollowTags\tests\integration\TagsDefinitionTrait; class NotificationsTest extends TestCase { + use RetrievesAuthorizedUsers; use ExtensionDepsTrait; use TagsDefinitionTrait; @@ -27,7 +30,19 @@ public function setUp(): void $this->extensionDeps(); $this->prepareDatabase([ + 'users' => [ + $this->normalUser(), + ], 'tags' => $this->tags(), + 'tag_user' => [ + ['user_id' => 2, 'tag_id' => 1, 'is_hidden' => 0 , 'subscription' => 'follow', 'created_at' => Carbon::now()->toDateTimeString()], + ], + 'discussions' => [ + ['id' => 1, 'title' => 'The quick brown fox jumps over the lazy dog', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'participant_count' => 1], + ], + 'posts' => [ + ['id' => 1, 'discussion_id' => 1, 'user_id' => 2, 'type' => 'comment', 'content' => '

Text

', 'is_private' => 0, 'is_approved' => 1, 'number' => 1], + ], ]); } } From ca7e178712cc23b841d9a382ee62ff0b5cb5e4cf Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 17 Dec 2024 07:14:29 +0000 Subject: [PATCH 07/12] Apply fixes from StyleCI --- tests/integration/notifications/NotificationsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integration/notifications/NotificationsTest.php b/tests/integration/notifications/NotificationsTest.php index 19d7b27..d92e0a9 100644 --- a/tests/integration/notifications/NotificationsTest.php +++ b/tests/integration/notifications/NotificationsTest.php @@ -12,8 +12,8 @@ namespace FoF\FollowTags\tests\integration\notifications; use Carbon\Carbon; -use Flarum\Testing\integration\TestCase; use Flarum\Testing\integration\RetrievesAuthorizedUsers; +use Flarum\Testing\integration\TestCase; use FoF\FollowTags\tests\integration\ExtensionDepsTrait; use FoF\FollowTags\tests\integration\TagsDefinitionTrait; @@ -33,9 +33,9 @@ public function setUp(): void 'users' => [ $this->normalUser(), ], - 'tags' => $this->tags(), + 'tags' => $this->tags(), 'tag_user' => [ - ['user_id' => 2, 'tag_id' => 1, 'is_hidden' => 0 , 'subscription' => 'follow', 'created_at' => Carbon::now()->toDateTimeString()], + ['user_id' => 2, 'tag_id' => 1, 'is_hidden' => 0, 'subscription' => 'follow', 'created_at' => Carbon::now()->toDateTimeString()], ], 'discussions' => [ ['id' => 1, 'title' => 'The quick brown fox jumps over the lazy dog', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'participant_count' => 1], From bd942944e9c4cf43d9d9915e57e08322dc6bb47c Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 17 Dec 2024 08:30:20 +0100 Subject: [PATCH 08/12] test: more test data --- tests/integration/notifications/NotificationsTest.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/integration/notifications/NotificationsTest.php b/tests/integration/notifications/NotificationsTest.php index d92e0a9..0a792f6 100644 --- a/tests/integration/notifications/NotificationsTest.php +++ b/tests/integration/notifications/NotificationsTest.php @@ -36,12 +36,19 @@ public function setUp(): void 'tags' => $this->tags(), 'tag_user' => [ ['user_id' => 2, 'tag_id' => 1, 'is_hidden' => 0, 'subscription' => 'follow', 'created_at' => Carbon::now()->toDateTimeString()], + ['user_id' => 2, 'tag_id' => 2, 'is_hidden' => 0, 'subscription' => 'lurking', 'created_at' => Carbon::now()->toDateTimeString()], + ], + 'discussion_tag' => [ + ['discussion_id' => 1, 'tag_id' => 1, 'created_at' => Carbon::now()->toDateTimeString()], + ['discussion_id' => 2, 'tag_id' => 2, 'created_at' => Carbon::now()->toDateTimeString()], ], 'discussions' => [ ['id' => 1, 'title' => 'The quick brown fox jumps over the lazy dog', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'participant_count' => 1], + ['id' => 2, 'title' => 'The quick brown fox jumps over the lazy dog', 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'participant_count' => 1], ], 'posts' => [ - ['id' => 1, 'discussion_id' => 1, 'user_id' => 2, 'type' => 'comment', 'content' => '

Text

', 'is_private' => 0, 'is_approved' => 1, 'number' => 1], + ['id' => 1, 'discussion_id' => 1, 'user_id' => 2, 'type' => 'comment', 'content' => '

Following

', 'is_private' => 0, 'is_approved' => 1, 'number' => 1], + ['id' => 2, 'discussion_id' => 2, 'user_id' => 2, 'type' => 'comment', 'content' => '

Lurking

', 'is_private' => 0, 'is_approved' => 1, 'number' => 1], ], ]); } From 514d6d1a1c421b655512756040a2eabaf8aaf7d3 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 17 Dec 2024 09:55:01 +0100 Subject: [PATCH 09/12] test: first test --- .../notifications/NotificationsTest.php | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/integration/notifications/NotificationsTest.php b/tests/integration/notifications/NotificationsTest.php index 0a792f6..3439045 100644 --- a/tests/integration/notifications/NotificationsTest.php +++ b/tests/integration/notifications/NotificationsTest.php @@ -52,4 +52,70 @@ public function setUp(): void ], ]); } + + /** + * @test + */ + public function notification_sent_when_new_discussion_in_followed_tag() + { + $response = $this->send( + $this->request('POST', '/api/discussions', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'title' => 'New discussion', + 'content' => '

New Post

', + ], + 'relationships' => [ + 'tags' => [ + 'data' => [ + ['type' => 'tags', 'id' => 1] + ] + ] + ] + ], + ], + ]) + ); + + $this->assertEquals(201, $response->getStatusCode()); + + $response = $this->send( + $this->request('GET', '/api/notifications', [ + 'authenticatedAs' => 2, + ]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $response = json_decode($response->getBody(), true); + + $this->assertEquals(1, count($response['data'])); + $this->assertEquals('newDiscussionInTag', $response['data'][0]['attributes']['contentType']); + } + + /** + * @test + */ + public function no_notification_sent_when_new_post_in_followed_tag() + { + + } + + /** + * @test + */ + public function notification_sent_when_new_discussion_in_lurked_tag() + { + + } + + /** + * @test + */ + public function notification_sent_when_new_post_in_lurked_tag() + { + + } } From 3e95ec6210cea139b09a3487c7af78b26bd5bd2a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 17 Dec 2024 08:55:14 +0000 Subject: [PATCH 10/12] Apply fixes from StyleCI --- .../notifications/NotificationsTest.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tests/integration/notifications/NotificationsTest.php b/tests/integration/notifications/NotificationsTest.php index 3439045..9be1ce2 100644 --- a/tests/integration/notifications/NotificationsTest.php +++ b/tests/integration/notifications/NotificationsTest.php @@ -61,19 +61,19 @@ public function notification_sent_when_new_discussion_in_followed_tag() $response = $this->send( $this->request('POST', '/api/discussions', [ 'authenticatedAs' => 1, - 'json' => [ + 'json' => [ 'data' => [ 'attributes' => [ - 'title' => 'New discussion', + 'title' => 'New discussion', 'content' => '

New Post

', ], 'relationships' => [ 'tags' => [ 'data' => [ - ['type' => 'tags', 'id' => 1] - ] - ] - ] + ['type' => 'tags', 'id' => 1], + ], + ], + ], ], ], ]) @@ -100,22 +100,19 @@ public function notification_sent_when_new_discussion_in_followed_tag() */ public function no_notification_sent_when_new_post_in_followed_tag() { - } /** * @test - */ + */ public function notification_sent_when_new_discussion_in_lurked_tag() { - } /** * @test - */ + */ public function notification_sent_when_new_post_in_lurked_tag() { - } } From c40cde711dab8c127789ebce8fa70fbca2989527 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 17 Dec 2024 09:57:03 +0100 Subject: [PATCH 11/12] chore: enable backend testing in workflows --- .github/workflows/backend.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index bf77423..edde91e 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -6,7 +6,7 @@ jobs: run: uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main with: - enable_backend_testing: false + enable_backend_testing: true enable_phpstan: true php_versions: '["8.0", "8.1", "8.2", "8.3"]' backend_directory: . From 5775a0b8ed8776404074f3948c594aa6718fff77 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Thu, 19 Dec 2024 17:07:28 +0100 Subject: [PATCH 12/12] chore: fix some testing setup issues --- composer.json | 6 ++++-- tests/integration/ExtensionDepsTrait.php | 2 +- tests/integration/TagsDefinitionTrait.php | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 4563316..f684100 100644 --- a/composer.json +++ b/composer.json @@ -72,11 +72,13 @@ "flarum/phpstan": "*", "flarum/mentions": "*", "flarum/gdpr": "dev-main", - "flarum/testing": "^1.0.0" + "flarum/testing": "^1.0.0", + "flarum/tags":"*", + "fof/extend": "*" }, "autoload-dev": { "psr-4": { - "FoF\\FollowTags\\": "tests/" + "FoF\\FollowTags\\Tests\\": "tests/" } }, "scripts": { diff --git a/tests/integration/ExtensionDepsTrait.php b/tests/integration/ExtensionDepsTrait.php index 404f95b..e7e4320 100644 --- a/tests/integration/ExtensionDepsTrait.php +++ b/tests/integration/ExtensionDepsTrait.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace FoF\FollowTags\tests\integration; +namespace FoF\FollowTags\Tests\integration; trait ExtensionDepsTrait { diff --git a/tests/integration/TagsDefinitionTrait.php b/tests/integration/TagsDefinitionTrait.php index 024ea31..007e9fb 100644 --- a/tests/integration/TagsDefinitionTrait.php +++ b/tests/integration/TagsDefinitionTrait.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace FoF\FollowTags\tests\integration; +namespace FoF\FollowTags\Tests\integration; trait TagsDefinitionTrait {