Skip to content

Commit eadd2b9

Browse files
authored
Merge pull request #19 from blitz-php/devs
chore: Amélioration du système d'évènement
2 parents f83e773 + cdda5bd commit eadd2b9

25 files changed

Lines changed: 455 additions & 133 deletions

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CONTRIBUTING.md export-ignore
2323
tests/ export-ignore
2424
spec/ export-ignore
2525
kahlan-config.php export-ignore
26+
rector.php export-ignore
2627
phpstan-baseline.neon.dist export-ignore
2728
phpstan-baseline.php export-ignore
2829
phpstan.neon.dist export-ignore

phpstan-baseline.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
'count' => 1,
8686
'path' => __DIR__ . '/src/Debug/Toolbar.php',
8787
];
88+
$ignoreErrors[] = [
89+
// identifier: if.alwaysTrue
90+
'message' => '#^If condition is always true\\.$#',
91+
'count' => 1,
92+
'path' => __DIR__ . '/src/Event/EventManager.php',
93+
];
8894
$ignoreErrors[] = [
8995
// identifier: phpDoc.parseError
9096
'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$#',
@@ -691,12 +697,6 @@
691697
'count' => 4,
692698
'path' => __DIR__ . '/src/Router/Dispatcher.php',
693699
];
694-
$ignoreErrors[] = [
695-
// identifier: function.notFound
696-
'message' => '#^Function expect not found\\.$#',
697-
'count' => 3,
698-
'path' => __DIR__ . '/src/Spec/Mock/MockCache.php',
699-
];
700700
$ignoreErrors[] = [
701701
// identifier: class.notFound
702702
'message' => '#^Call to method directive\\(\\) on an unknown class Jenssegers\\\\Blade\\\\Blade\\.$#',
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Blitz PHP framework.
5+
*
6+
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
use BlitzPHP\Container\Services;
13+
use BlitzPHP\Contracts\Event\EventInterface;
14+
15+
use function Kahlan\expect;
16+
17+
describe('Events / Event', function (): void {
18+
beforeAll(function (): void {
19+
$this->eventManager = Services::event();
20+
$this->eventManager->clearListeners();
21+
});
22+
23+
afterEach(function (): void {
24+
$this->eventManager->clearListeners();
25+
});
26+
27+
describe('Listeners', function (): void {
28+
it('Les callbacks sont bien enregistrés', function (): void {
29+
$callback1 = static function (): void {
30+
};
31+
$callback2 = static function (): void {
32+
};
33+
34+
$this->eventManager->on('foo', $callback1);
35+
$this->eventManager->on('foo', $callback2);
36+
37+
expect($this->eventManager->getListeners('foo')[0])->toBe([$callback1, $callback2]);
38+
});
39+
40+
it('clearListeners', function (): void {
41+
$callback1 = static function (): void {
42+
};
43+
$callback2 = static function (): void {
44+
};
45+
$callback3 = static function (): void {
46+
};
47+
48+
$this->eventManager->on('foo', $callback1);
49+
$this->eventManager->on('foo', $callback3);
50+
$this->eventManager->on('bar', $callback2);
51+
$this->eventManager->on('baz', $callback2);
52+
53+
expect($this->eventManager->getListeners())->toBe([
54+
'foo' => [[$callback1, $callback3]],
55+
'bar' => [[$callback2]],
56+
'baz' => [[$callback2]],
57+
]);
58+
59+
$this->eventManager->clearListeners('foo');
60+
61+
expect($this->eventManager->getListeners())->toBe([
62+
'bar' => [[$callback2]],
63+
'baz' => [[$callback2]],
64+
]);
65+
66+
$this->eventManager->clearListeners();
67+
68+
expect($this->eventManager->getListeners())->toBe([]);
69+
});
70+
});
71+
72+
define('Execution', function(): void {
73+
it('Execute un event', function (): void {
74+
$result = null;
75+
$this->eventManager->on('foo', static function (EventInterface $event) use (&$result): void {
76+
$result = $event->getTarget();
77+
});
78+
79+
$this->eventManager->emit('foo', 'bar');
80+
81+
expect($result)->toBe('bar');
82+
});
83+
84+
it('Execute un event avec une classe callable', function (): void {
85+
$box = new class () {
86+
public string $logged;
87+
88+
public function hold($event): void
89+
{
90+
$this->logged = $event->getTarget();
91+
}
92+
};
93+
94+
$this->eventManager->on('foo', $box->hold(...));
95+
96+
$this->eventManager->emit('foo', 'bar');
97+
98+
expect($box->logged)->toBe('bar');
99+
});
100+
});
101+
102+
describe('Arret de l\'execution', function (): void {
103+
it('Arrete l\'execution des autres listeners lorsque FALSE est renvoyé', function (): void {
104+
$result = null;
105+
106+
$this->eventManager->on('foo', static function () use (&$result) {
107+
$result = 1;
108+
109+
return false;
110+
});
111+
$this->eventManager->on('foo', static function () use (&$result): void {
112+
$result = 2;
113+
});
114+
115+
$this->eventManager->emit('foo');
116+
117+
expect($result)->toBe(1);
118+
});
119+
120+
it('Arrete l\'execution des autres listeners lorsque stopPropagation est utilisé', function (): void {
121+
$result = null;
122+
123+
$this->eventManager->on('foo', static function (EventInterface $event) use (&$result): void {
124+
$result = 1;
125+
126+
$event->stopPropagation();
127+
});
128+
$this->eventManager->on('foo', static function () use (&$result): void {
129+
$result = 2;
130+
});
131+
132+
$this->eventManager->emit('foo');
133+
134+
expect($result)->toBe(1);
135+
});
136+
});
137+
138+
describe('Priorite', function (): void {
139+
it('Priorite', function (): void {
140+
$result = 0;
141+
142+
$this->eventManager->on('foo', static function () use (&$result) {
143+
$result = 1;
144+
145+
return false;
146+
}, EventInterface::PRIORITY_NORMAL);
147+
148+
// Ceci doit etre lancer en premier car elle a une priorite elevee
149+
$this->eventManager->on('foo', static function () use (&$result) {
150+
$result = 2;
151+
152+
return false;
153+
}, EventInterface::PRIORITY_HIGH);
154+
155+
$this->eventManager->emit('foo');
156+
157+
expect($result)->toBe(2);
158+
});
159+
160+
it('Priorite multiple', function (): void {
161+
$result = [];
162+
163+
$this->eventManager->on('foo', static function () use (&$result): void {
164+
$result[] = 'a';
165+
}, EventInterface::PRIORITY_NORMAL);
166+
167+
$this->eventManager->on('foo', static function () use (&$result): void {
168+
$result[] = 'b';
169+
}, EventInterface::PRIORITY_LOW);
170+
171+
$this->eventManager->on('foo', static function () use (&$result): void {
172+
$result[] = 'c';
173+
}, EventInterface::PRIORITY_HIGH);
174+
175+
$this->eventManager->on('foo', static function () use (&$result): void {
176+
$result[] = 'd';
177+
}, 75);
178+
179+
$this->eventManager->emit('foo');
180+
181+
expect($result)->toBe(['c', 'd', 'a', 'b']);
182+
});
183+
});
184+
185+
describe('Retrait de listener ', function (): void {
186+
it('Le retrait de listener fonctionne', function (): void {
187+
$result = false;
188+
189+
$callback = static function () use (&$result): void {
190+
$result = true;
191+
};
192+
193+
$this->eventManager->on('foo', $callback);
194+
195+
$this->eventManager->emit('foo');
196+
expect($result)->toBeTruthy();
197+
198+
$result = false;
199+
expect($this->eventManager->off('foo', $callback))->toBeTruthy();
200+
201+
$this->eventManager->emit('foo');
202+
expect($result)->toBeFalsy();
203+
});
204+
205+
it('Retire le listener une seule fois', function (): void {
206+
$result = false;
207+
208+
$callback = static function () use (&$result): void {
209+
$result = true;
210+
};
211+
212+
$this->eventManager->on('foo', $callback);
213+
214+
$this->eventManager->emit('foo');
215+
expect($result)->toBeTruthy();
216+
217+
$result = false;
218+
expect($this->eventManager->off('foo', $callback))->toBeTruthy();
219+
expect($this->eventManager->off('foo', $callback))->toBeFalsy();
220+
221+
$this->eventManager->emit('foo');
222+
expect($result)->toBeFalsy();
223+
});
224+
225+
it('Retrait d\'un listener inconnue', function (): void {
226+
$result = false;
227+
228+
$callback = static function () use (&$result): void {
229+
$result = true;
230+
};
231+
232+
$this->eventManager->on('foo', $callback);
233+
234+
$this->eventManager->emit('foo');
235+
expect($result)->toBeTruthy();
236+
237+
$result = false;
238+
expect($this->eventManager->off('bar', $callback))->toBeFalsy();
239+
240+
$this->eventManager->emit('foo');
241+
expect($result)->toBeTruthy();
242+
});
243+
});
244+
});

spec/system/framework/Publisher/PublisherSupport.spec.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use function Kahlan\expect;
1818

1919
describe('Publisher / PublisherSupport', function (): void {
20-
beforeAll(function () {
20+
beforeAll(function (): void {
2121
helper('filesystem');
2222

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

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

@@ -92,7 +92,7 @@
9292
expect($publisher->getErrors())->toBe($expected);
9393
});
9494

95-
it('wipeDirectory', function () {
95+
it('wipeDirectory', function (): void {
9696
$directory = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . bin2hex(random_bytes(6));
9797
mkdir($directory, 0700);
9898
expect(is_dir($directory))->toBeTruthy();
@@ -103,14 +103,14 @@
103103
expect(is_dir($directory))->toBeFalsy();
104104
});
105105

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

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

113-
it('wipe', function () {
113+
it('wipe', function (): void {
114114
$directory = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . bin2hex(random_bytes(6));
115115
mkdir($directory, 0700);
116116
$directory = realpath($directory) ?: $directory;

spec/system/framework/Router/AutoRouter.spec.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
beforeAll(function (): void {
2424
$this->collection = new RouteCollection(Services::locator(), (object) config('routing'));
2525

26-
$this->createNewAutoRouter = function($namespace = 'Spec\BlitzPHP\App\Controllers'): AutoRouter {
27-
return new AutoRouter(
26+
$this->createNewAutoRouter = fn($namespace = 'Spec\BlitzPHP\App\Controllers'): AutoRouter => new AutoRouter(
2827
[],
2928
$namespace,
3029
$this->collection->getDefaultController(),
3130
$this->collection->getDefaultMethod(),
3231
true
3332
);
34-
};
3533
});
3634

3735
it('L\'autoroute trouve le controller et la methode par defaut "get"', function (): void {

src/Cli/Commands/Generators/Component.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function execute(array $params)
7777

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

80-
$viewName = Text::toKebab(Helpers::classBasename($className));
80+
$viewName = Text::convertTo(Helpers::classBasename($className), 'kebab');
8181
$viewName = preg_replace(
8282
'/([a-z][a-z0-9_\/\\\\]+)(-component)$/i',
8383
'$1',

src/Cli/Commands/Utilities/ConfigCheck.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ class ConfigCheck extends Command
5151
*/
5252
public function execute(array $params)
5353
{
54-
if (empty($file = strtolower($this->argument('config', '')))) {
54+
$file = strtolower($this->argument('config', ''));
55+
56+
if ($file === '' || $file === '0') {
5557
$this->fail('Vous devez spécifier la configuration à utiliser pour la vérification.')->eol();
5658
$this->write(' Usage: ' . $this->usage)->eol();
5759
$this->write('Exemple: config:check app')->eol();

0 commit comments

Comments
 (0)