Commit | Line | Data |
---|---|---|
423c9229 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
12 | namespace Civi\Test; | |
13 | ||
14 | use Civi\Core\Event\EventScanner; | |
15 | ||
16 | class EventChecker { | |
17 | ||
18 | /** | |
19 | * @var \Civi\Test\EventCheck[]|null | |
20 | */ | |
21 | private $allChecks = NULL; | |
22 | ||
23 | /** | |
24 | * @var \Civi\Test\EventCheck[]|null | |
25 | */ | |
26 | private $activeChecks = NULL; | |
27 | ||
28 | /** | |
29 | * @param \PHPUnit\Framework\Test $test | |
30 | * | |
31 | * @return $this | |
32 | */ | |
a4ecaa21 | 33 | public function start($test) { |
423c9229 TO |
34 | if ($this->activeChecks === NULL) { |
35 | $this->activeChecks = []; | |
36 | foreach ($this->findAll() as $template) { | |
37 | /** @var EventCheck $template */ | |
38 | if ($template->isSupported($test)) { | |
39 | $checker = clone $template; | |
40 | $checker->setTest($test); | |
41 | $this->activeChecks[] = $checker; | |
42 | $checker->setUp(); | |
43 | } | |
44 | } | |
45 | } | |
46 | return $this; | |
47 | } | |
48 | ||
49 | /** | |
50 | * @return $this | |
51 | */ | |
52 | public function addListeners() { | |
53 | $d = \Civi::dispatcher(); | |
54 | foreach ($this->activeChecks ?: [] as $checker) { | |
55 | /** @var EventCheck $checker */ | |
56 | $d->addListenerMap($checker, EventScanner::findListeners($checker)); | |
57 | // For the moment, KISS. But we may want a counter at some point - to ensure things actually run. | |
58 | //foreach (EventScanner::findListeners($checker) as $event => $listeners) { | |
59 | // foreach ($listeners as $listener) { | |
60 | // $d->addListener($event, | |
61 | // function($args...) use ($listener) { | |
62 | // $count++; | |
63 | // $m = $listener[1]; | |
64 | // $checker->$m(...$args); | |
65 | // }, | |
66 | // $listener[1] ?? 0 | |
67 | // ); | |
68 | // } | |
69 | //} | |
70 | } | |
71 | return $this; | |
72 | } | |
73 | ||
74 | /** | |
75 | * @return $this | |
76 | */ | |
77 | public function stop() { | |
78 | // NOTE: In test environment, dispatcher will be removed regardless. | |
79 | foreach ($this->activeChecks ?? [] as $checker) { | |
80 | /** @var \Civi\Test\EventCheck $checker */ | |
81 | Invasive::call([$checker, 'tearDown']); | |
82 | $checker->setTest(NULL); | |
83 | } | |
84 | $this->activeChecks = NULL; | |
85 | return $this; | |
86 | } | |
87 | ||
88 | /** | |
89 | * @return EventCheck[] | |
90 | */ | |
91 | protected function findAll() { | |
92 | if ($this->allChecks === NULL) { | |
93 | $all = []; | |
94 | $testDir = \Civi::paths()->getPath('[civicrm.root]/tests/events'); | |
95 | $files = \CRM_Utils_File::findFiles($testDir, '*.evch.php', TRUE); | |
96 | sort($files); | |
97 | foreach ($files as $file) { | |
98 | $all[$file] = require $testDir . '/' . $file; | |
99 | } | |
100 | $this->allChecks = $all; | |
101 | } | |
102 | ||
103 | return $this->allChecks; | |
104 | } | |
105 | ||
106 | } |