This repository has been archived by the owner on Mar 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from AntoineAugusti/unit-tests
Add unit tests
- Loading branch information
Showing
14 changed files
with
353 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
* text=auto | ||
|
||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/.travis.yml export-ignore | ||
/LICENSE.md export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/README.md export-ignore | ||
/tests export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.DS_Store | ||
/vendor | ||
composer.phar | ||
build | ||
composer.lock | ||
.DS_Store | ||
composer.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
language: php | ||
|
||
php: | ||
- 5.4 | ||
- 5.5 | ||
|
||
before_script: | ||
- curl -s http://getcomposer.org/installer | php | ||
- php composer.phar install --dev | ||
|
||
script: phpunit | ||
language: php | ||
|
||
sudo: false | ||
|
||
matrix: | ||
include: | ||
- php: 5.4 | ||
- php: 5.5 | ||
- php: 5.6 | ||
env: COLLECT_COVERAGE=true | ||
- php: 7 | ||
- php: hhvm | ||
|
||
install: | ||
- travis_retry composer install --no-interaction --prefer-source | ||
|
||
script: | ||
- if [[ "$COLLECT_COVERAGE" == "true" ]]; then vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover; else vendor/bin/phpunit --no-coverage; fi | ||
|
||
after_script: | ||
- if [[ "$COLLECT_COVERAGE" == "true" ]]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,9 @@ Thomaswelton\LaravelGravatar\LaravelGravatarServiceProvider | |
|
||
Alias the Gravatar facade by adding it to the aliases array in the `config/app.php` file. | ||
```php | ||
'aliases' => array( | ||
'aliases' => [ | ||
'Gravatar' => 'Thomaswelton\LaravelGravatar\Facades\Gravatar' | ||
) | ||
] | ||
``` | ||
|
||
## Configuration - Optional | ||
|
@@ -35,15 +35,15 @@ php artisan vendor:publish | |
Update the config file to specify the default avatar size to use and a default image to be return if no Gravatar is found. | ||
|
||
Allowed defaults: | ||
- (bool) `false` | ||
- (string) `404` | ||
- (string) `<custom URL>`: the URL to an image of your choice (publicly available, with an image extension and without a query string). | ||
- (string) `404`: do not load any image if none is associated with the email hash, instead return an HTTP 404. | ||
- (string) `mm`: (mystery-man) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash). | ||
- (string) `identicon`: a geometric pattern based on an email hash. | ||
- (string) `monsterid`: a generated 'monster' with different colors, faces, etc. | ||
- (string) `wavatar`: generated faces with differing features and backgrounds. | ||
- (string) `retro`: awesome generated, 8-bit arcade-style pixelated faces. | ||
|
||
Example images can be viewed on [the Gravatar website](https://gravatar.com/site/implement/images/). | ||
Example images can be viewed on [the Gravatar website](https://gravatar.com/site/implement/images/#default-image). | ||
|
||
### Content Ratings | ||
|
||
|
@@ -55,7 +55,6 @@ By default only "G" rated images will be shown. You can change this system wide | |
|
||
The content rating can be changed by changing the `$rating` argument when calling `Gravatar::src` or `Gravatar::image`. | ||
|
||
|
||
## Usage | ||
|
||
### Gravatar::exists($email) | ||
|
@@ -77,7 +76,7 @@ Can optionally pass in the size required as an integer. The size will be contain | |
<img src="{{ Gravatar::src('[email protected]', 1024) }}" width=1024> | ||
``` | ||
|
||
### Gravatar::image($email, $alt = null, $attributes = array(), $rating = null) | ||
### Gravatar::image($email, $alt = null, $attributes = [], $rating = null) | ||
|
||
Returns the HTML for an `<img>` tag | ||
|
||
|
@@ -86,8 +85,8 @@ Returns the HTML for an `<img>` tag | |
echo Gravatar::image('[email protected]'); | ||
|
||
// Show image at 200px | ||
echo Gravatar::image('[email protected]', 'Some picture', array('width' => 200, 'height' => 200)); | ||
echo Gravatar::image('[email protected]', 'Some picture', ['width' => 200, 'height' => 200]); | ||
|
||
// Show image at 512px scaled in HTML to 1024px | ||
echo Gravatar::image('[email protected]', 'Some picture', array('width' => 1024, 'height' => 1024)); | ||
echo Gravatar::image('[email protected]', 'Some picture', ['width' => 1024, 'height' => 1024]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit colors="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false"> | ||
<testsuites> | ||
<testsuite name="all"> | ||
<directory suffix="Test.php">tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="tap" target="build/report.tap"/> | ||
<log type="junit" target="build/report.junit.xml"/> | ||
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/> | ||
<log type="coverage-text" target="build/coverage.txt"/> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace Thomaswelton\LaravelGravatar; | ||
|
||
use Illuminate\Contracts\Config\Repository as Config; | ||
use thomaswelton\GravatarLib\Gravatar as GravatarLib; | ||
|
||
class Gravatar extends GravatarLib | ||
{ | ||
/** | ||
* The maximum size allowed for the Gravatar. | ||
*/ | ||
const MAX_SIZE = 512; | ||
|
||
/** | ||
* The default size of the Gravatar. | ||
* | ||
* @var int | ||
*/ | ||
private $defaultSize = null; | ||
|
||
public function __construct(Config $config) | ||
{ | ||
// Set default configuration values | ||
$this->setDefaultImage($config->get('gravatar.default')); | ||
$this->defaultSize = $config->get('gravatar.size'); | ||
$this->setMaxRating($config->get('gravatar.maxRating', 'g')); | ||
|
||
$this->enableSecureImages(); | ||
} | ||
|
||
/** | ||
* Return the URL of a Gravatar. Note: it does not check for the existence of this Gravatar. | ||
* | ||
* @param string $email The email address. | ||
* @param int $size Override the size of the Gravatar. | ||
* @param null|string $rating Override the default rating if you want to. | ||
* | ||
* @return string The URL of the Gravatar. | ||
*/ | ||
public function src($email, $size = null, $rating = null) | ||
{ | ||
if (is_null($size)) { | ||
$size = $this->defaultSize; | ||
} | ||
|
||
$size = max(1, min(self::MAX_SIZE, $size)); | ||
|
||
$this->setAvatarSize($size); | ||
|
||
if (!is_null($rating)) { | ||
$this->setMaxRating($rating); | ||
} | ||
|
||
return $this->buildGravatarURL($email); | ||
} | ||
|
||
/** | ||
* Return the code of HTML image for a Gravatar. | ||
* | ||
* @param string $email The email address. | ||
* @param string $alt The alt attribute for the image. | ||
* @param array $attributes Override the 'height' and the 'width' of the image if you want. | ||
* @param null|string $rating Override the default rating if you want to. | ||
* | ||
* @return string The code of the HTML image. | ||
*/ | ||
public function image($email, $alt = null, $attributes = [], $rating = null) | ||
{ | ||
$dimensions = []; | ||
|
||
if (array_key_exists('width', $attributes)) { | ||
$dimensions[] = $attributes['width']; | ||
} | ||
if (array_key_exists('height', $attributes)) { | ||
$dimensions[] = $attributes['height']; | ||
} | ||
|
||
if (count($dimensions) > 0) { | ||
$size = min(self::MAX_SIZE, max($dimensions)); | ||
} else { | ||
$size = $this->defaultSize; | ||
} | ||
|
||
$src = $this->src($email, $size, $rating); | ||
|
||
if (!array_key_exists('width', $attributes) && !array_key_exists('height', $attributes)) { | ||
$attributes['width'] = $this->size; | ||
$attributes['height'] = $this->size; | ||
} | ||
|
||
return $this->formatImage($src, $alt, $attributes); | ||
} | ||
|
||
/** | ||
* Check if a Gravatar image exists. | ||
* | ||
* @param string $email The email address. | ||
* | ||
* @return bool True if the Gravatar exists, false otherwise. | ||
*/ | ||
public function exists($email) | ||
{ | ||
$this->setDefaultImage('404'); | ||
|
||
$url = $this->buildGravatarURL($email); | ||
$headers = get_headers($url, 1); | ||
|
||
return substr($headers[0], 9, 3) == '200'; | ||
} | ||
|
||
/** | ||
* Get the HTML image code. | ||
* | ||
* @param string $src The source attribute of the image. | ||
* @param string $alt The alt attribute of the image. | ||
* @param array $attributes Used to set the width and the height. | ||
* | ||
* @return string The HTML code. | ||
*/ | ||
private function formatImage($src, $alt, $attributes) | ||
{ | ||
return sprintf('<img src="%s" alt="%s" height="%s" width="%s">', $src, $alt, $attributes['height'], $attributes['width']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.