Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit testing and using Guzzle #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[composer.json]
indent_style = space
indent_size = 4
43 changes: 43 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
language: php

php:
- 5.6
- 7.0
- nightly

sudo: false

env:
global:
- PATH="$HOME/.composer/vendor/bin:$PATH"

cache:
directories:
- $HOME/.composer/cache

matrix:
fast_finish: true
include:
- php: 5.6
env: COMPOSER_FLAGS="--prefer-lowest"
allow_failures:
- php: nightly

before_install:
- phpenv global 5.6

install:
- mkdir -p ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d && echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- mkdir -p build/logs
- composer global require satooshi/php-coveralls:@stable --no-update
- composer global update --prefer-dist --no-interaction
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS

before_script:
- composer require phpunit/phpunit

script:
- vendor/bin/phpunit

after_script:
- coveralls -v
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
}
],
"require": {
"php": ">=5.2.2",
"ext-simplexml": "*"
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.2"
},
"autoload": {
"classmap": ["src/"]
Expand Down
25 changes: 25 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
colors="true"
bootstrap="./vendor/autoload.php"
charset="UTF-8">

<testsuites>
<testsuite name="Testing Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="result/" />
</logging>

</phpunit>
35 changes: 12 additions & 23 deletions src/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* @license New BSD License
* @version 1.3
*/

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;

class Feed
{
/** @var int */
Expand Down Expand Up @@ -202,30 +207,14 @@ private static function loadXml($url, $user, $pass)
*/
private static function httpRequest($url, $user, $pass)
{
if (extension_loaded('curl')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if ($user !== null || $pass !== null) {
curl_setopt($curl, CURLOPT_USERPWD, "$user:$pass");
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // no echo, just return result
if (!ini_get('open_basedir')) {
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // sometime is useful :)
}
$result = curl_exec($curl);
return curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200
? $result
: false;

} elseif ($user === null && $pass === null) {
return file_get_contents($url);

} else {
throw new FeedException('PHP extension CURL is not loaded.');
$client = new Client();
$requestOptions['verify'] = __DIR__ . '/cacert.pem';
if($user !== NULL && $pass !== NULL) {
$requestOptions['auth'] = [$user, $pass];
}
$response = $client->request('GET', $url, $requestOptions);

return $response->getBody()->getContents();
}


Expand Down
Loading