Skip to content

Commit

Permalink
test: add E2E tests to ensure everything is working
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshamg1304 committed Jul 2, 2024
1 parent b465646 commit 577843a
Show file tree
Hide file tree
Showing 6 changed files with 634 additions and 2 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"require": {
"ramsey/uuid": "^4.2",
"guzzlehttp/guzzle": "^7.8",
"lastguest/murmurhash": "^2.1.1"
"lastguest/murmurhash": "^2.1.1",
"vwo/vwo-fme-sdk-e2e-test-settings-n-cases": "^1.2.4"
},
"autoload": {
"psr-4": {
Expand All @@ -27,6 +28,10 @@
}
},
"scripts": {
"start": ["cp -r ./git-hooks/* ./.git/hooks/ && chmod -R +x ./.git/hooks;"]
"start": ["cp -r ./git-hooks/* ./.git/hooks/ && chmod -R +x ./.git/hooks;"],
"test": ["./vendor/bin/phpunit"]
},
"require-dev": {
"phpunit/phpunit": "^11.2"
}
}
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<phpunit stopOnFailure="false" colors="true">
<testsuites>
<testsuite name="Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
Empty file added tests/bootstrap.php
Empty file.
135 changes: 135 additions & 0 deletions tests/e2e/GetFlagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/**
* Copyright 2024 Wingify Software Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace vwo;

use PHPUnit\Framework\TestCase;

class GetFlagTest extends TestCase
{
protected $testsData;
protected $getFlagTests;

protected function setUp(): void
{
// Initialize data
$data = SettingsAndTestCases::get();
$this->testsData = $data;
$this->getFlagTests = $data['GETFLAG_TESTS'];
}

public function testGetFlagWithoutStorage()
{
$this->runTests($this->getFlagTests['GETFLAG_WITHOUT_STORAGE']);
}

public function testGetFlagWithMEGRandomAlgo()
{
$this->runTests($this->getFlagTests['GETFLAG_MEG_RANDOM']);
}

public function testGetFlagWithMEGAdvanceAlgo()
{
$this->runTests($this->getFlagTests['GETFLAG_MEG_ADVANCE']);
}

public function testGetFlagWithStorage()
{
$this->runTests($this->getFlagTests['GETFLAG_WITH_STORAGE'], new TestStorageService());
}

protected function runTests($tests, $storageMap = null)
{
foreach ($tests as $testData) {
$this->runSingleTest($testData, $storageMap);
}
}

protected function runSingleTest($testData, $storageMap = null)
{
$vwoOptions = [
'accountId' => '123456',
'sdkKey' => 'abcdef',
];

if ($storageMap !== null) {
$vwoOptions['storage'] = $storageMap;
}

$vwoBuilder = new VWOBuilder($vwoOptions);
$vwoBuilder->setLogger();
$settingsFile = $this->testsData[$testData['settings']];
$vwoBuilder->setSettings($settingsFile);

$options = [
'sdkKey' => 'sdk-key',
'accountId' => 'account-id',
'vwoBuilder' => $vwoBuilder, // pass only for E2E tests
];

$vwoClient = VWO::init($options);

if ($storageMap !== null) {
$storageData = $storageMap->get($testData['featureKey'], $testData['context']['id']);
$this->assertNull($storageData['rolloutKey']);
$this->assertNull($storageData['rolloutVariationId']);
$this->assertNull($storageData['experimentKey']);
$this->assertNull($storageData['experimentVariationId']);
}

$featureFlag = $vwoClient->getFlag($testData['featureKey'], $testData['context']);

$this->assertEquals($testData['expectation']['isEnabled'], $featureFlag->isEnabled());
$this->assertEquals($testData['expectation']['intVariable'], $featureFlag->getVariable('int', 1));
$this->assertEquals($testData['expectation']['stringVariable'], $featureFlag->getVariable('string', 'VWO'));
$this->assertEquals($testData['expectation']['floatVariable'], $featureFlag->getVariable('float', 1.1));
$this->assertEquals($testData['expectation']['booleanVariable'], $featureFlag->getVariable('boolean', false));
$this->assertEquals($testData['expectation']['jsonVariable'], json_decode(json_encode($featureFlag->getVariable('json', [])), true));

if ($storageMap !== null) {
$storageData = $storageMap->get($testData['featureKey'], $testData['context']['id']);
$this->assertEquals($testData['expectation']['storageData']['rolloutKey'], $storageData['rolloutKey']);
$this->assertEquals($testData['expectation']['storageData']['rolloutVariationId'], $storageData['rolloutVariationId']);
$this->assertEquals($testData['expectation']['storageData']['experimentKey'], $storageData['experimentKey']);
$this->assertEquals($testData['expectation']['storageData']['experimentVariationId'], $storageData['experimentVariationId']);
}
}
}
class TestStorageService {
private $map = [];

public function get($featureKey, $userId) {
$key = $featureKey . '_' . $userId;
//echo 'Stored data: ' . $key . "\n";
return isset($this->map[$key]) ? $this->map[$key] : null;
}

public function set($data) {
$key = $data['featureKey'] . '_' . $data['user'];
//echo 'Data to store: ' . json_encode($data) . "\n";

$this->map[$key] = [
'rolloutKey' => $data['rolloutKey'],
'rolloutVariationId' => $data['rolloutVariationId'],
'experimentKey' => $data['experimentKey'],
'experimentVariationId' => $data['experimentVariationId']
];
//dump("data in set", $this->map[$key]);
return true;
}
}
128 changes: 128 additions & 0 deletions tests/e2e/TrackEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

/**
* Copyright 2024 Wingify Software Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace vwo;

use PHPUnit\Framework\TestCase;
use vwo\VWOBuilder;
use vwo\VWO;
use vwo\SettingsAndTestCases;

class TrackEventTest extends TestCase
{
protected $options;

protected function setUp(): void
{
$vwoBuilder = new VWOBuilder([
'accountId' => '123456',
'sdkKey' => 'abcdef'
]);
$vwoBuilder->setLogger();
$vwoBuilder->setSettings(SettingsAndTestCases::get()['BASIC_ROLLOUT_SETTINGS']);

$this->options = [
'sdkKey' => 'sdk-key',
'accountId' => 'account-id',
'vwoBuilder' => $vwoBuilder, // pass only for E2E tests
];
}


public function testTrackEventSuccessfully()
{
$vwoClient = VWO::init($this->options);

// Mock input data
$eventName = 'custom1';
$eventProperties = ['key' => 'value'];
$context = ['id' => '123'];

// Call the trackEvent method
$result = $vwoClient->trackEvent($eventName, $context, $eventProperties);

// Assert that the method resolves with the correct data
$this->assertEquals([$eventName => true], $result);
}

public function testTrackEventWithoutMetric()
{
$vwoClient = VWO::init($this->options);

// Mock input data
$eventName = 'testEvent';
$eventProperties = ['key' => 'value'];
$context = ['id' => '123'];

// Call the trackEvent method
$result = $vwoClient->trackEvent($eventName, $context, $eventProperties);

// Assert that the method resolves with the correct data
$this->assertEquals([$eventName => false], $result);
}

public function testTrackEventWithInvalidEventName()
{
$vwoClient = VWO::init($this->options);

// Mock input data with invalid eventName
$eventName = 123; // Invalid eventName
$eventProperties = ['key' => 'value'];
$context = ['id' => '123'];

// Call the trackEvent method
$result = $vwoClient->trackEvent($eventName, $context, $eventProperties);

// Assert that the method resolves with the correct data
$this->assertEquals([$eventName => false], $result);
}

public function testTrackEventWithInvalidEventProperties()
{
$vwoClient = VWO::init($this->options);

// Mock input data with invalid eventProperties
$eventName = 'testEvent';
$eventProperties = 'invalid'; // Invalid eventProperties
$context = ['id' => '123'];

// Call the trackEvent method
$result = $vwoClient->trackEvent($eventName, $context, $eventProperties);

// Assert that the method resolves with the correct data
$this->assertEquals([$eventName => false], $result);
}

public function testTrackEventWithInvalidContext()
{
$vwoClient = VWO::init($this->options);

// Mock input data with invalid context
$eventName = 'testEvent';
$eventProperties = ['key' => 'value'];
$context = []; // Invalid context without userId

// Call the trackEvent method
$result = $vwoClient->trackEvent($eventName, $context, $eventProperties);

// Assert that the method resolves with the correct data
$this->assertEquals([$eventName => false], $result);
}
}

?>
Loading

0 comments on commit 577843a

Please sign in to comment.