From fcf3829fe3fad6902e85e60e7d0528a9139e8e89 Mon Sep 17 00:00:00 2001 From: Simon Schulte Date: Mon, 2 Dec 2024 09:12:57 +0100 Subject: [PATCH] add test workflow --- .github/workflows/test.yml | 72 ++++++++++++++++++++++++++++++++++++++ composer.json | 18 ++++++---- docker-compose.yml | 48 +++++++++++++++++++++++++ example.php | 69 ++++++++++++++++++++++++++++++++++++ 4 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 docker-compose.yml create mode 100644 example.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..80828c9 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,72 @@ +name: run tests + +on: [push] + +jobs: + test: + runs-on: ubuntu-latest + permissions: + contents: 'read' + id-token: 'write' + + steps: + - uses: actions/checkout@v4 + - name: Authenticate to Google Cloud + id: auth + uses: google-github-actions/auth@v1 + with: + token_format: access_token + workload_identity_provider: projects/949875736540/locations/global/workloadIdentityPools/external-pool/providers/github-provider + service_account: artifact-pusher@talon-artifacts.iam.gserviceaccount.com + - name: Login to GAR + uses: docker/login-action@v3 + with: + registry: europe-west3-docker.pkg.dev + username: oauth2accesstoken + password: ${{ steps.auth.outputs.access_token }} + - uses: hoverkraft-tech/compose-action@v2.0.2 + - name: Setup PHP Action + uses: shivammathur/setup-php@2.31.1 + with: + tools: composer + - name: Install dependencies + run: | + composer install --prefer-dist --no-dev + sudo apt-get install jq curl + - name: Run example + run: | + ACCOUNT_RESPONSE=$(curl -s --location "http://localhost:9000/v1/accounts" \ + --header "Content-Type: application/json" \ + --data-raw '{ + "companyName": "demo", + "email": "integrationtest@talon.one", + "password": "Password1234!" + }'); + export TALON_USER_ID=$(echo $ACCOUNT_RESPONSE | jq ".userId"); + export TALON_USER_TOKEN=$(echo $ACCOUNT_RESPONSE | jq ".token" | tr -d '"'); + USER_RESPONSE=$(curl -s --location "http://localhost:9000/v1/users/$TALON_USER_ID" \ + --header "Authorization: Bearer $TALON_USER_TOKEN"); + export TALON_ACCOUNT_ID=$(echo $USER_RESPONSE | jq ".accountId"); + echo "User with ID $TALON_USER_ID and Token $TALON_USER_TOKEN was created for application $TALON_ACCOUNT_ID"; + APPLICATION_RESPONSE=$(curl -s --location "http://localhost:9000/v1/applications" \ + --header "Content-Type: application/json" \ + --header "Authorization: Bearer $TALON_USER_TOKEN" \ + --data-raw '{ + "name": "demo", + "currency": "EUR", + "timezone": "Europe/Berlin", + "enableFlattenedCartItems": false + }'); + export TALON_APPLICATION_ID=$(echo $USER_RESPONSE | jq ".id"); + echo "Application with ID $TALON_APPLICATION_ID was created" + API_KEY_RESPONSE=$(curl -s -v --location "http://localhost:9000/v1/applications/$TALON_APPLICATION_ID/apikeys" \ + --header "Content-Type: application/json" \ + --header "Authorization: Bearer $TALON_USER_TOKEN" \ + --data-raw '{ + "title": "Application HIT KEY", + "expires": "2099-01-01T0:00:00Z" + }'); + echo "Api-Key-Response: $API_KEY_RESPONSE"; + export TALON_API_KEY=$(echo $API_KEY_RESPONSE | jq ".key" | tr -d '"'); + echo "Api-Key $TALON_API_KEY created" + php example.php; diff --git a/composer.json b/composer.json index 988ab78..e7e1750 100644 --- a/composer.json +++ b/composer.json @@ -24,17 +24,21 @@ "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "guzzlehttp/guzzle": "^7" + "guzzlehttp/guzzle": "*" }, "require-dev": { - "phpunit/phpunit": "^7.4", - "squizlabs/php_codesniffer": "~2.6", - "friendsofphp/php-cs-fixer": "~2.12" + "phpunit/phpunit": "*", + "squizlabs/php_codesniffer": "*", + "friendsofphp/php-cs-fixer": "*" }, "autoload": { - "psr-4": { "TalonOne\\Client\\" : "lib/" } + "psr-4": { + "TalonOne\\Client\\": "lib/" + } }, "autoload-dev": { - "psr-4": { "TalonOne\\Client\\" : "test/" } + "psr-4": { + "TalonOne\\Client\\": "test/" + } } -} +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5b65193 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,48 @@ +services: + api-service: + image: europe-west3-docker.pkg.dev/talon-artifacts/talon-images/talon-service:master + depends_on: + - database-service + ports: + - "9000:9000" + environment: + - TALON_DB_NAME=talon + - TALON_DB_USER=talon + - TALON_DB_PASSWORD=talon.one.9000 + - TALON_DB_HOST=database-service + - TALON_DB_PORT=5432 + - TALON_ENABLE_WEBHOOK_WORKER_POOL=false + - TZ=UTC + - RELEASE_STAGE=ci + - TALON_CH_ENABLED=false + - TALON_DISABLE_PROFILER=true + - USE_REPLICA_DB=false + command: + - /talon/talon + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:9000/v1/status/health"] + interval: 10s + timeout: 5s + retries: 10 + restart: "on-failure:10" + + database-service: + image: docker.io/bitnami/postgresql:15 + volumes: + - 'postgresql_master_data:/bitnami/postgresql' + ports: + - "5433:5432" + environment: + - POSTGRESQL_DATABASE=talon + - POSTGRESQL_USERNAME=talon + - POSTGRESQL_PASSWORD=talon.one.9000 + healthcheck: + test: ["CMD-SHELL", "pg_isready -U talon -d talon"] + interval: 10s + timeout: 5s + retries: 5 + restart: "on-failure:10" + +volumes: + postgresql_master_data: + \ No newline at end of file diff --git a/example.php b/example.php new file mode 100644 index 0000000..2e4b198 --- /dev/null +++ b/example.php @@ -0,0 +1,69 @@ +setHost('http://localhost:9000') + ->setApiKeyPrefix('Authorization', 'ApiKey-v1') + ->setApiKey('Authorization', $_ENV['TALON_API_KEY']); + +// Initiating an integration api instance with the config +$apiInstance = new \TalonOne\Client\Api\IntegrationApi( + // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. + // This is optional, `GuzzleHttp\Client` will be used as default when `null` is passed. + null, // new YouClientImplementation(), + $config +); + +$customer_session_id = 'customer_session_id_example'; // string | The unique identifier for this session +$customer_session = new \TalonOne\Client\Model\NewCustomerSessionV2([ + 'profileId' => 'example_prof_id', + 'couponCodes' => [ + 'Cool-Summer!' + ], + 'cartItems' => [ + new \TalonOne\Client\Model\CartItem([ + 'name' => 'Hawaiian Pizza', + 'sku' => 'piz-hw-001', + 'quantity' => 1, + 'price' => 5.85 + ]) + ] +]); +$body = new \TalonOne\Client\Model\IntegrationRequest([ + 'customerSession' => $customer_session, + // Optional list of requested information to be present on the response. + // See lib/Model/IntegrationRequest.php#getResponseContentAllowableValues for full list + // 'responseContent' => [ + // \TalonOne\Client\Model\IntegrationRequest::RESPONSE_CONTENT_CUSTOMER_SESSION, + // \TalonOne\Client\Model\IntegrationRequest::RESPONSE_CONTENT_COUPONS + // ] +]); + +try { + // Create/Update a customer session using `updateCustomerSessionV2` function + $integration_state = $apiInstance->updateCustomerSessionV2($customer_session_id, $body); + print_r($integration_state); + + // Parsing the returned effects list, please consult https://developers.talon.one/Integration-API/handling-effects-v2 for the full list of effects and their corresponding properties + foreach ($integration_state->getEffects() as $effect) { + if ("addLoyaltyPoints" == $effect->getEffectType()) { + // Initiating right props instance according to the effect type + $props = new \TalonOne\Client\Model\AddLoyaltyPointsEffectProps((array) $effect->getProps()); + + // Access the specific effect's properties + echo $props->getName(), ':: ', $props->getRecipientIntegrationId(), ' just earned ', $props->getValue(), ' points', PHP_EOL; + } + if ("acceptCoupon" == $effect->getEffectType()) { + // Initiating right props instance according to the effect type + $props = new \TalonOne\Client\Model\AcceptCouponEffectProps((array) $effect->getProps()); + // work with AcceptCouponEffectProps' properties + // ... + } + } +} catch (Exception $e) { + echo 'Exception when calling IntegrationApi->updateCustomerSessionV2: ', $e->getMessage(), PHP_EOL; +} + +?> \ No newline at end of file