Skip to content

Commit e12ed37

Browse files
authored
Add integration test for curl event publisher (#77)
1 parent a239522 commit e12ed37

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

.circleci/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ jobs:
1919
docker-image:
2020
type: string
2121

22+
environment:
23+
LD_INCLUDE_INTEGRATION_TESTS: 1
24+
2225
docker:
2326
- image: <<parameters.docker-image>>
27+
- image: wiremock/wiremock
2428

2529
steps:
30+
- setup_remote_docker
2631
- checkout
2732
- run:
2833
name: validate composer.json

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ To run all unit tests:
3333
```
3434

3535
It is preferable to run tests against all supported minor versions of PHP (as described in `README.md` under Requirements), or at least the lowest and highest versions, prior to submitting a pull request. However, LaunchDarkly's CI tests will run automatically against all supported versions.
36+
37+
By default, this test suite does not include any integration test that relies on external dependencies. To include them, set the environment variable `LD_INCLUDE_INTEGRATION_TESTS=1` before running the tests. Note that you will also need an instance of [WireMock](http://wiremock.org/) for the tests to connect to. This can be run via docker as follows:
38+
39+
```
40+
docker run --rm -p 8080:8080 wiremock/wiremock
41+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"friendsofphp/php-cs-fixer": ">=2.2.19 <3.0",
2323
"guzzlehttp/guzzle": "^7",
2424
"kevinrob/guzzle-cache-middleware": "^3",
25-
"phpunit/phpunit": "^9",
2625
"phpunit/php-code-coverage": "^9",
26+
"phpunit/phpunit": "^9",
2727
"vimeo/psalm": "4.9.2"
2828
},
2929
"suggest": {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace LaunchDarkly\Tests\Impl\Integrations;
4+
5+
use GuzzleHttp\Client;
6+
use LaunchDarkly\EventPublisher;
7+
use LaunchDarkly\Impl\Integrations;
8+
use LaunchDarkly\LDClient;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class CurlEventPublisherTest extends TestCase
12+
{
13+
public function setUp(): void
14+
{
15+
if (!getenv("LD_INCLUDE_INTEGRATION_TESTS")) {
16+
$this->markTestSkipped("Skipping integration test");
17+
}
18+
19+
$client = new Client();
20+
$client->request('DELETE', 'http://localhost:8080/__admin/requests');
21+
}
22+
23+
public function testSendsCorrectBodyAndHeaders()
24+
{
25+
$event = json_encode(["key" => "user-key"]);
26+
$publisher = new Integrations\CurlEventPublisher('sdk-key', ['events_uri' => 'http://localhost:8080']);
27+
$publisher->publish($event);
28+
29+
$requests = [];
30+
$client = new Client();
31+
32+
// Provide time for the curl to execute
33+
$start = time();
34+
while (time() - $start < 5) {
35+
$response = $client->request('GET', 'http://localhost:8080/__admin/requests');
36+
$body = json_decode($response->getBody()->getContents(), true);
37+
$requests = $body['requests'];
38+
39+
if ($requests) {
40+
break;
41+
}
42+
usleep(100);
43+
}
44+
45+
if (!$requests) {
46+
$this->fail("Unable to connect to endpoint within specified timeout");
47+
}
48+
49+
$this->assertCount(1, $requests);
50+
51+
$request = $requests[0]['request'];
52+
53+
// Validate that we hit the right endpoint with the right data
54+
$this->assertEquals($event, $request['body']);
55+
$this->assertEquals('/bulk', $request['url']);
56+
57+
// And validate that we provided all the correct headers
58+
$headers = $request['headers'];
59+
$this->assertEquals('application/json', $headers['Content-Type']);
60+
$this->assertEquals('application/json', $headers['Accept']);
61+
$this->assertEquals('sdk-key', $headers['Authorization']);
62+
$this->assertEquals('PHPClient/' . LDClient::VERSION, $headers['User-Agent']);
63+
$this->assertEquals(EventPublisher::CURRENT_SCHEMA_VERSION, $headers['X-LaunchDarkly-Event-Schema']);
64+
}
65+
}

0 commit comments

Comments
 (0)