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 .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CONTRIBUTING.md export-ignore
tests/ export-ignore
spec/ export-ignore
kahlan-config.php export-ignore
rector.php export-ignore
phpstan-baseline.neon.dist export-ignore
phpstan-baseline.php export-ignore
phpstan.neon.dist export-ignore
Expand Down
12 changes: 6 additions & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
'count' => 1,
'path' => __DIR__ . '/src/Debug/Toolbar.php',
];
$ignoreErrors[] = [
// identifier: if.alwaysTrue
'message' => '#^If condition is always true\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Event/EventManager.php',
];
$ignoreErrors[] = [
// identifier: phpDoc.parseError
'message' => '#^PHPDoc tag @method has invalid value \\(static void configure\\(callable \\$callback\\(RouteBuilder \\$route\\)\\) Configure les parametres de routing\\.\\)\\: Unexpected token "\\(", expected \'\\)\' at offset 63$#',
Expand Down Expand Up @@ -691,12 +697,6 @@
'count' => 4,
'path' => __DIR__ . '/src/Router/Dispatcher.php',
];
$ignoreErrors[] = [
// identifier: function.notFound
'message' => '#^Function expect not found\\.$#',
'count' => 3,
'path' => __DIR__ . '/src/Spec/Mock/MockCache.php',
];
$ignoreErrors[] = [
// identifier: class.notFound
'message' => '#^Call to method directive\\(\\) on an unknown class Jenssegers\\\\Blade\\\\Blade\\.$#',
Expand Down
244 changes: 244 additions & 0 deletions spec/system/framework/Events/Event.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
<?php

/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

use BlitzPHP\Container\Services;
use BlitzPHP\Contracts\Event\EventInterface;

use function Kahlan\expect;

describe('Events / Event', function (): void {
beforeAll(function (): void {
$this->eventManager = Services::event();
$this->eventManager->clearListeners();
});

afterEach(function (): void {
$this->eventManager->clearListeners();
});

describe('Listeners', function (): void {
it('Les callbacks sont bien enregistrés', function (): void {
$callback1 = static function (): void {
};
$callback2 = static function (): void {
};

$this->eventManager->on('foo', $callback1);
$this->eventManager->on('foo', $callback2);

expect($this->eventManager->getListeners('foo')[0])->toBe([$callback1, $callback2]);
});

it('clearListeners', function (): void {
$callback1 = static function (): void {
};
$callback2 = static function (): void {
};
$callback3 = static function (): void {
};

$this->eventManager->on('foo', $callback1);
$this->eventManager->on('foo', $callback3);
$this->eventManager->on('bar', $callback2);
$this->eventManager->on('baz', $callback2);

expect($this->eventManager->getListeners())->toBe([
'foo' => [[$callback1, $callback3]],
'bar' => [[$callback2]],
'baz' => [[$callback2]],
]);

$this->eventManager->clearListeners('foo');

expect($this->eventManager->getListeners())->toBe([
'bar' => [[$callback2]],
'baz' => [[$callback2]],
]);

$this->eventManager->clearListeners();

expect($this->eventManager->getListeners())->toBe([]);
});
});

define('Execution', function(): void {
it('Execute un event', function (): void {
$result = null;
$this->eventManager->on('foo', static function (EventInterface $event) use (&$result): void {
$result = $event->getTarget();
});

$this->eventManager->emit('foo', 'bar');

expect($result)->toBe('bar');
});

it('Execute un event avec une classe callable', function (): void {
$box = new class () {
public string $logged;

public function hold($event): void
{
$this->logged = $event->getTarget();
}
};

$this->eventManager->on('foo', $box->hold(...));

$this->eventManager->emit('foo', 'bar');

expect($box->logged)->toBe('bar');
});
});

describe('Arret de l\'execution', function (): void {
it('Arrete l\'execution des autres listeners lorsque FALSE est renvoyé', function (): void {
$result = null;

$this->eventManager->on('foo', static function () use (&$result) {
$result = 1;

return false;
});
$this->eventManager->on('foo', static function () use (&$result): void {
$result = 2;
});

$this->eventManager->emit('foo');

expect($result)->toBe(1);
});

it('Arrete l\'execution des autres listeners lorsque stopPropagation est utilisé', function (): void {
$result = null;

$this->eventManager->on('foo', static function (EventInterface $event) use (&$result): void {
$result = 1;

$event->stopPropagation();
});
$this->eventManager->on('foo', static function () use (&$result): void {
$result = 2;
});

$this->eventManager->emit('foo');

expect($result)->toBe(1);
});
});

describe('Priorite', function (): void {
it('Priorite', function (): void {
$result = 0;

$this->eventManager->on('foo', static function () use (&$result) {
$result = 1;

return false;
}, EventInterface::PRIORITY_NORMAL);

// Ceci doit etre lancer en premier car elle a une priorite elevee
$this->eventManager->on('foo', static function () use (&$result) {
$result = 2;

return false;
}, EventInterface::PRIORITY_HIGH);

$this->eventManager->emit('foo');

expect($result)->toBe(2);
});

it('Priorite multiple', function (): void {
$result = [];

$this->eventManager->on('foo', static function () use (&$result): void {
$result[] = 'a';
}, EventInterface::PRIORITY_NORMAL);

$this->eventManager->on('foo', static function () use (&$result): void {
$result[] = 'b';
}, EventInterface::PRIORITY_LOW);

$this->eventManager->on('foo', static function () use (&$result): void {
$result[] = 'c';
}, EventInterface::PRIORITY_HIGH);

$this->eventManager->on('foo', static function () use (&$result): void {
$result[] = 'd';
}, 75);

$this->eventManager->emit('foo');

expect($result)->toBe(['c', 'd', 'a', 'b']);
});
});

describe('Retrait de listener ', function (): void {
it('Le retrait de listener fonctionne', function (): void {
$result = false;

$callback = static function () use (&$result): void {
$result = true;
};

$this->eventManager->on('foo', $callback);

$this->eventManager->emit('foo');
expect($result)->toBeTruthy();

$result = false;
expect($this->eventManager->off('foo', $callback))->toBeTruthy();

$this->eventManager->emit('foo');
expect($result)->toBeFalsy();
});

it('Retire le listener une seule fois', function (): void {
$result = false;

$callback = static function () use (&$result): void {
$result = true;
};

$this->eventManager->on('foo', $callback);

$this->eventManager->emit('foo');
expect($result)->toBeTruthy();

$result = false;
expect($this->eventManager->off('foo', $callback))->toBeTruthy();
expect($this->eventManager->off('foo', $callback))->toBeFalsy();

$this->eventManager->emit('foo');
expect($result)->toBeFalsy();
});

it('Retrait d\'un listener inconnue', function (): void {
$result = false;

$callback = static function () use (&$result): void {
$result = true;
};

$this->eventManager->on('foo', $callback);

$this->eventManager->emit('foo');
expect($result)->toBeTruthy();

$result = false;
expect($this->eventManager->off('bar', $callback))->toBeFalsy();

$this->eventManager->emit('foo');
expect($result)->toBeTruthy();
});
});
});
10 changes: 5 additions & 5 deletions spec/system/framework/Publisher/PublisherSupport.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use function Kahlan\expect;

describe('Publisher / PublisherSupport', function (): void {
beforeAll(function () {
beforeAll(function (): void {
helper('filesystem');

$this->file = str_replace(['/', '\\'], DS, SUPPORT_PATH . 'Files/baker/banana.php');
Expand Down Expand Up @@ -79,7 +79,7 @@
expect(is_dir($scratch))->toBeFalsy();
});

it('Recuperation des erreurs', function () {
it('Recuperation des erreurs', function (): void {
$publisher = new Publisher();
expect($publisher->getErrors())->toBe([]);

Expand All @@ -92,7 +92,7 @@
expect($publisher->getErrors())->toBe($expected);
});

it('wipeDirectory', function () {
it('wipeDirectory', function (): void {
$directory = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . bin2hex(random_bytes(6));
mkdir($directory, 0700);
expect(is_dir($directory))->toBeTruthy();
Expand All @@ -103,14 +103,14 @@
expect(is_dir($directory))->toBeFalsy();
});

it('wipeIgnoresFiles', function () {
it('wipeIgnoresFiles', function (): void {
$method = ReflectionHelper::getPrivateMethodInvoker(Publisher::class, 'wipeDirectory');
$method($this->file);

expect(is_file($this->file))->toBeTruthy();
});

it('wipe', function () {
it('wipe', function (): void {
$directory = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . bin2hex(random_bytes(6));
mkdir($directory, 0700);
$directory = realpath($directory) ?: $directory;
Expand Down
4 changes: 1 addition & 3 deletions spec/system/framework/Router/AutoRouter.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
beforeAll(function (): void {
$this->collection = new RouteCollection(Services::locator(), (object) config('routing'));

$this->createNewAutoRouter = function($namespace = 'Spec\BlitzPHP\App\Controllers'): AutoRouter {
return new AutoRouter(
$this->createNewAutoRouter = fn($namespace = 'Spec\BlitzPHP\App\Controllers'): AutoRouter => new AutoRouter(
[],
$namespace,
$this->collection->getDefaultController(),
$this->collection->getDefaultMethod(),
true
);
};
});

it('L\'autoroute trouve le controller et la methode par defaut "get"', function (): void {
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Commands/Generators/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function execute(array $params)

$this->template = 'component_view.tpl.php';

$viewName = Text::toKebab(Helpers::classBasename($className));
$viewName = Text::convertTo(Helpers::classBasename($className), 'kebab');
$viewName = preg_replace(
'/([a-z][a-z0-9_\/\\\\]+)(-component)$/i',
'$1',
Expand Down
4 changes: 3 additions & 1 deletion src/Cli/Commands/Utilities/ConfigCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class ConfigCheck extends Command
*/
public function execute(array $params)
{
if (empty($file = strtolower($this->argument('config', '')))) {
$file = strtolower($this->argument('config', ''));

if ($file === '' || $file === '0') {
$this->fail('Vous devez spécifier la configuration à utiliser pour la vérification.')->eol();
$this->write(' Usage: ' . $this->usage)->eol();
$this->write('Exemple: config:check app')->eol();
Expand Down
Loading