Skip to content

Commit d60d542

Browse files
authored
Merge pull request #10 from splitio/datetime_fix_and_test_revamp
Datetime fix and test revamp
2 parents c8b968d + a39ad79 commit d60d542

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1107
-190
lines changed

.github/workflows/ci.yml

+13-29
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
name: ci
2+
23
on:
34
pull_request:
45
branches:
56
- main
6-
- dev
77
push:
88
branches:
99
- main
10-
- dev
1110

1211
concurrency:
13-
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
12+
group: ${{ github.workflow }}-${{ github.event_name == 'push' && github.run_number || github.event.pull_request.number }}
1413
cancel-in-progress: true
1514

1615
jobs:
@@ -57,29 +56,14 @@ jobs:
5756
- name: Set VERSION env
5857
run: echo "VERSION=$(cat src/Version.php | grep 'const CURRENT' | cut -d "'" -f 2 | sed "s/'//g")" >> $GITHUB_ENV
5958

60-
# - name: SonarQube Scan (Push)
61-
# if: matrix.version == '8.2' && github.event_name == 'push'
62-
# uses: SonarSource/[email protected]
63-
# env:
64-
# SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
65-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66-
# with:
67-
# projectBaseDir: .
68-
# args: >
69-
# -Dsonar.host.url=${{ secrets.SONARQUBE_HOST }}
70-
# -Dsonar.projectVersion=${{ env.VERSION }}
71-
#
72-
# - name: SonarQube Scan (Pull Request)
73-
# if: matrix.version == '8.2' && github.event_name == 'pull_request'
74-
# uses: SonarSource/[email protected]
75-
# env:
76-
# SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
77-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78-
# with:
79-
# projectBaseDir: .
80-
# args: >
81-
# -Dsonar.host.url=${{ secrets.SONARQUBE_HOST }}
82-
# -Dsonar.projectVersion=${{ env.VERSION }}
83-
# -Dsonar.pullrequest.key=${{ github.event.pull_request.number }}
84-
# -Dsonar.pullrequest.branch=${{ github.event.pull_request.head.ref }}
85-
# -Dsonar.pullrequest.base=${{ github.event.pull_request.base.ref }}
59+
- name: SonarQube Scan
60+
if: matrix.version == '8.2'
61+
uses: SonarSource/[email protected]
62+
env:
63+
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
64+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
65+
with:
66+
projectBaseDir: .
67+
args: >
68+
-Dsonar.host.url=${{ secrets.SONARQUBE_HOST }}
69+
-Dsonar.projectVersion=${{ env.VERSION }}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
vendor
22
.php-version
33
.phpunit.result.cache
4+
.phpunit.cache
5+
coverage.xml
6+
coverage.html/

CHANGES

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1.1.0 (Sep 6, 2023):
2+
====================
3+
- Fix issue with datetime attributes on php7
4+
- Remove unit tests from autoload
5+
- Add support for .track()
6+
- Internal: Bump test coverage, CI work, build system work.

Makefile

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
PHP ?= php
22
COMPOSER ?= composer
33
PHPUNIT ?= vendor/bin/phpunit
4+
COVERAGE_FILE ?= coverage.xml
5+
COVERAGE_HTML_PATH ?= coverage.html
46

5-
default: help
7+
SOURCES := $(shell find src -iname "*.php")
68

7-
.phony: test help
9+
.phony: help cleandeps deps test test-args display-coverage
810

9-
deps:
10-
$(COMPOSER) update
11+
default: help
1112

12-
$(PHPUNIT): deps
13+
## Remove all downloaded dependencies & lock file
14+
cleandeps:
15+
rm -Rf vendor/ composer.lock
16+
17+
## Install/Update dependencies
18+
deps: composer.lock vendor/
1319

14-
## Update window sizes for send/receive buffers used in sockets API (required for UTs)
15-
seqpacket-pre-test:
20+
## Update window sizes for send/receive buffers used in sockets API for using SEQPACKET with large payloads (Linux only)
21+
sock-buf-size-enhance:
1622
sudo echo '8000000' > /proc/sys/net/core/wmem_max
1723
sudo echo '8000000' > /proc/sys/net/core/rmem_max
1824

1925
## Run all tests (args optional)
2026
test:
21-
$(PHPUNIT) -c phpunit.xml --testsuite all $(ARGS)
27+
XDEBUG_MODE=coverage $(PHPUNIT) -c phpunit.xml -v --coverage-clover $(COVERAGE_FILE) $(ARGS)
2228

