Skip to content

Commit a3df825

Browse files
committed
test: add test suite for analytics v2
1 parent c9ac574 commit a3df825

File tree

2 files changed

+343
-0
lines changed

2 files changed

+343
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
namespace Feature;
4+
5+
use Tests\TestCase;
6+
use Exception;
7+
8+
class AnalyticsEventsV2Test extends TestCase
9+
{
10+
private $ruleName = 'test_v2_rule';
11+
private $ruleConfiguration;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->ruleConfiguration = [
18+
"name" => $this->ruleName,
19+
"type" => "counter",
20+
"collection" => "test_products",
21+
"event_type" => "click",
22+
"rule_tag" => "test_tag",
23+
"params" => [
24+
"counter_field" => "popularity",
25+
"weight" => 1
26+
]
27+
];
28+
29+
if (!$this->isV30OrAbove()) {
30+
$this->markTestSkipped('New Analytics API is not supported in Typesense v29.0 and below');
31+
}
32+
33+
try {
34+
$this->client()->collections->create([
35+
'name' => 'test_products',
36+
'fields' => [
37+
['name' => 'company_name', 'type' => 'string'],
38+
['name' => 'num_employees', 'type' => 'int32'],
39+
['name' => 'country', 'type' => 'string', 'facet' => true],
40+
['name' => 'popularity', 'type' => 'int32', 'optional' => true]
41+
],
42+
'default_sorting_field' => 'num_employees'
43+
]);
44+
} catch (Exception $e) {
45+
}
46+
47+
try {
48+
$this->client()->analyticsV2->rules()->create([$this->ruleConfiguration]);
49+
} catch (Exception $e) {
50+
}
51+
}
52+
53+
protected function tearDown(): void
54+
{
55+
if (!$this->isV30OrAbove()) {
56+
try {
57+
$rules = $this->client()->analyticsV2->rules()->retrieve();
58+
if (is_array($rules)) {
59+
foreach ($rules as $rule) {
60+
if (strpos($rule['name'], 'test_v2_') === 0) {
61+
try {
62+
$this->client()->analyticsV2->rules()[$rule['name']]->delete();
63+
} catch (Exception $e) {
64+
}
65+
}
66+
}
67+
}
68+
} catch (Exception $e) {
69+
}
70+
71+
try {
72+
$this->client()->collections['test_products']->delete();
73+
} catch (Exception $e) {
74+
}
75+
}
76+
}
77+
78+
public function testCanCreateEventsWithV2API(): void
79+
{
80+
$event = [
81+
"name" => $this->ruleName,
82+
"event_type" => "click",
83+
"data" => [
84+
"doc_ids" => ["1", "2"],
85+
"user_id" => "test_user"
86+
]
87+
];
88+
89+
$response = $this->client()->analyticsV2->events()->create($event);
90+
$this->assertIsArray($response);
91+
}
92+
93+
public function testCanCreateMultipleEventsWithV2API(): void
94+
{
95+
$event1 = [
96+
"name" => $this->ruleName,
97+
"event_type" => "click",
98+
"data" => [
99+
"doc_id" => "1",
100+
"user_id" => "test_user_1"
101+
]
102+
];
103+
104+
$event2 = [
105+
"name" => $this->ruleName,
106+
"event_type" => "click",
107+
"data" => [
108+
"doc_id" => "2",
109+
"user_id" => "test_user_2"
110+
]
111+
];
112+
113+
$response1 = $this->client()->analyticsV2->events()->create($event1);
114+
$this->assertIsArray($response1);
115+
116+
$response2 = $this->client()->analyticsV2->events()->create($event2);
117+
$this->assertIsArray($response2);
118+
}
119+
120+
public function testCanRetrieveEventsWithV2API(): void
121+
{
122+
$event = [
123+
"name" => $this->ruleName,
124+
"event_type" => "click",
125+
"data" => [
126+
"doc_id" => "1",
127+
"user_id" => "test_user"
128+
]
129+
];
130+
131+
$this->client()->analyticsV2->events()->create($event);
132+
133+
$response = $this->client()->analyticsV2->events()->retrieve([
134+
'user_id' => 'test_user',
135+
'name' => $this->ruleName,
136+
'n'=> 10
137+
]);
138+
139+
$this->assertIsArray($response);
140+
}
141+
142+
public function testCanCreateEventWithDifferentEventTypes(): void
143+
{
144+
$clickEvent = [
145+
"name" => $this->ruleName,
146+
"event_type" => "click",
147+
"data" => [
148+
"doc_id" => "1",
149+
"user_id" => "test_user"
150+
]
151+
];
152+
153+
$conversionEvent = [
154+
"name" => $this->ruleName,
155+
"event_type" => "conversion",
156+
"data" => [
157+
"doc_id" => "1",
158+
"user_id" => "test_user"
159+
]
160+
];
161+
162+
$clickResponse = $this->client()->analyticsV2->events()->create($clickEvent);
163+
$this->assertIsArray($clickResponse);
164+
165+
$conversionResponse = $this->client()->analyticsV2->events()->create($conversionEvent);
166+
$this->assertIsArray($conversionResponse);
167+
}
168+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
namespace Feature;
4+
5+
use Tests\TestCase;
6+
use Typesense\Exceptions\RequestMalformed;
7+
use Exception;
8+
9+
class AnalyticsRulesV2Test extends TestCase
10+
{
11+
private $ruleName = 'test_v2_rule';
12+
private $ruleConfiguration;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->ruleConfiguration = [
19+
"name" => $this->ruleName,
20+
"type" => "counter",
21+
"collection" => "test_products",
22+
"event_type" => "click",
23+
"rule_tag" => "test_tag",
24+
"params" => [
25+
"counter_field" => "popularity",
26+
"weight" => 1
27+
]
28+
];
29+
30+
if (!$this->isV30OrAbove()) {
31+
$this->markTestSkipped('New Analytics API is not supported in Typesense v29.0 and below');
32+
}
33+
34+
try {
35+
$this->client()->collections->create([
36+
'name' => 'test_products',
37+
'fields' => [
38+
['name' => 'company_name', 'type' => 'string'],
39+
['name' => 'num_employees', 'type' => 'int32'],
40+
['name' => 'country', 'type' => 'string', 'facet' => true],
41+
['name' => 'popularity', 'type' => 'int32', 'optional' => true]
42+
],
43+
'default_sorting_field' => 'num_employees'
44+
]);
45+
} catch (Exception $e) {
46+
}
47+
48+
try {
49+
$this->client()->analyticsV2->rules()->create([$this->ruleConfiguration]);
50+
} catch (Exception $e) {
51+
}
52+
}
53+
54+
protected function tearDown(): void
55+
{
56+
if (!$this->isV30OrAbove()) {
57+
try {
58+
$rules = $this->client()->analyticsV2->rules()->retrieve();
59+
if (is_array($rules)) {
60+
foreach ($rules as $rule) {
61+
if (strpos($rule['name'], 'test_v2_') === 0) {
62+
try {
63+
$this->client()->analyticsV2->rules()[$rule['name']]->delete();
64+
} catch (Exception $e) {
65+
}
66+
}
67+
}
68+
}
69+
} catch (Exception $e) {
70+
}
71+
72+
try {
73+
$this->client()->collections['test_products']->delete();
74+
} catch (Exception $e) {
75+
}
76+
}
77+
}
78+
79+
public function testCanCreateRulesWithV2API(): void
80+
{
81+
$rules = [
82+
[
83+
"name" => "test_rule_1",
84+
"type" => "counter",
85+
"collection" => "test_products",
86+
"event_type" => "click",
87+
"rule_tag" => "test_tag",
88+
"params" => [
89+
"counter_field" => "popularity",
90+
"weight" => 1
91+
]
92+
],
93+
[
94+
"name" => "test_rule_2",
95+
"type" => "counter",
96+
"collection" => "test_products",
97+
"event_type" => "conversion",
98+
"rule_tag" => "test_tag",
99+
"params" => [
100+
"counter_field" => "popularity",
101+
"weight" => 2
102+
]
103+
]
104+
];
105+
106+
$response = $this->client()->analyticsV2->rules()->create($rules);
107+
$this->assertIsArray($response);
108+
109+
$allRules = $this->client()->analyticsV2->rules()->retrieve();
110+
$this->assertIsArray($allRules);
111+
112+
$ruleNames = array_column($allRules, 'name');
113+
$this->assertContains('test_rule_1', $ruleNames);
114+
$this->assertContains('test_rule_2', $ruleNames);
115+
}
116+
117+
public function testCanRetrieveARuleWithV2API(): void
118+
{
119+
$returnData = $this->client()->analyticsV2->rules()[$this->ruleName]->retrieve();
120+
$this->assertEquals($this->ruleName, $returnData['name']);
121+
$this->assertEquals('counter', $returnData['type']);
122+
$this->assertEquals('test_products', $returnData['collection']);
123+
}
124+
125+
public function testCanUpdateARuleWithV2API(): void
126+
{
127+
$updateParams = [
128+
"type" => "counter",
129+
"collection" => "test_products",
130+
"event_type" => "click",
131+
"rule_tag" => "updated_tag",
132+
"params" => [
133+
"counter_field" => "popularity",
134+
"weight" => 5
135+
]
136+
];
137+
138+
$response = $this->client()->analyticsV2->rules()[$this->ruleName]->update($updateParams);
139+
$this->assertEquals($this->ruleName, $response['name']);
140+
$this->assertEquals('updated_tag', $response['rule_tag']);
141+
$this->assertEquals(5, $response['params']['weight']);
142+
}
143+
144+
public function testCanDeleteARuleWithV2API(): void
145+
{
146+
$returnData = $this->client()->analyticsV2->rules()[$this->ruleName]->delete();
147+
$this->assertEquals($this->ruleName, $returnData['name']);
148+
149+
$this->expectException(RequestMalformed::class);
150+
$this->client()->analyticsV2->rules()[$this->ruleName]->retrieve();
151+
}
152+
153+
public function testCanRetrieveAllRulesWithV2API(): void
154+
{
155+
$returnData = $this->client()->analyticsV2->rules()->retrieve();
156+
$this->assertIsArray($returnData);
157+
$this->assertGreaterThanOrEqual(1, count($returnData));
158+
159+
$ruleNames = array_column($returnData, 'name');
160+
$this->assertContains('test_v2_rule', $ruleNames);
161+
$this->assertContains('test_rule_1', $ruleNames);
162+
$this->assertContains('test_rule_2', $ruleNames);
163+
}
164+
165+
public function testArrayAccessCompatibility(): void
166+
{
167+
$rule = $this->client()->analyticsV2->rules()[$this->ruleName];
168+
$this->assertInstanceOf('Typesense\AnalyticsRuleV2', $rule);
169+
170+
$this->assertTrue(isset($this->client()->analyticsV2->rules()[$this->ruleName]));
171+
172+
$rule = $this->client()->analyticsV2->rules()[$this->ruleName];
173+
$this->assertInstanceOf('Typesense\AnalyticsRuleV2', $rule);
174+
}
175+
}

0 commit comments

Comments
 (0)