Skip to content

Commit fffde53

Browse files
Boot a fresh Connectors instance when the AI Client dependency appears.
load_connectors() runs once, so the registration test can no longer reload the shared registry after the event-dispatcher stub is loaded. A new instance is how a request boots with that class already present. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 732d754 commit fffde53

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎tests/phpunit/connectors/test-class-connector-ai-client.php‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,34 @@ public function setUp(): void {
105105
/**
106106
* Registration is gated on WP_AI_Client_Event_Dispatcher, not the SDK class.
107107
*
108-
* Asserts the absent branch first when the class is missing, then loads the
109-
* stub so the present branch is covered on WP 6.x CI. The require_once leaks
110-
* the stub into the rest of this PHPUnit process; no other test asserts the
111-
* connector is absent.
108+
* Asserts the absent branch against the boot-time registry when the class is
109+
* missing. load_connectors() runs once per Connectors instance, so the present
110+
* branch is a new instance created after the stub is loaded — the same sequence
111+
* as a request that boots with the dispatcher already available. The require_once
112+
* leaks the stub into the rest of this PHPUnit process; no other test asserts
113+
* the connector is absent.
112114
*/
113115
public function test_connector_registration_is_gated_on_event_dispatcher() {
114116
$connector = new Connector_AI_Client();
115117

116118
if ( ! class_exists( 'WP_AI_Client_Event_Dispatcher' ) ) {
117119
$this->assertFalse( $connector->is_dependency_satisfied() );
118-
119-
$this->plugin->connectors->unload_connectors();
120-
$this->plugin->connectors->load_connectors();
121120
$this->assertArrayNotHasKey( 'ai-client', $this->plugin->connectors->connectors );
122121

123122
require_once __DIR__ . '/stubs/class-wp-ai-client-event-dispatcher.php';
124123
}
125124

126125
$this->assertTrue( $connector->is_dependency_satisfied() );
127126

128-
$this->plugin->connectors->unload_connectors();
129-
$this->plugin->connectors->load_connectors();
130-
$this->assertArrayHasKey( 'ai-client', $this->plugin->connectors->connectors );
127+
// A second load_connectors() on the shared instance is a no-op. Boot a
128+
// fresh registry now that the dependency exists, then detach its hooks
129+
// so later tests keep the mock registered in setUp().
130+
$connectors = new Connectors( $this->plugin );
131+
try {
132+
$this->assertArrayHasKey( 'ai-client', $connectors->connectors );
133+
} finally {
134+
$connectors->unload_connectors();
135+
}
131136
}
132137

133138
/**

0 commit comments

Comments
 (0)