23-
## Run phpunit with specified args
24-
test-args:
25-
$(PHPUNIT) $(ARGS)
29+
## Display a human readable coverage report after (re)generating it if required.
30+
display-coverage: $(COVERAGE_HTML_PATH)
31+
open $(COVERAGE_HTML_PATH)/index.html
32+
33+
$(COVERAGE_HTML_PATH): $(SOURCES)
34+
XDEBUG_MODE=coverage $(PHPUNIT) -c phpunit.xml -v --coverage-html $(COVERAGE_HTML_PATH)/
35+
36+
composer.lock vendor/: composer.json
37+
$(COMPOSER) update
38+
39+
$(PHPUNIT): deps
2640

2741
# Help target borrowed from: https://docs.cloudposse.com/reference/best-practices/make-best-practices/
2842
## This help screen

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"type": "library",
1818
"autoload": {
1919
"psr-4": {
20-
"SplitIO\\ThinSdk\\": "src/",
20+
"SplitIO\\ThinSdk\\": "src/"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
2125
"SplitIO\\Test\\": "tests/"
2226
}
2327
},

examples/track.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
require_once '../vendor/autoload.php';
4+
5+
use \SplitIO\ThinSdk\Factory;
6+
7+
$factory = Factory::withConfig([
8+
'transfer' => [
9+
'address' => '../../splitd/splitd.sock',
10+
'type' => 'unix-stream',
11+
],
12+
'logging' => [
13+
'level' => \Psr\Log\LogLevel::INFO,
14+
],
15+
]);
16+
17+
$client = $factory->client();
18+
if ($client->track("key", "user", "checkin", 0.123, ['age' => 22])) {
19+
echo "event tracked successfully";
20+
} else {
21+
echo "event tracking failed.";
22+
}

phpunit.xml

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit colors="true">
2+
<phpunit colors="true"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
cacheResultFile=".phpunit.cache/test-results"
7+
executionOrder="depends,defects"
8+
beStrictAboutOutputDuringTests="true"
9+
beStrictAboutTodoAnnotatedTests="true"
10+
convertDeprecationsToExceptions="true"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
verbose="true">
14+
315
<testsuites>
4-
<testsuite name="all">
16+
<testsuite name="default">
517
<directory suffix="Test.php">tests/</directory>
618
</testsuite>
719
<testsuite name="link">
@@ -11,4 +23,11 @@
1123
<directory suffix="Test.php">tests/Link/Transfer/</directory>
1224
</testsuite>
1325
</testsuites>
26+
27+
<coverage cacheDirectory=".phpunit.cache/code-coverage"
28+
processUncoveredFiles="true">
29+
<include>
30+
<directory suffix=".php">src</directory>
31+
</include>
32+
</coverage>
1433
</phpunit>

sonar-project.properties

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sonar.projectName=php-thin-client
2+
sonar.projectKey=splitsoftware_thin-sdk
3+
sonar.sources=src
4+
sonar.tests=tests
5+
sonar.php.coverage.reportPaths=coverage.xml
6+
sonar.links.ci=https://github.com/splitio/php-thin-client
7+
sonar.links.scm=https://github.com/splitio/php-thin-client/actions

src/Client.php

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace SplitIO\ThinSdk;
44

55
use \SplitIO\ThinSdk\Utils\ImpressionListener;
6+
use \SplitIO\ThinSdk\Utils\InputValidator\InputValidator;
7+
use \SplitIO\ThinSdk\Utils\InputValidator\ValidationException;
68
use \SplitIO\ThinSdk\Models\Impression;
79
use \SplitIO\ThinSdk\Link\Consumer\Manager;
810
use \SplitIO\ThinSdk\Link\Protocol\V1\ImpressionListenerData;
@@ -14,12 +16,14 @@ class Client implements ClientInterface
1416
private /*Link\Consumer\Manager*/ $lm;
1517
private /*LoggerInterface*/ $logger;
1618
private /*?ImpressionListener*/ $impressionListener;
19+
private /*InputValidator*/ $inputValidator;
1720

1821
public function __construct(Manager $manager, LoggerInterface $logger, ?ImpressionListener $impressionListener)
1922
{
2023
$this->logger = $logger;
2124
$this->lm = $manager;
2225
$this->impressionListener = $impressionListener;
26+
$this->inputValidator = new InputValidator($logger);
2327
}
2428

2529
public function getTreatment(string $key, ?string $bucketingKey, string $feature, ?array $attributes): string
@@ -51,6 +55,19 @@ public function getTreatments(string $key, ?string $bucketingKey, array $feature
5155
}
5256
}
5357

