Skip to content

Commit

Permalink
Add Settings Extender tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 1, 2020
1 parent 33066d6 commit 400b102
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 9 deletions.
9 changes: 0 additions & 9 deletions tests/integration/extenders/ApiSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ protected function prepDb()
]);
}

protected function prepSettingsDb()
{
$this->prepareDatabase([
'settings' => [
['key' => 'customPrefix.customSetting', 'value' => 'customValue']
],
]);
}

/**
* @test
*/
Expand Down
188 changes: 188 additions & 0 deletions tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Tests\integration\extenders;

use Flarum\Extend;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;

class SettingsTest extends TestCase
{
use RetrievesAuthorizedUsers;

protected function prepDb()
{
$this->prepareDatabase([
'users' => [
$this->adminUser(),
$this->normalUser()
],
'settings' => [
['key' => 'custom-prefix.custom_setting', 'value' => 'customValue'],
['key' => 'custom-prefix.custom_setting2', 'value' => 'customValue']
]
]);
}

/**
* @test
*/
public function custom_setting_doesnt_exist_by_default()
{
$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

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

$this->assertArrayNotHasKey('customPrefix.customSetting', $payload['data']['attributes']);
}

/**
* @test
*/
public function custom_setting_exists_if_added()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting')
);

$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

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

$this->assertArrayHasKey('customPrefix.customSetting', $payload['data']['attributes']);
$this->assertEquals('customValue', $payload['data']['attributes']['customPrefix.customSetting']);
}

/**
* @test
*/
public function custom_setting_falls_back_to_default_value()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.unavailableCustomSetting', 'custom-prefix.unavailable_custom_setting')
->default('custom-prefix.unavailable_custom_setting', 'default')
);

$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

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

$this->assertArrayHasKey('customPrefix.unavailableCustomSetting', $payload['data']['attributes']);
$this->assertEquals('default', $payload['data']['attributes']['customPrefix.unavailableCustomSetting']);
}

/**
* @test
*/
public function custom_setting_modifier_works_if_added()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting')
->modifier('custom-prefix.custom_setting', function ($value) {
return $value.'Modified';
})
);

$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

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

$this->assertArrayHasKey('customPrefix.customSetting', $payload['data']['attributes']);
$this->assertEquals('customValueModified', $payload['data']['attributes']['customPrefix.customSetting']);
}

/**
* @test
*/
public function custom_setting_modifier_works_with_invokable_class()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2')
->modifier('custom-prefix.custom_setting2', CustomInvokableClass::class)
);

$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

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

$this->assertArrayHasKey('customPrefix.customSetting2', $payload['data']['attributes']);
$this->assertEquals('customValueModifiedByInvokable', $payload['data']['attributes']['customPrefix.customSetting2']);
}

/**
* @test
*/
public function custom_setting_modifier_works_on_default_value()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.unavailableCustomSetting2', 'custom-prefix.unavailable_custom_setting2')
->default('custom-prefix.unavailable_custom_setting2', 'default')
->modifier('custom-prefix.unavailable_custom_setting2', function ($value) {
return $value.'Modified';
})
);

$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

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

$this->assertArrayHasKey('customPrefix.unavailableCustomSetting2', $payload['data']['attributes']);
$this->assertEquals('defaultModified', $payload['data']['attributes']['customPrefix.unavailableCustomSetting2']);
}
}

class CustomInvokableClass
{
public function __invoke($value)
{
return $value.'ModifiedByInvokable';
}
}

0 comments on commit 400b102

Please sign in to comment.