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

Updated composer, travis and tests #10

Open
wants to merge 9 commits into
base: develop
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
16 changes: 14 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ php:
- hhvm
- nightly

matrix:
fast_finish: true
allow_failures:
- php: nightly

before_script:
- composer install
- composer install --dev
- mkdir -p build/logs
- chmod +x bin/phpunit
- chmod +x bin/coveralls

script:
- bin/phpunit --coverage-clover build/logs/clover.xml

script: phpunit --coverage-text
after_success:
- travis_retry bin/coveralls -v
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ examples in your browser: http://localhost/phpbrainz/examples/browse.php
?>
```

**With Guzzle3**
**With Guzzle3 (deprecated)**

```php
<?php
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
}
],
"require-dev": {
"guzzlehttp/guzzle": "~5.3",
"phpunit/phpunit": "^5.3"
"guzzlehttp/guzzle": "^3.0|^5.3|^6.0",
"phpunit/phpunit": "^5.3",
"satooshi/php-coveralls": "^1.0"
},
"replace": {
"mikealmond/musicbrainz" : "~0.2"
Expand All @@ -34,5 +35,8 @@
"psr-0": {
"MusicBrainz": "src/"
}
},
"config": {
"bin-dir": "bin"
}
}
19 changes: 9 additions & 10 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@

<testsuites>
<testsuite name="MusicBrainz Test Suite">
<directory>./tests/MusicBrainz/</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./src/</directory>
</whitelist>
<blacklist>
<directory>./tests/</directory>
<directory>./vendor/</directory>
</blacklist>
</filter>

<logging>
<log
type="coverage-html"
target="./tests/MusicBrainz/Coverage"
charset="UTF-8"
yui="true"
lowUpperBound="35"
highLowerBound="70"
showUncoveredFiles="true"
/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

<php>
<ini name="display_errors" value="true"/>
</php>

</phpunit>
14 changes: 14 additions & 0 deletions src/MusicBrainz/Filters/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,18 @@ public function createParameters(array $params = array())

return $params;
}

protected function toArray($object)
{
if(is_object($object)) {
return get_object_vars($object);
}

if(is_array($object)) {
return $object;
}

throw new \Exception('unable to convert to array');
}

}
2 changes: 2 additions & 0 deletions src/MusicBrainz/Filters/ArtistFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ public function parseResponse(array $response, MusicBrainz $brainz)
$artists = array();
if (isset($response['artist'])) {
foreach ($response['artist'] as $artist) {
$artist = $this->toArray($artist);
$artists[] = new Artist($artist, $brainz);
}
} elseif (isset($response['artists'])) {
foreach ($response['artists'] as $artist) {
$artist = $this->toArray($artist);
$artists[] = new Artist($artist, $brainz);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/MusicBrainz/Filters/ReleaseGroupFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function parseResponse(array $response, MusicBrainz $brainz)

$releaseGroups = array();
foreach ($response['release-groups'] as $releaseGroup) {
$releaseGroup = $this->toArray($releaseGroup);
$releaseGroups[] = new ReleaseGroup($releaseGroup, $brainz);
}

Expand Down
14 changes: 12 additions & 2 deletions src/MusicBrainz/HttpAdapters/GuzzleFiveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,21 @@ public function call($path, array $params = array(), array $options = array(), $
}
}

$request = $this->client->createRequest('GET', $this->endpoint . '/' . $path, $requestOptions);
$response = $this->client->request('GET', $this->endpoint . '/' . $path, $requestOptions);

// musicbrainz throttle
sleep(1);

return $this->client->send($request)->json();
$responseBody = $response->getBody()->getContents();
$jsonResponse = \GuzzleHttp\json_decode($responseBody);

if(json_last_error() === JSON_ERROR_NONE) {
if($returnArray) {
return get_object_vars($jsonResponse);
}
return $jsonResponse;
}

throw new Exception('Expected JSON response. Got:' . $responseBody );
}
}
80 changes: 80 additions & 0 deletions src/MusicBrainz/HttpAdapters/GuzzleSixAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace MusicBrainz\HttpAdapters;

use Guzzle\Http\ClientInterface;
use MusicBrainz\Exception;

class GuzzleSixAdapter extends AbstractHttpAdapter
{
/**
* @var ClientInterface
*/
private $client;

/**
* Initialize class
*
* @param ClientInterface $client
* @param null $endpoint
*/
public function __construct(ClientInterface $client, $endpoint = NULL)
{
$this->client = $client;

if(filter_var($endpoint, FILTER_VALIDATE_URL)) {
$this->endpoint = $endpoint;
}
}

/**
* Perform an HTTP request on MusicBrainz
*
* @param string $path
* @param array $params
* @param array $options
* @param bool $isAuthRequired
* @param bool $returnArray
*
* @return array|bool|float|int|string
*
* @throws Exception
*/
public function call(
$path,
array $params = array(),
array $options = array(),
$isAuthRequired = false,
$returnArray = false
) {
if ($options['user-agent'] == '') {
throw new Exception('You must set a valid User Agent before accessing the MusicBrainz API');
}

$requestOptions = [
'headers' => [
'Accept' => 'application/json',
'User-Agent' => $options['user-agent']
],
'query' => $params
];

if ($isAuthRequired) {
if ($options['user'] != NULL && $options['password'] != NULL) {
$requestOptions['auth'] = [
'username' => $options['user'],
'password' => $options['password'],
CURLAUTH_DIGEST
];
} else {
throw new Exception('Authentication is required');
}
}

$request = $this->client->createRequest('GET', $this->endpoint . '/' . $path, $requestOptions);

// musicbrainz throttle
sleep(1);

return $this->client->send($request)->json();
}
}
8 changes: 4 additions & 4 deletions src/MusicBrainz/MusicBrainz.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MusicBrainz
/**
* @var array
*/
private static $validIncludes = array(
public static $validIncludes = array(
'artist' => array(
"recordings",
"releases",
Expand Down Expand Up @@ -187,7 +187,7 @@ class MusicBrainz
/**
* @var array
*/
private static $validBrowseIncludes = array(
public static $validBrowseIncludes = array(
'release' => array(
"artist-credits",
"labels",
Expand Down Expand Up @@ -235,7 +235,7 @@ class MusicBrainz
/**
* @var array
*/
private static $validReleaseTypes = array(
public static $validReleaseTypes = array(
"nat",
"album",
"single",
Expand All @@ -252,7 +252,7 @@ class MusicBrainz
/**
* @var array
*/
private static $validReleaseStatuses = array(
public static $validReleaseStatuses = array(
"official",
"promotion",
"bootleg",
Expand Down
Loading