58+
public function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool
59+
{
60+
try {
61+
$properties = $this->inputValidator->validProperties($properties);
62+
return $this->lm->track($key, $trafficType, $eventType, $value, $properties);
63+
} catch (ValidationException $exc) {
64+
$this->logger->error("error validating event properties: " . $exc->getMessage());
65+
} catch (\Exception $exc) {
66+
$this->logger->error($exc);
67+
}
68+
return false;
69+
}
70+
5471
private function handleListener(string $key, ?string $bucketingKey, string $feature, ?array $attributes, string $treatment, ?ImpressionListenerData $ilData)
5572
{
5673
if ($this->impressionListener == null || $ilData == null) {

src/ClientInterface.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
interface ClientInterface
66
{
7-
public function getTreatment(string $key, ?string $bucketingKey, string $feature, ?array $attributes): string;
8-
public function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
7+
function getTreatment(string $key, ?string $bucketingKey, string $feature, ?array $attributes): string;
8+
function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
9+
function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool;
910
}

src/Foundation/Enum/C73.php

-19
This file was deleted.

src/Foundation/Enum/EnumValue.php

-18
This file was deleted.

src/Foundation/Lang/Enforce.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,35 @@
44

55
class Enforce
66
{
7-
static function isInt($in, ?string $msg = null): int
7+
public static function isInt($in, ?string $msg = null): int
88
{
99
if (!is_int($in)) {
1010
throw new \Exception($msg ?? ("expected an int got a " . gettype($in)));
1111
}
1212
return $in;
1313
}
1414

15-
static function isString($in, ?string $msg = null): string
15+
public static function isString($in, ?string $msg = null): string
1616
{
1717
if (!is_string($in)) {
1818
throw new \Exception($msg ?? ("expected a string got a " . gettype($in)));
1919
}
2020
return $in;
2121
}
2222

23-
static function isArray($in, ?string $msg = null): array
23+
public static function isArray($in, ?string $msg = null): array
2424
{
2525
if (!is_array($in)) {
2626
throw new \Exception($msg ?? ("expected an array got a " . gettype($in)));
2727
}
2828
return $in;
2929
}
3030

31+
public static function isBool($in, ?string $msg = null): bool
32+
{
33+
if (!is_bool($in)) {
34+
throw new \Exception($msg ?? ("expected a bool, got a " . gettype($in)));
35+
}
36+
return $in;
37+
}
3138
}
32-
33-

src/Link/Consumer/Initializer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class Initializer
1313
{
14-
static function setup(
14+
public static function setup(
1515
Version $version,
1616
Config\Transfer $transferConfig,
1717
Config\Serialization $serializationConfig,

src/Link/Consumer/Manager.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ interface Manager
66
{
77
function getTreatment(string $key, ?string $bucketingKey, string $feature, ?array $attributes): array;
88
function getTreatments(string $key, ?string $bucketingKey, array $features, ?array $attributes): array;
9+
function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool;
910
}
1011

src/Link/Consumer/V1Manager.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public function getTreatments(string $key, ?string $bucketingKey, array $feature
6464
return $results;
6565
}
6666

67+
public function track(string $key, string $trafficType, string $eventType, ?float $value, ?array $properties): bool
68+
{
69+
return Protocol\V1\TrackResponse::fromRaw(
70+
$this->rpcWithReconnect(RPC::forTrack($key, $trafficType, $eventType, $value, $properties))
71+
)->getSuccess();
72+
}
73+
74+
6775
private function register(string $id, bool $impressionFeedback)
6876
{
6977
// this is performed without retries to avoid an endless loop,

src/Link/Protocol/V1/RPC.php

+20
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ public static function forTreatments(string $key, ?string $bucketingKey, array $
7979
);
8080
}
8181

82+
public static function forTrack(
83+
string $key,
84+
string $trafficType,
85+
string $eventType,
86+
?float $value,
87+
?array $properties
88+
): RPC {
89+
return new RPC(
90+
Version::V1(),
91+
OpCode::Track(),
92+
array(
93+
TrackArgs::KEY()->getValue() => $key,
94+
TrackArgs::TRAFFIC_TYPE()->getValue() => $trafficType,
95+
TrackArgs::EVENT_TYPE()->getValue() => $eventType,
96+
TrackArgs::VALUE()->getValue() => $value,
97+
TrackArgs::PROPERTIES()->getValue() => $properties,
98+
)
99+
);
100+
}
101+
82102
function getSerializable() /* : mixed */
83103
{
84104
return array(

0 commit comments

Comments
 (0)