Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Backend Testing #79

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: .
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
vendor
composer.lock
js/dist
.phpunit.result.cache
25 changes: 22 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,32 @@
"flarum/subscriptions": "*",
"flarum/phpstan": "*",
"flarum/mentions": "*",
"flarum/gdpr": "dev-main"
"flarum/gdpr": "dev-main",
"flarum/testing": "^1.0.0",
"flarum/tags":"*",
"fof/extend": "*"
},
"autoload-dev": {
"psr-4": {
"FoF\\FollowTags\\Tests\\": "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."
}
}
Empty file added tests/fixtures/.gitkeep
Empty file.
22 changes: 22 additions & 0 deletions tests/integration/ExtensionDepsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of fof/follow-tags.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\FollowTags\Tests\integration;

trait ExtensionDepsTrait
{
public function extensionDeps(): void
{
$this->extension('flarum-tags');
$this->extension('fof-extend');
$this->extension('fof-follow-tags');
}
}
24 changes: 24 additions & 0 deletions tests/integration/TagsDefinitionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of fof/follow-tags.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\FollowTags\Tests\integration;

trait TagsDefinitionTrait
{
public function tags(): array
{
return [
['id' => 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],
];
}
}
118 changes: 118 additions & 0 deletions tests/integration/notifications/NotificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/*
* This file is part of fof/follow-tags.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\FollowTags\tests\integration\notifications;

use Carbon\Carbon;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use FoF\FollowTags\tests\integration\ExtensionDepsTrait;
use FoF\FollowTags\tests\integration\TagsDefinitionTrait;

class NotificationsTest extends TestCase
{
use RetrievesAuthorizedUsers;
use ExtensionDepsTrait;
use TagsDefinitionTrait;

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

$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()],
['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' => '<t><p>Following</p></t>', 'is_private' => 0, 'is_approved' => 1, 'number' => 1],
['id' => 2, 'discussion_id' => 2, 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>Lurking</p></t>', 'is_private' => 0, 'is_approved' => 1, 'number' => 1],
],
]);
}

/**
* @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' => '<t><p>New Post</p></t>',
],
'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()
{
}
}
18 changes: 18 additions & 0 deletions tests/integration/setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of fof/follow-tags.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Flarum\Testing\integration\Setup\SetupScript;

require __DIR__.'/../../vendor/autoload.php';

$setup = new SetupScript();

$setup->run();
25 changes: 25 additions & 0 deletions tests/phpunit.integration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Flarum Integration Tests">
<directory suffix="Test.php">./integration</directory>
<exclude>./integration/tmp</exclude>
</testsuite>
</testsuites>
</phpunit>
27 changes: 27 additions & 0 deletions tests/phpunit.unit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Flarum Unit Tests">
<directory suffix="Test.php">./unit</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener" />
</listeners>
</phpunit>
Empty file added tests/unit/.gitkeep
Empty file.
Loading