Skip to content

Commit c119672

Browse files
committed
Add some more acceptance tests.
1 parent c190595 commit c119672

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

tests/_support/AcceptanceTester.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function amOnAPageThatMakesGuzzleRequest( string $test ): void {
3636
$this->amOnPage( "/?_qm_acceptance_group=guzzle_requests&_qm_acceptance_test={$test}" );
3737
}
3838

39+
public function amOnAPageThatTriggersCallbackType( string $test ): void {
40+
$this->amOnPage( "/?_qm_acceptance_group=callback_types&_qm_acceptance_test={$test}" );
41+
}
42+
3943
public function seeQMMenuWithWarning(): void {
4044
$this->seeElement( '#wp-admin-bar-query-monitor.qm-warning' );
4145
}

tests/_support/MUPlugin/acceptance.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,57 @@
7777
break;
7878
}
7979
break;
80+
case 'callback_types':
81+
switch ( $_GET['_qm_acceptance_test'] ) {
82+
case 'function':
83+
// Regular function callback
84+
add_action( 'qm_test_hook', '__return_true' );
85+
do_action( 'qm_test_hook' );
86+
break;
87+
case 'method':
88+
// Object method callback
89+
$obj = new class {
90+
public function test_method() {
91+
return true;
92+
}
93+
};
94+
add_action( 'qm_test_hook', [ $obj, 'test_method' ] );
95+
do_action( 'qm_test_hook' );
96+
break;
97+
case 'static_method':
98+
// Static method callback - create our own test class
99+
if ( ! class_exists( 'QM_Test_Static_Class' ) ) {
100+
class QM_Test_Static_Class {
101+
public static function test_static_method() {
102+
return 'static test result';
103+
}
104+
}
105+
}
106+
add_action( 'qm_test_hook', [ 'QM_Test_Static_Class', 'test_static_method' ] );
107+
do_action( 'qm_test_hook' );
108+
break;
109+
case 'closure':
110+
// Closure callback
111+
add_action( 'qm_test_hook', function() {
112+
// Test closure for acceptance testing
113+
return 'test closure result';
114+
} );
115+
do_action( 'qm_test_hook' );
116+
break;
117+
case 'invokable':
118+
// Invokable object callback
119+
add_action( 'qm_test_hook', new class {
120+
public function __invoke() {
121+
return 'invokable result';
122+
}
123+
} );
124+
do_action( 'qm_test_hook' );
125+
break;
126+
default:
127+
throw new \InvalidArgumentException( 'Unknown test: ' . $_GET['_qm_acceptance_test'] );
128+
break;
129+
}
130+
break;
80131
default:
81132
throw new \InvalidArgumentException( 'Unknown group: ' . $_GET['_qm_acceptance_group'] );
82133
break;

tests/acceptance/CallbacksCest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Acceptance tests for callback types in Hooks and Actions panel.
4+
*/
5+
6+
class CallbacksCest {
7+
public function _before( AcceptanceTester $I ): void {
8+
$I->loginAsAdmin();
9+
}
10+
11+
public function FunctionCallbackShouldBeDisplayed( AcceptanceTester $I ): void {
12+
$I->amOnAPageThatTriggersCallbackType( 'function' );
13+
$I->seeQMMenu();
14+
$I->seeInQMPanel( 'Hooks & Actions', '__return_true()' );
15+
$I->seeInQMPanel( 'Hooks & Actions', 'qm_test_hook' );
16+
}
17+
18+
public function MethodCallbackShouldBeDisplayed( AcceptanceTester $I ): void {
19+
$I->amOnAPageThatTriggersCallbackType( 'method' );
20+
$I->seeQMMenu();
21+
$I->seeInQMPanel( 'Hooks & Actions', 'test_method()' );
22+
$I->seeInQMPanel( 'Hooks & Actions', 'qm_test_hook' );
23+
}
24+
25+
public function StaticMethodCallbackShouldBeDisplayed( AcceptanceTester $I ): void {
26+
$I->amOnAPageThatTriggersCallbackType( 'static_method' );
27+
$I->seeQMMenu();
28+
$I->seeInQMPanel( 'Hooks & Actions', 'QM_Test_Static_Class::test_static_method()' );
29+
$I->seeInQMPanel( 'Hooks & Actions', 'qm_test_hook' );
30+
}
31+
32+
public function ClosureCallbackShouldBeDisplayed( AcceptanceTester $I ): void {
33+
$I->amOnAPageThatTriggersCallbackType( 'closure' );
34+
$I->seeQMMenu();
35+
$I->seeInQMPanel( 'Hooks & Actions', 'Closure on line' );
36+
$I->seeInQMPanel( 'Hooks & Actions', 'acceptance.php' );
37+
$I->seeInQMPanel( 'Hooks & Actions', 'qm_test_hook' );
38+
}
39+
40+
public function InvokableCallbackShouldBeDisplayed( AcceptanceTester $I ): void {
41+
$I->amOnAPageThatTriggersCallbackType( 'invokable' );
42+
$I->seeQMMenu();
43+
$I->seeInQMPanel( 'Hooks & Actions', '__invoke()' );
44+
$I->seeInQMPanel( 'Hooks & Actions', 'qm_test_hook' );
45+
}
46+
}

0 commit comments

Comments
 (0)