Skip to content

Commit 7d3bc59

Browse files
authored
Merge pull request #52 from launchdarkly/eb/ch74871/dont-send-events
ensure events aren't sent if send_events is false
2 parents 8393c67 + 364dff2 commit 7d3bc59

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/LaunchDarkly/LDClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace LaunchDarkly;
33

44
use LaunchDarkly\Impl\EventFactory;
5-
use LaunchDarkly\Impl\NullEventPublisher;
5+
use LaunchDarkly\Impl\NullEventProcessor;
66
use LaunchDarkly\Integrations\Guzzle;
77
use Monolog\Handler\ErrorLogHandler;
88
use Monolog\Logger;
@@ -129,7 +129,7 @@ public function __construct($sdkKey, $options = array())
129129
}
130130
$this->_eventProcessor = $ep;
131131
} elseif ($this->_offline || !$this->_send_events) {
132-
$this->_eventProcessor = new EventProcessor($sdkKey, $options);
132+
$this->_eventProcessor = new NullEventProcessor($sdkKey, $options);
133133
} else {
134134
$this->_eventProcessor = new EventProcessor($sdkKey, $options);
135135
}

tests/LDClientTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,22 @@ public function testTrackSendsEventWithDataAndMetricValue()
593593
$this->assertEquals($metricValue, $event['metricValue']);
594594
}
595595

596+
public function testEventsAreNotPublishedIfSendEventsIsFalse()
597+
{
598+
// In order to do this test, we cannot provide a mock object for Event_Processor_,
599+
// because if we do that, it won't bother even looking at the send_events flag.
600+
// Instead, we need to just put in a mock Event_Publisher_, so that the default
601+
// EventProcessor would forward events to it if send_events were not disabled.
602+
$mockPublisher = new MockEventPublisher("", array());
603+
$options = array(
604+
'feature_requester_class' => MockFeatureRequester::class,
605+
'event_publisher' => $mockPublisher
606+
);
607+
$client = new LDClient("someKey", $options);
608+
$client->track('eventkey', new LDUser('userkey'));
609+
$this->assertEquals(array(), $mockPublisher->payloads);
610+
}
611+
596612
public function testOnlyValidFeatureRequester()
597613
{
598614
$this->setExpectedException(InvalidArgumentException::class);

tests/MockEventPublisher.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace LaunchDarkly\Tests;
3+
4+
class MockEventPublisher implements \LaunchDarkly\EventPublisher
5+
{
6+
public $payloads = array();
7+
8+
public function __construct($sdkKey, array $options)
9+
{
10+
}
11+
12+
public function publish($payload)
13+
{
14+
$this->payloads[] = $payload;
15+
}
16+
}

0 commit comments

Comments
 (0)