Skip to content

Commit 5710ea3

Browse files
committed
Initial upload
1 parent 982cb75 commit 5710ea3

File tree

6 files changed

+211
-9
lines changed

6 files changed

+211
-9
lines changed

composer.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
"require": {
2222
"php": "^7.3 || ^8.0",
2323
"codeigniter4/authentication-implementation": "^1.0",
24-
"codeigniter4/settings": "^1.0"
24+
"codeigniter4/settings": "^2.0"
2525
},
2626
"require-dev": {
2727
"codeigniter4/codeigniter4": "dev-develop",
28-
"lonnieezell/codigniter-shield": "dev-develop",
28+
"tatter/imposter": "^1.0",
2929
"tatter/tools": "^1.15"
3030
},
3131
"autoload": {
@@ -45,10 +45,6 @@
4545
{
4646
"type": "vcs",
4747
"url": "https://github.com/codeigniter4/codeigniter4"
48-
},
49-
{
50-
"type": "vcs",
51-
"url": "https://github.com/lonnieezell/codigniter-shield"
5248
}
5349
],
5450
"minimum-stability": "dev",

phpstan.neon.dist

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ parameters:
1010
- src/Config/Routes.php
1111
- src/Views/*
1212
ignoreErrors:
13-
- '#Call to an undefined static method Config\\Services::[A-Za-z]+\(\)#'
14-
- '#Cannot access property [\$a-z_]+ on (array|object)#'
15-
- '#Unsafe usage of new static\(\)*#'
1613
universalObjectCratesClasses:
1714
- CodeIgniter\Entity
1815
- CodeIgniter\Entity\Entity
1916
- Faker\Generator
2017
scanDirectories:
2118
- vendor/codeigniter4/codeigniter4/system/Helpers
19+
- vendor/tatter/imposter/src/Helpers
2220
dynamicConstantNames:
2321
- APP_NAMESPACE
2422
- CI_DEBUG

src/Helpers/preferences_helper.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
if (! function_exists('preference')) {
4+
/**
5+
* Provides a wrapper for user contextual Settings.
6+
*
7+
* @param mixed|null $value
8+
*
9+
* @throws RuntimeException if attempting to set a preference witohut an active user
10+
*
11+
* @return mixed
12+
*/
13+
function preference(string $key, $value = null)
14+
{
15+
/** @var \CodeIgniter\Settings\Settings $setting */
16+
$setting = service('settings');
17+
18+
// Check for an active user
19+
if (null === $userId = user_id()) {
20+
// Allow anonymous gets
21+
if (count(func_get_args()) === 1) {
22+
return $setting->get($key);
23+
}
24+
25+
throw new RuntimeException('You cannot set a preference without an active user.');
26+
}
27+
28+
// Getting the contextual value
29+
$context = 'user:' . $userId;
30+
if (count(func_get_args()) === 1) {
31+
return $setting->get($key, $context);
32+
}
33+
34+
// Setting the contextual value
35+
return $setting->set($key, $value, $context);
36+
}
37+
}

tests/HelperTest.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
use CodeIgniter\I18n\Time;
4+
use CodeIgniter\Test\DatabaseTestTrait;
5+
use Tests\Support\TestCase;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class HelperTest extends TestCase
11+
{
12+
use DatabaseTestTrait;
13+
14+
/*
15+
function preference(string $key, $value = null)
16+
{
17+
$setting = service('settings');
18+
19+
// Check for an active user
20+
if (null === $userId = user_id()) {
21+
// Allow anonymous gets
22+
if (count(func_get_args()) === 1) {
23+
return $setting->get($key);
24+
}
25+
26+
throw new RuntimeException('You cannot set a preference without an active user.');
27+
}
28+
29+
// Getting the contextual value
30+
$context = 'user:' . $userId;
31+
if (count(func_get_args()) === 1) {
32+
return $setting->get($key, $context);
33+
}
34+
35+
// Setting the contextual value
36+
return $setting->set($key, $value, $context);
37+
}
38+
}
39+
*/
40+
41+
public function testAnonymousGets()
42+
{
43+
$this->assertSame('Apple', preference('Food.fruit'));
44+
}
45+
46+
public function testAnonymousThrowsOnSet()
47+
{
48+
$this->expectException('RuntimeException');
49+
$this->expectExceptionMessage('You cannot set a preference without an active user.');
50+
51+
preference('Food.fruit', 'Banana');
52+
}
53+
54+
public function testSets()
55+
{
56+
service('auth')->login(1);
57+
58+
preference('Food.fruit', 'Pineapple');
59+
60+
$this->seeInDatabase($this->table, [
61+
'class' => 'Tests\Support\Config\Food',
62+
'key' => 'fruit',
63+
'value' => 'Pineapple',
64+
'type' => 'string',
65+
'context' => 'user:1',
66+
]);
67+
}
68+
69+
public function testGets()
70+
{
71+
service('auth')->login(1);
72+
73+
$this->hasInDatabase($this->table, [
74+
'class' => 'Tests\Support\Config\Food',
75+
'key' => 'fruit',
76+
'value' => 'Orange',
77+
'created_at' => Time::now()->toDateTimeString(),
78+
'updated_at' => Time::now()->toDateTimeString(),
79+
]);
80+
81+
$this->assertSame('Orange', preference('Food.fruit'));
82+
}
83+
/*
84+
public function testReturnsValueDotArray()
85+
{
86+
$this->hasInDatabase($this->table, [
87+
'class' => 'Foo',
88+
'key' => 'bar',
89+
'value' => 'baz',
90+
'type' => 'string',
91+
'created_at' => Time::now()->toDateTimeString(),
92+
'updated_at' => Time::now()->toDateTimeString(),
93+
]);
94+
95+
$this->assertSame('baz', setting('Foo.bar'));
96+
}
97+
98+
public function testSettingValueDotArray()
99+
{
100+
$this->hasInDatabase($this->table, [
101+
'class' => 'Foo',
102+
'key' => 'bar',
103+
'value' => 'baz',
104+
'type' => 'string',
105+
'created_at' => Time::now()->toDateTimeString(),
106+
'updated_at' => Time::now()->toDateTimeString(),
107+
]);
108+
109+
setting('Foo.bar', false);
110+
111+
$this->seeInDatabase($this->table, [
112+
'class' => 'Foo',
113+
'key' => 'bar',
114+
'value' => '0',
115+
'type' => 'boolean',
116+
]);
117+
118+
$this->assertFalse(setting('Foo.bar'));
119+
}
120+
*/
121+
}

tests/_support/Config/Food.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tests\Support\Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
7+
class Food extends BaseConfig
8+
{
9+
public $fruit = 'Apple';
10+
}

tests/_support/TestCase.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Tests\Support;
4+
5+
use CodeIgniter\Test\CIUnitTestCase;
6+
use Nexus\PHPUnit\Extension\Expeditable;
7+
8+
abstract class TestCase extends CIUnitTestCase
9+
{
10+
use Expeditable;
11+
12+
protected $namespace = 'CodeIgniter\Settings';
13+
protected $refresh = true;
14+
15+
/**
16+
* @var string
17+
*/
18+
protected $table;
19+
20+
public static function setUpBeforeClass(): void
21+
{
22+
parent::setUpBeforeClass();
23+
24+
helper(['auth', 'preferences']);
25+
}
26+
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->table = config('Settings')->database['table'];
32+
}
33+
34+
protected function tearDown(): void
35+
{
36+
parent::tearDown();
37+
38+
$this->resetServices();
39+
}
40+
}

0 commit comments

Comments
 (0)