Skip to content

Commit c2a5cde

Browse files
committed
Tweak Events testing
1 parent ac5f13c commit c2a5cde

2 files changed

Lines changed: 32 additions & 23 deletions

File tree

system/Events/Events.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\Events;
1+
<?php
2+
namespace CodeIgniter\Events;
23

34
use Config\Services;
45

@@ -112,12 +113,10 @@ public static function initialize()
112113

113114
foreach (static::$files as $file)
114115
{
115-
if (! is_file($file))
116+
if (is_file($file))
116117
{
117-
continue;
118+
include $file;
118119
}
119-
120-
include $file;
121120
}
122121

123122
static::$initialized = true;
@@ -183,9 +182,7 @@ public static function trigger($eventName, ...$arguments): bool
183182
{
184183
$start = microtime(true);
185184

186-
$result = static::$simulate === false
187-
? $listener(...$arguments)
188-
: true;
185+
$result = static::$simulate === false ? $listener(...$arguments) : true;
189186

190187
if (CI_DEBUG)
191188
{
@@ -345,5 +342,4 @@ public static function getPerformanceLogs()
345342
}
346343

347344
//--------------------------------------------------------------------
348-
349345
}

tests/system/Events/EventsTest.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<?php namespace CodeIgniter\Events;
1+
<?php
2+
namespace CodeIgniter\Events;
23

34
use CodeIgniter\Config\Config;
45
use Tests\Support\Events\MockEvents;
56

67
class EventsTest extends \CIUnitTestCase
78
{
9+
810
/**
911
* Accessible event manager instance
1012
*/
@@ -16,17 +18,21 @@ protected function setUp()
1618

1719
$this->manager = new MockEvents();
1820

19-
$config = config('Modules');
20-
$config->activeExplorers = [];
21-
Config::injectMock('Modules', $config);
22-
2321
Events::removeAllListeners();
2422
}
2523

2624
//--------------------------------------------------------------------
2725

26+
/**
27+
* @runInSeparateProcess
28+
* @preserveGlobalState disabled
29+
*/
2830
public function testInitialize()
2931
{
32+
$config = config('Modules');
33+
$config->activeExplorers = [];
34+
Config::injectMock('Modules', $config);
35+
3036
// it should start out empty
3137
$default = [APPPATH . 'Config/Events.php'];
3238
$this->manager->setFiles([]);
@@ -40,19 +46,26 @@ public function testInitialize()
4046
// but we should be able to change it through the backdoor
4147
$this->manager::setFiles(['/peanuts']);
4248
$this->assertEquals(['/peanuts'], $this->manager->getFiles());
49+
50+
// re-initializing should have no effect
51+
Events::initialize();
52+
$this->assertEquals(['/peanuts'], $this->manager->getFiles());
4353
}
4454

4555
//--------------------------------------------------------------------
4656

47-
// Not working currently - might want to revisit at some point.
48-
// public function testPerformance()
49-
// {
50-
// $logged = Events::getPerformanceLogs();
51-
// // there should be a few event activities logged
52-
// $this->assertGreaterThan(0,count($logged));
53-
//
54-
// // might want additional tests after some activity, or to inspect what has happened so far
55-
// }
57+
public function testPerformance()
58+
{
59+
$result = null;
60+
Events::on('foo', function ($arg) use (&$result) {
61+
$result = $arg;
62+
});
63+
Events::trigger('foo', 'bar');
64+
65+
$logged = Events::getPerformanceLogs();
66+
// there should be some event activity logged
67+
$this->assertGreaterThan(0, count($logged));
68+
}
5669

5770
//--------------------------------------------------------------------
5871

0 commit comments

Comments
 (0)