| 1 |
<?php |
| 2 |
|
| 3 |
namespace LinnoSDK\Telemetry\Tests; |
| 4 |
|
| 5 |
use LinnoSDK\Telemetry\Client; |
| 6 |
use LinnoSDK\Telemetry\TriggerManager; |
| 7 |
use LinnoSDK\Telemetry\Drivers\DriverInterface; |
| 8 |
use Mockery; |
| 9 |
use PHPUnit\Framework\TestCase; |
| 10 |
|
| 11 |
/** |
| 12 |
* TriggerManagerTest |
| 13 |
* |
| 14 |
* Covers: |
| 15 |
* - T016 WordPress action custom-event routing (US2) |
| 16 |
* - T023 Onboarding and kui/aha trigger definitions emit canonical names (US3) |
| 17 |
*/ |
| 18 |
class TriggerManagerTest 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 makeDriver( bool $sendResult = true ): DriverInterface |
| 35 |
{ |
| 36 |
$driver = Mockery::mock( DriverInterface::class ); |
| 37 |
$driver->shouldReceive( 'setApiKey' )->zeroOrMoreTimes(); |
| 38 |
$driver->shouldReceive( 'getLastError' )->andReturn( null )->zeroOrMoreTimes(); |
| 39 |
// byDefault() makes this a fallback; explicit ->with() expectations take priority. |
| 40 |
$driver->shouldReceive( 'send' )->andReturn( $sendResult )->zeroOrMoreTimes()->byDefault(); |
| 41 |
return $driver; |
| 42 |
} |
| 43 |
|
| 44 |
private function makeClient( array $extra = [], ?DriverInterface $driver = null ): Client |
| 45 |
{ |
| 46 |
$config = array_merge( |
| 47 |
[ |
| 48 |
'pluginFile' => '/var/www/html/wp-content/plugins/my-plugin/my-plugin.php', |
| 49 |
'slug' => 'my-plugin', |
| 50 |
'pluginName' => 'My Plugin', |
| 51 |
'version' => '1.0.0', |
| 52 |
], |
| 53 |
$extra |
| 54 |
); |
| 55 |
|
| 56 |
if ( $driver !== null ) { |
| 57 |
$config['_test_driver'] = $driver; |
| 58 |
} |
| 59 |
|
| 60 |
return new Client( $config ); |
| 61 |
} |
| 62 |
|
| 63 |
// ----------------------------------------------------------------------- |
| 64 |
// T016 — Custom event trigger routing via TriggerManager::on() (US2) |
| 65 |
// ----------------------------------------------------------------------- |
| 66 |
|
| 67 |
public function testCustomTriggerRoutesEventThroughClient(): void |
| 68 |
{ |
| 69 |
$client = $this->makeClient(); |
| 70 |
|
| 71 |
// Grant consent AFTER construction |
| 72 |
update_option( 'linno_telemetry_allow_tracking', 'yes' ); |
| 73 |
|
| 74 |
// Register a custom trigger that fires on 'my_plugin_post_published' |
| 75 |
$client->triggers() |
| 76 |
->on( 'post_published', 'my_plugin_post_published' ); |
| 77 |
$client->triggers()->init(); |
| 78 |
|
| 79 |
// Fire the hook — should call client->track('post_published', []) without throwing |
| 80 |
do_action( 'my_plugin_post_published' ); |
| 81 |
|
| 82 |
$this->assertTrue( true ); |
| 83 |
} |
| 84 |
|
| 85 |
// ----------------------------------------------------------------------- |
| 86 |
// T023 — Onboarding trigger emits activation/onboarding_completed (US3) |
| 87 |
// ----------------------------------------------------------------------- |
| 88 |
|
| 89 |
public function testOnboardingTriggerEmitsCanonicalEvent(): void |
| 90 |
{ |
| 91 |
$driver = $this->makeDriver(); |
| 92 |
$client = $this->makeClient( [], $driver ); |
| 93 |
|
| 94 |
// Grant consent AFTER construction (constructor resets consent state via upgrade check) |
| 95 |
update_option( 'linno_telemetry_allow_tracking', 'yes' ); |
| 96 |
|
| 97 |
$client->define_triggers( [ |
| 98 |
'setup' => 'my_plugin_setup_complete', |
| 99 |
] ); |
| 100 |
|
| 101 |
// Simulate the hook firing |
| 102 |
do_action( 'my_plugin_setup_complete' ); |
| 103 |
|
| 104 |
// Validate the event was marked as sent with canonical key |
| 105 |
$this->assertEquals( 'yes', get_option( 'my-plugin_event_sent_onboarding_completed' ) ); |
| 106 |
} |
| 107 |
|
| 108 |
// ----------------------------------------------------------------------- |
| 109 |
// T023 — KUI / AHA trigger emits activation/aha_reached (US3) |
| 110 |
// ----------------------------------------------------------------------- |
| 111 |
|
| 112 |
public function testKuiTriggerEmitsCanonicalAhaEvent(): void |
| 113 |
{ |
| 114 |
$client = $this->makeClient(); |
| 115 |
|
| 116 |
// Grant consent AFTER construction |
| 117 |
update_option( 'linno_telemetry_allow_tracking', 'yes' ); |
| 118 |
|
| 119 |
$client->define_triggers( [ |
| 120 |
'kui' => [ |
| 121 |
'order_received' => [ |
| 122 |
'hook' => 'woocommerce_order_created', |
| 123 |
], |
| 124 |
], |
| 125 |
] ); |
| 126 |
|
| 127 |
// Trigger the hook — track_kui() is called → emits activation/aha_reached |
| 128 |
do_action( 'woocommerce_order_created' ); |
| 129 |
|
| 130 |
// Assert no exception was thrown |
| 131 |
$this->assertTrue( true ); |
| 132 |
} |
| 133 |
|
| 134 |
public function testAhaTriggerEmitsCanonicalAhaEvent(): void |
| 135 |
{ |
| 136 |
$client = $this->makeClient(); |
| 137 |
|
| 138 |
// Grant consent AFTER construction |
| 139 |
update_option( 'linno_telemetry_allow_tracking', 'yes' ); |
| 140 |
|
| 141 |
// Use 'aha' canonical key (alias for 'kui') |
| 142 |
$client->define_triggers( [ |
| 143 |
'aha' => [ |
| 144 |
'milestone_reached' => [ |
| 145 |
'hook' => 'my_plugin_milestone', |
| 146 |
], |
| 147 |
], |
| 148 |
] ); |
| 149 |
|
| 150 |
do_action( 'my_plugin_milestone' ); |
| 151 |
|
| 152 |
$this->assertTrue( true ); |
| 153 |
} |
| 154 |
} |
| 155 |
|