Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: E2E Tests

on:
push:
branches: [master]
pull_request:
branches: [master]
workflow_dispatch:
inputs:
e2e_tests_ref:
description: 'Branch or ref of sdk-e2e-tests to use'
required: false
default: 'main'

jobs:
e2e-tests:
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}
runs-on: ubuntu-latest

steps:
- name: Checkout SDK
uses: actions/checkout@v4
with:
path: sdk

- name: Checkout sdk-e2e-tests
uses: actions/checkout@v4
with:
repository: segmentio/sdk-e2e-tests
ref: ${{ inputs.e2e_tests_ref || 'main' }}
token: ${{ secrets.E2E_TESTS_TOKEN }}
path: sdk-e2e-tests

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: curl, json

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Run E2E tests
working-directory: sdk-e2e-tests
run: |
./scripts/run-tests.sh \
--sdk-dir "${{ github.workspace }}/sdk/e2e-cli" \
--cli "php ${{ github.workspace }}/sdk/e2e-cli/main.php"

- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: e2e-test-results
path: sdk-e2e-tests/test-results/
if-no-files-found: ignore
39 changes: 39 additions & 0 deletions .github/workflows/publish-e2e-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Publish E2E CLI

on:
push:
branches: [master]
paths:
- 'e2e-cli/**'
- 'lib/**'
schedule:
- cron: '0 0 1 * *'
workflow_dispatch:

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout SDK
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: curl, json

- name: Prepare artifact
run: |
mkdir -p artifact
cp e2e-cli/main.php artifact/
cp e2e-cli/e2e-config.json artifact/
cp e2e-cli/run-e2e.sh artifact/
cp -r lib artifact/lib

- name: Upload CLI artifact
uses: actions/upload-artifact@v4
with:
name: e2e-cli-php
path: artifact/
retention-days: 90
131 changes: 131 additions & 0 deletions e2e-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# analytics-php e2e-cli

A small CLI tool that drives the analytics-php SDK in end-to-end tests.

## Requirements

- PHP 7.4 or 8.x

No `composer install` is required — `main.php` uses a simple `spl_autoload_register`
to load the SDK classes directly from `../lib/`.

## Usage

```bash
php main.php --input '<json>'
```

### Input JSON format

```json
{
"writeKey": "YOUR_WRITE_KEY",
"apiHost": "https://api.segment.io",
"sequences": [
{
"delayMs": 0,
"events": [
{
"type": "track",
"event": "Test Event",
"userId": "user-1",
"properties": { "plan": "pro" }
},
{
"type": "identify",
"userId": "user-1",
"traits": { "email": "user@example.com" }
},
{
"type": "page",
"userId": "user-1",
"name": "Home",
"category": "Nav"
},
{
"type": "screen",
"userId": "user-1",
"name": "Main Screen"
},
{
"type": "alias",
"userId": "new-id",
"previousId": "old-id"
},
{
"type": "group",
"userId": "user-1",
"groupId": "group-1",
"traits": { "name": "Acme Corp" }
}
]
}
],
"config": {
"flushAt": 15,
"timeout": 10
}
}
```

| Field | Description |
|---|---|
| `writeKey` | Segment write key |
| `apiHost` | Full URL of the Segment API host (scheme is stripped; PHP SDK always uses HTTPS) |
| `sequences[].delayMs` | Milliseconds to wait before processing this sequence |
| `sequences[].events` | List of events to send |
| `config.flushAt` | Number of events to accumulate before auto-flushing (maps to `flush_at`) |
| `config.timeout` | cURL timeout in seconds (maps to `curl_timeout`) |

### Supported event types

`track`, `identify`, `page`, `screen`, `alias`, `group`

### Output JSON (stdout)

On success:

```json
{"success": true, "sentBatches": 1}
```

On failure:

```json
{"success": false, "sentBatches": 0, "error": "HTTP 400: Bad Request"}
```

Exit code is `0` on success and `1` on failure. Debug logs are written to **stderr**.

## Running E2E tests

```bash
./run-e2e.sh
```

By default this expects the `sdk-e2e-tests` repo to be checked out alongside
the `analytics-php` repo:

```
parent/
analytics-php/
e2e-cli/ ← you are here
sdk-e2e-tests/
```

Override the location with an environment variable:

```bash
E2E_TESTS_DIR=/path/to/sdk-e2e-tests ./run-e2e.sh
```

Any additional arguments are forwarded to `sdk-e2e-tests/scripts/run-tests.sh`.

## How it works

1. `main.php` parses `--input` JSON from `$argv`.
2. A `Segment\Client` is created with `lib_curl` consumer and the provided options.
3. Each sequence is processed in order; `delayMs` introduces an optional pause.
4. After all events are enqueued, `flush()` is called to send them synchronously.
5. Errors captured by the `error_handler` callback or a `false` return from
`flush()` cause the script to exit with code `1`.
13 changes: 13 additions & 0 deletions e2e-cli/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "segment/analytics-php-e2e-cli",
"description": "E2E testing CLI for Segment Analytics PHP",
"type": "project",
"require": {
"php": "^7.4 || ^8.0"
},
"autoload": {
"psr-4": {
"Segment\\": "../lib/"
}
}
}
7 changes: 7 additions & 0 deletions e2e-cli/e2e-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": "php",
"test_suites": "basic",
"auto_settings": false,
"patch": null,
"env": {}
}
Loading
Loading