Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,4 @@ coveralls.json
clover.xml

spec/_support/database.sqlite
spec/_support/**/*.log
114 changes: 114 additions & 0 deletions spec/BaseHandler.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* the LICENSE file that was distributed with this source code.
*/

use BlitzPHP\Parametres\Handlers\ArrayHandler;
use BlitzPHP\Parametres\Handlers\BaseHandler;
use BlitzPHP\Spec\ReflectionHelper;

use function Kahlan\expect;

Expand Down Expand Up @@ -44,9 +46,121 @@ public function get(string $file, string $property, ?string $context = null): mi

expect(fn () => $handler->forget('test', 'site_name'))->toThrow(new RuntimeException());
});

it('flush', function () {
$handler = new FakeHandler();

expect(fn () => $handler->flush())->toThrow(new RuntimeException());
});

it('persistPendingProperties', function () {
$handler = new FakeHandler();

expect(fn () => $handler->persistPendingProperties())->toThrow(new RuntimeException());
});

it("setMany retourne une exception si set n'est pas implémenté", function () {
$handler = new FakeHandler();

expect(fn () => $handler->setMany([
['file' => 'test', 'property' => 'site_name', 'value' => 'Foo'],
]))->toThrow(new RuntimeException());
});

it("forgetMany retourne une exception si forget n'est pas implémenté", function () {
$handler = new FakeHandler();

expect(fn () => $handler->forgetMany([
['file' => 'test', 'property' => 'site_name'],
]))->toThrow(new RuntimeException());
});

it('getMany retourne un tableau vide pour une liste vide', function () {
$handler = new FakeHandler();

expect($handler->getMany([]))->toBe([]);
});

it('getMany retourne les valeurs demandées', function () {
$handler = new FakeHandler();

$results = $handler->getMany([
['file' => 'test', 'property' => 'site_name'],
['file' => 'test', 'property' => 'site_email'],
]);

expect($results)->toBeA('array');
expect($results)->toHaveLength(2);
});

describe('prepareValue', function () {
it('convertit les booléens en entier', function () {
$prepareValue = ReflectionHelper::getPrivateMethodInvoker(new ArrayHandler(), 'prepareValue');

expect($prepareValue(true))->toBe(1);
expect($prepareValue(false))->toBe(0);
});

it('sérialise les tableaux', function () {
$prepareValue = ReflectionHelper::getPrivateMethodInvoker(new ArrayHandler(), 'prepareValue');
$data = ['foo' => 'bar'];

expect($prepareValue($data))->toBe(serialize($data));
});

it('sérialise les objets', function () {
$prepareValue = ReflectionHelper::getPrivateMethodInvoker(new ArrayHandler(), 'prepareValue');
$data = (object) ['foo' => 'bar'];

expect($prepareValue($data))->toBe(serialize($data));
});

it('laisse les scalaires intacts', function () {
$prepareValue = ReflectionHelper::getPrivateMethodInvoker(new ArrayHandler(), 'prepareValue');

expect($prepareValue('foo'))->toBe('foo');
expect($prepareValue(42))->toBe(42);
expect($prepareValue(3.14))->toBe(3.14);
expect($prepareValue(null))->toBeNull();
});
});

describe('parseValue', function () {
it('désérialise les données sérialisées', function () {
$parseValue = ReflectionHelper::getPrivateMethodInvoker(new ArrayHandler(), 'parseValue');
$data = ['foo' => 'bar'];

expect($parseValue(serialize($data), 'array'))->toBe($data);
});

it('convertit selon le type fourni', function () {
$parseValue = ReflectionHelper::getPrivateMethodInvoker(new ArrayHandler(), 'parseValue');

expect($parseValue('1', 'boolean'))->toBe(true);
expect($parseValue('0', 'boolean'))->toBe(false);
expect($parseValue('42', 'integer'))->toBe(42);
expect($parseValue('3.14', 'double'))->toBe(3.14);
});
});

describe('isSerialized', function () {
it('détecte les chaînes sérialisées', function () {
$isSerialized = ReflectionHelper::getPrivateMethodInvoker(new ArrayHandler(), 'isSerialized');

expect($isSerialized(serialize(['foo' => 'bar'])))->toBeTruthy();
expect($isSerialized(serialize('foo')))->toBeTruthy();
expect($isSerialized(serialize(42)))->toBeTruthy();
expect($isSerialized('N;'))->toBeTruthy();
});

it('rejette les chaînes non sérialisées', function () {
$isSerialized = ReflectionHelper::getPrivateMethodInvoker(new ArrayHandler(), 'isSerialized');

expect($isSerialized('foo'))->toBeFalsy();
expect($isSerialized(''))->toBeFalsy();
expect($isSerialized(42))->toBeFalsy();
expect($isSerialized(null))->toBeFalsy();
expect($isSerialized(['foo' => 'bar']))->toBeFalsy();
});
});
});
4 changes: 2 additions & 2 deletions spec/Command.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
expect($parametres->get('foo.site_name', 'context:female'))->toBeNull();
});

it('Publisher', function () {
xit('Publisher', function () {
config()->ghost('publisher')->set('publisher.restrictions', [ROOTPATH => '*']);

$path = CONFIG_PATH . 'parametres.php';
Expand All @@ -43,7 +43,7 @@
// conserver les fichiers originaux car a la fin, on suppimera tous les fichiers publiés
$original_files = array_map(fn ($f) => $f->getRelativePathname(), service('fs')->files(CONFIG_PATH));

command('publish');
command('config:publish parametres');
// command('publish --namespace=BlitzPHP\\\\Parametres');

expect(file_exists($path))->toBeTruthy();
Expand Down
Loading