Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #20 from AntoineAugusti/unit-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
AntoineAugusti committed Oct 13, 2015
2 parents c688c58 + f0633b7 commit e8a0275
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 132 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
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
5 changes: 3 additions & 2 deletions .gitignore
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
32 changes: 21 additions & 11 deletions .travis.yml
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
4 changes: 2 additions & 2 deletions LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)
# The MIT License (MIT)

Copyright (c) 2014 Thomas Welton
Copyright (c) 2014 - current year Thomas Welton

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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]);
```
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
"illuminate/support":"~5.0",
"thomaswelton/gravatarlib":"0.1.x"
},
"require-dev":{
"phpunit/phpunit":"4.8.*",
"mockery/mockery":"0.9.*"
},
"autoload":{
"psr-0":{
"Thomaswelton\\LaravelGravatar":"src/"
"psr-4":{
"Thomaswelton\\LaravelGravatar\\":"src/",
"Thomaswelton\\Tests\\LaravelGravatar\\":"tests/"
}
},
"minimum-stability":"stable"
}
}
18 changes: 0 additions & 18 deletions phpunit.xml

This file was deleted.

20 changes: 20 additions & 0 deletions phpunit.xml.dist
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>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Thomaswelton\LaravelGravatar\Facades;
<?php

namespace Thomaswelton\LaravelGravatar\Facades;

use Illuminate\Support\Facades\Facade;

Expand All @@ -9,6 +11,8 @@ class Gravatar extends Facade
*
* @return string
*/
protected static function getFacadeAccessor() { return 'gravatar'; }

protected static function getFacadeAccessor()
{
return 'gravatar';
}
}
125 changes: 125 additions & 0 deletions src/Gravatar.php
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']);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php namespace Thomaswelton\LaravelGravatar;
<?php

namespace Thomaswelton\LaravelGravatar;

use Illuminate\Support\ServiceProvider;

class LaravelGravatarServiceProvider extends ServiceProvider
{
/**
* Boot the service provider.
*
* @return void
*/
public function boot()
{
Expand All @@ -16,8 +16,6 @@ public function boot()

/**
* Setup the config.
*
* @return void
*/
protected function setupConfig()
{
Expand All @@ -28,13 +26,10 @@ protected function setupConfig()

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['gravatar'] = $this->app->share(function($app)
{
$this->app['gravatar'] = $this->app->share(function ($app) {
return new Gravatar($this->app['config']);
});
}
Expand Down
Loading

0 comments on commit e8a0275

Please sign in to comment.