Skip to content

Commit

Permalink
chore: enable basic sanity test, fix PHP7.x error (#69)
Browse files Browse the repository at this point in the history
* chore: enable basic sanity test

* Apply fixes from StyleCI

* remove phpunit cache

* add phpunit.cache.result

* bump fof/extend requirement

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot committed Nov 30, 2023
1 parent aa45b24 commit ba43934
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 7 deletions.
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/[email protected]
with:
enable_backend_testing: false
enable_backend_testing: true
enable_phpstan: true

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
28 changes: 23 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"omines/oauth2-gitlab": "^3.3.0",
"wohali/oauth2-discord-new": "^1.2.1",
"league/oauth2-linkedin": "^5.1.2",
"fof/extend": "^1.3.0"
"fof/extend": "^1.3.3"
},
"replace": {
"flarum/auth-facebook": "*",
Expand Down Expand Up @@ -79,18 +79,36 @@
},
"flarum-cli": {
"modules": {
"githubActions": true
"githubActions": true,
"backendTesting": true
}
}
},
"require-dev": {
"flarum/phpstan": "*"
"flarum/phpstan": "*",
"flarum/testing": "^1.0.0"
},
"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."
},
"autoload-dev": {
"psr-4": {
"FoF\\OAuth\\Tests\\": "tests/"
}
}
}
4 changes: 3 additions & 1 deletion src/Api/CurrentUserAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public function __invoke(CurrentUserSerializer $serializer, User $user, array $a
{
$session = $serializer->getRequest()->getAttribute('session');

$attributes['loginProvider'] = $this->cache->get(AbstractOAuthController::SESSION_OAUTH2PROVIDER.'_'.$session->getId());
if ($session !== null) {
$attributes['loginProvider'] = $this->cache->get(AbstractOAuthController::SESSION_OAUTH2PROVIDER.'_'.$session->getId());
}

return $attributes;
}
Expand Down
Empty file added tests/fixtures/.gitkeep
Empty file.
82 changes: 82 additions & 0 deletions tests/integration/api/ForumSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

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

namespace FoF\OAuth\Tests\integration\api;

use Flarum\Extend;
use Flarum\Testing\integration\TestCase;

class ForumSerializerTest extends TestCase
{
public function setUp(): void
{
$this->extension('fof-oauth');

$this->extend(
(new Extend\Csrf())->exemptRoute('login')
);
}

/**
* @test
*/
public function it_includes_providers_in_forum_attributes_for_guests()
{
$response = $this->send(
$this->request('GET', '/api')
);

$this->assertEquals(200, $response->getStatusCode());

$body = json_decode($response->getBody(), true);

$this->assertArrayHasKey('fof-oauth', $body['data']['attributes']);
}

/**
* @test
*/
public function it_does_not_include_providers_in_forum_attributes_for_logged_in_users()
{
$response = $this->send(
$this->request('GET', '/api', ['authenticatedAs' => 1])
);

$this->assertEquals(200, $response->getStatusCode());

$body = json_decode($response->getBody()->getContents(), true);

$this->assertArrayNotHasKey('fof-oauth', $body['data']['attributes']);
}

/**
* @test
*/
public function admin_panel_is_available()
{
$login = $this->send(
$this->request('POST', '/login', [
'json' => [
'identification' => 'admin',
'password' => 'password',
],
])
);

$this->assertEquals(200, $login->getStatusCode());

$response = $this->send(
$this->request('GET', '/admin', ['cookiesFrom' => $login])
);

$this->assertEquals(200, $response->getStatusCode());
}
}
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/oauth.
*
* 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.

0 comments on commit ba43934

Please sign in to comment.