Navigation Menu

Skip to content

Commit

Permalink
Added functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickaël Andrieu committed Oct 22, 2018
1 parent 7278baf commit 8773db9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
28 changes: 28 additions & 0 deletions classes/GamificationTools.php
Expand Up @@ -41,6 +41,34 @@ public static function parseMetaData($content)
$content = preg_replace_callback('#\{language\}(.*)\{/language\}#', create_function('$matches', 'return Context::getContext()->language->$matches[1];'), $content);
$content = preg_replace_callback('#\{country\}(.*)\{/country\}#', create_function('$matches', 'return Context::getContext()->country->$matches[1];'), $content);

return $content;
}

/**
* Retrieve Json api file, forcing gzip compression to save bandwith.
* @param string $url
* @param bool $withResponseHeaders
* @return string|bool
*/
public static function retrieveJsonApiFile($url, $withResponseHeaders = false) {
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 2);
curl_setopt($curl,CURLOPT_ENCODING , 'gzip');
curl_setopt($curl,CURLOPT_USERAGENT,'gzip');
curl_setopt($curl, CURLOPT_HEADER, $withResponseHeaders);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, $withResponseHeaders);

$content = curl_exec($curl);

curl_close($curl);


return $content;
}
}
28 changes: 1 addition & 27 deletions gamification.php
Expand Up @@ -348,7 +348,7 @@ public function getData($iso_lang = null)
$iso_currency = $this->context->currency->iso_code;
$file_name = 'data_'.strtoupper($iso_lang).'_'.strtoupper($iso_currency).'_'.strtoupper($iso_country).'.json';
$versioning = '?v='.$this->version.'&ps_version='._PS_VERSION_;
$data = $this->retrieveJsonApiFile($this->url_data.$file_name.$versioning);
$data = GamificationTools::retrieveJsonApiFile($this->url_data.$file_name.$versioning);

return (bool)file_put_contents($this->cache_data.'data_'.strtoupper($iso_lang).'_'.strtoupper($iso_currency).'_'.strtoupper($iso_country).'.json', $data);
}
Expand Down Expand Up @@ -567,30 +567,4 @@ public function isFresh($file, $timeout = 86400000)

return $now < $lastFileUpdate;
}

/**
* Retrieve Json api file, forcing gzip compression to save bandwith.
* @param $url
* @return bool|mixed
*/
private function retrieveJsonApiFile($url) {
Tools::refreshCACertFile();
$curl = curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_CAINFO, _PS_CACHE_CA_CERT_FILE_);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 2);
curl_setopt($curl,CURLOPT_ENCODING , 'gzip');
curl_setopt($curl,CURLOPT_USERAGENT,'gzip');

$content = curl_exec($curl);
curl_close($curl);

return $content;
}
}
5 changes: 4 additions & 1 deletion phpunit.xml
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd"
bootstrap="tests/autoload.php">

<testsuites>
<testsuite name="Unit tests">
<directory>tests/unit/</directory>
</testsuite>
<testsuite name="Functional tests">
<directory>tests/functional/</directory>
</testsuite>
</testsuites>
</phpunit>
14 changes: 14 additions & 0 deletions tests/functional/CallToGamificationServerTest.php
@@ -0,0 +1,14 @@
<?php

use PHPUnit\Framework\TestCase;

class CallToGamificationServerTest extends TestCase
{
public function testContentIsGzipped()
{
$workingCallUrl = "https://gamification.prestashop.com/json/data_EN_IDR_ID.json";

$response = GamificationTools::retrieveJsonApiFile($workingCallUrl, true);
self::assertContains('Content-Encoding: gzip', $response);
}
}

0 comments on commit 8773db9

Please sign in to comment.