| 1 |
<?php |
| 2 |
|
| 3 |
namespace LinnoSDK\Telemetry\Tests; |
| 4 |
|
| 5 |
use LinnoSDK\Telemetry\EventDispatcher; |
| 6 |
use LinnoSDK\Telemetry\Drivers\DriverInterface; |
| 7 |
use Mockery; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
|
| 10 |
/** |
| 11 |
* EventDispatcherTest |
| 12 |
* |
| 13 |
* Covers: |
| 14 |
* - T007 Missing-driver warning logs (dispatch with NullDriver) |
| 15 |
* - T010 Send-failure logging |
| 16 |
* - T012 Lifecycle dispatch assertions (minimal dispatch) (US1) |
| 17 |
*/ |
| 18 |
class EventDispatcherTest extends TestCase |
| 19 |
{ |
| 20 |
protected function setUp(): void |
| 21 |
{ |
| 22 |
wp_reset_stubs(); |
| 23 |
} |
| 24 |
|
| 25 |
protected function tearDown(): void |
| 26 |
{ |
| 27 |
Mockery::close(); |
| 28 |
} |
| 29 |
|
| 30 |
// ----------------------------------------------------------------------- |
| 31 |
// Helpers |
| 32 |
// ----------------------------------------------------------------------- |
| 33 |
|
| 34 |
private function makeConfig( array $overrides = [] ): array |
| 35 |
{ |
| 36 |
return array_merge( [ |
| 37 |
'pluginName' => 'My Plugin', |
| 38 |
'version' => '1.0.0', |
| 39 |
'unique_id' => 'test-uid-123', |
| 40 |
'slug' => 'my-plugin', |
| 41 |
], $overrides ); |
| 42 |
} |
| 43 |
|
| 44 |
private function makeDriver( bool $sendResult = true, ?string $lastError = null ): DriverInterface |
| 45 |
{ |
| 46 |
$driver = Mockery::mock( DriverInterface::class ); |
| 47 |
$driver->shouldReceive( 'getLastError' )->andReturn( $lastError )->zeroOrMoreTimes(); |
| 48 |
// byDefault() makes this a fallback; explicit ->with() expectations take priority. |
| 49 |
$driver->shouldReceive( 'send' )->andReturn( $sendResult )->zeroOrMoreTimes()->byDefault(); |
| 50 |
return $driver; |
| 51 |
} |
| 52 |
|
| 53 |
// ----------------------------------------------------------------------- |
| 54 |
// T010 — Send-failure logging |
| 55 |
// ----------------------------------------------------------------------- |
| 56 |
|
| 57 |
public function testDispatchReturnsFalseOnSendFailure(): void |
| 58 |
{ |
| 59 |
$driver = $this->makeDriver( false, 'Connection refused' ); |
| 60 |
$dispatcher = new EventDispatcher( $driver, $this->makeConfig() ); |
| 61 |
|
| 62 |
$result = $dispatcher->dispatch( 'some_event', [ |
| 63 |
'site_url' => 'https://example.com', |
| 64 |
'unique_id' => 'uid', |
| 65 |
'plugin_name' => 'My Plugin', |
| 66 |
'plugin_version' => '1.0.0', |
| 67 |
'timestamp' => '2026-01-01T00:00:00+00:00', |
| 68 |
] ); |
| 69 |
|
| 70 |
$this->assertFalse( $result ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testDispatchMinimalReturnsFalseOnSendFailure(): void |
| 74 |
{ |
| 75 |
$driver = $this->makeDriver( false, 'Timeout' ); |
| 76 |
$dispatcher = new EventDispatcher( $driver, $this->makeConfig() ); |
| 77 |
|
| 78 |
$result = $dispatcher->dispatch_minimal( 'activation/plugin_activated', [ |
| 79 |
'site_url' => 'https://example.com', |
| 80 |
'unique_id' => 'uid', |
| 81 |
] ); |
| 82 |
|
| 83 |
$this->assertFalse( $result ); |
| 84 |
} |
| 85 |
|
| 86 |
// ----------------------------------------------------------------------- |
| 87 |
// T012 — Lifecycle dispatch minimal assertions (US1) |
| 88 |
// ----------------------------------------------------------------------- |
| 89 |
|
| 90 |
public function testDispatchMinimalSendsLifecycleEventName(): void |
| 91 |
{ |
| 92 |
$driver = $this->makeDriver( true ); |
| 93 |
$driver->shouldReceive( 'send' ) |
| 94 |
->with( 'activation/plugin_activated', Mockery::type( 'array' ) ) |
| 95 |
->once() |
| 96 |
->andReturn( true ); |
| 97 |
|
| 98 |
$dispatcher = new EventDispatcher( $driver, $this->makeConfig() ); |
| 99 |
$result = $dispatcher->dispatch_minimal( 'activation/plugin_activated', [ |
| 100 |
'site_url' => 'https://example.com', |
| 101 |
'unique_id' => 'uid', |
| 102 |
] ); |
| 103 |
|
| 104 |
$this->assertTrue( $result ); |
| 105 |
} |
| 106 |
|
| 107 |
public function testDispatchMinimalSendsDeactivationEventName(): void |
| 108 |
{ |
| 109 |
$driver = $this->makeDriver( true ); |
| 110 |
$driver->shouldReceive( 'send' ) |
| 111 |
->with( 'activation/plugin_deactivated', Mockery::type( 'array' ) ) |
| 112 |
->once() |
| 113 |
->andReturn( true ); |
| 114 |
|
| 115 |
$dispatcher = new EventDispatcher( $driver, $this->makeConfig() ); |
| 116 |
$result = $dispatcher->dispatch_minimal( 'activation/plugin_deactivated', [ |
| 117 |
'site_url' => 'https://example.com', |
| 118 |
'unique_id' => 'uid', |
| 119 |
'reason' => 'none', |
| 120 |
] ); |
| 121 |
|
| 122 |
$this->assertTrue( $result ); |
| 123 |
} |
| 124 |
|
| 125 |
// ----------------------------------------------------------------------- |
| 126 |
// T008 — Plugin version and name correctly handed off to dispatcher |
| 127 |
// ----------------------------------------------------------------------- |
| 128 |
|
| 129 |
public function testDispatchPopulatesPluginMetadataFromConfig(): void |
| 130 |
{ |
| 131 |
$driver = Mockery::mock( DriverInterface::class ); |
| 132 |
$driver->shouldReceive( 'send' )->andReturnUsing( |
| 133 |
function ( string $event, array $properties ) { |
| 134 |
// Assert metadata was properly populated from config |
| 135 |
\PHPUnit\Framework\Assert::assertEquals( 'My Plugin', $properties['plugin_name'] ); |
| 136 |
\PHPUnit\Framework\Assert::assertEquals( '2.3.1', $properties['plugin_version'] ); |
| 137 |
return true; |
| 138 |
} |
| 139 |
); |
| 140 |
$driver->shouldReceive( 'getLastError' )->andReturn( null ); |
| 141 |
|
| 142 |
$dispatcher = new EventDispatcher( $driver, $this->makeConfig( [ |
| 143 |
'pluginName' => 'My Plugin', |
| 144 |
'version' => '2.3.1', |
| 145 |
'unique_id' => 'uid', |
| 146 |
] ) ); |
| 147 |
|
| 148 |
$dispatcher->dispatch( 'test_event', [ |
| 149 |
'site_url' => 'https://example.com', |
| 150 |
'unique_id' => 'uid', |
| 151 |
'timestamp' => '2026-01-01T00:00:00+00:00', |
| 152 |
] ); |
| 153 |
} |
| 154 |
} |
| 155 |
|