diff --git a/docker-compose.yml b/docker-compose.yml index e84d4f58..97531a76 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,13 +1,13 @@ version: '3.5' services: - typesense: - image: typesense/typesense:26.0 - environment: - TYPESENSE_DATA_DIR: /data - TYPESENSE_API_KEY: xyz - volumes: - - /tmp/typesense-server-data:/data - ports: - - 8108:8108 - restart: "no" + typesense: + image: typesense/typesense:27.0.rc21 + environment: + TYPESENSE_DATA_DIR: /data + TYPESENSE_API_KEY: xyz + volumes: + - /tmp/typesense-server-data:/data + ports: + - 8108:8108 + restart: 'no' diff --git a/src/Analytics.php b/src/Analytics.php index 0adf4fd2..2f161dd1 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -10,6 +10,8 @@ class Analytics private AnalyticsRules $rules; + private AnalyticsEvents $events; + public function __construct(ApiCall $apiCall) { $this->apiCall = $apiCall; @@ -22,4 +24,12 @@ public function rules() } return $this->rules; } + + public function events() + { + if (!isset($this->events)) { + $this->events = new AnalyticsEvents($this->apiCall); + } + return $this->events; + } } diff --git a/src/AnalyticsEvents.php b/src/AnalyticsEvents.php new file mode 100644 index 00000000..6b7180bf --- /dev/null +++ b/src/AnalyticsEvents.php @@ -0,0 +1,47 @@ +apiCall = $apiCall; + } + + /** + * @param array $params + * + * @return array + * @throws TypesenseClientError|HttpClientException + */ + public function create($params) + { + return $this->apiCall->post($this->endpoint_path(), $params); + } + + /** + * @return string + */ + private function endpoint_path($operation = null) + { + return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation"); + } +} diff --git a/tests/Feature/AnalyticsEventsTest.php b/tests/Feature/AnalyticsEventsTest.php index 05384f5c..c1fa70d6 100644 --- a/tests/Feature/AnalyticsEventsTest.php +++ b/tests/Feature/AnalyticsEventsTest.php @@ -6,17 +6,66 @@ class AnalyticsEventsTest extends TestCase { - //* there is no method for sending events - // public function testCanCreateAnEvent(): void - // { - // $returnData = $this->client()->analytics->rules()->even - // $this->assertEquals($returnData['name'], $this->ruleName); - // } + private $ruleName = 'product_queries_aggregation'; - public function testNeedImplementationForAnalyticsEvents(): void + protected function setUp(): void { - $this->markTestIncomplete( - 'This test has not been implemented yet.', - ); + parent::setUp(); + $this->client()->collections->create([ + "name" => "products", + "fields" => [ + [ + "name" => "title", + "type" => "string" + ], + [ + "name" => "popularity", + "type" => "int32", + "optional" => true + ] + ] + ]); + $this->client()->analytics->rules()->upsert($this->ruleName, [ + "name" => "products_popularity", + "type" => "counter", + "params" => [ + "source" => [ + "collections" => [ + "products" + ], + "events" => [ + [ + "type" => "click", + "weight" => 1, + "name" => "products_click_event" + ] + ] + ], + "destination" => [ + "collection" => "products", + "counter_field" => "popularity" + ] + ] + ]); + } + + protected function tearDown(): void + { + parent::tearDown(); + $this->client()->analytics->rules()->{'product_queries_aggregation'}->delete(); + } + + public function testCanCreateAnEvent(): void + { + $response = $this->client()->analytics->events()->create([ + "type" => "click", + "name" => "products_click_event", + "data" => [ + "q" => "nike shoes", + "doc_id" => "1024", + "user_id" => "111112" + ] + ]); + $this->assertTrue($response['ok']); } } diff --git a/tests/Feature/AnalyticsRulesTest.php b/tests/Feature/AnalyticsRulesTest.php index 0d863c90..8aaecc33 100644 --- a/tests/Feature/AnalyticsRulesTest.php +++ b/tests/Feature/AnalyticsRulesTest.php @@ -7,7 +7,7 @@ class AnalyticsRulesTest extends TestCase { - private $ruleName = 'product_queries_aggregation'; + private $ruleName = 'test_rule'; private $ruleConfiguration = [ "type" => "popular_queries", "params" => [ @@ -29,6 +29,14 @@ protected function setUp(): void $this->ruleUpsertResponse = $this->client()->analytics->rules()->upsert($this->ruleName, $this->ruleConfiguration); } + protected function tearDown(): void + { + $rules = $this->client()->analytics->rules()->retrieve(); + foreach ($rules['rules'] as $rule) { + $this->client()->analytics->rules()->{$rule['name']}->delete(); + } + } + public function testCanUpsertARule(): void { $this->assertEquals($this->ruleName, $this->ruleUpsertResponse['name']);