Skip to content

Commit

Permalink
📦 changes: added tests for gravatar & doc rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
hendurhance committed Dec 29, 2023
1 parent 6e54c29 commit 8ea5595
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 19 deletions.
99 changes: 92 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@
[![Total Downloads](https://img.shields.io/packagist/dt/martian/laravatar.svg?style=flat-square)](https://packagist.org/packages/martian/laravatar)
![GitHub Actions](https://github.com/amplifiedhq/laravatar/actions/workflows/main.yml/badge.svg)

🚀 A lightweight and easy-to-use Laravel package that add avatars to your models using [Gravatar](https://gravatar.com), [DiceBear](https://www.dicebear.com/), [UI Avatars](https://ui-avatars.com/) or [Boring Avatar](https://boringavatars.com/)
🚀 A lightweight and easy-to-use Laravel package designed to simplify avatar generation for your Eloquent models. It provides a flexible and extensible solution for generating avatars using [Gravatar](https://gravatar.com), [DiceBear](https://www.dicebear.com/), [UI Avatars](https://ui-avatars.com/) or [Boring Avatar](https://boringavatars.com/)

## Supported Avatar Drivers
| Driver | Description | Supported | Link |
| --- | --- | --- | --- |
| Gravatar | Gravatar is a service for providing globally unique avatars. | Yes | [Gravatar](https://gravatar.com) |
| DiceBear | DiceBear is an avatar library for designers and developers. | Yes | [DiceBear](https://www.dicebear.com/) |
| UI Avatars | UI Avatars is an avatar library for designers and developers. | Yes | [UI Avatars](https://ui-avatars.com/) |
| Boring Avatar | Boring avatars is a tiny JavaScript React library that generates custom, SVG-based, round avatars from any username and color palette. | Yes | [Boring Avatar](https://boringavatars.com/) |
| Gravatar | Gravatar is a service for providing globally unique avatars. | Yes ✅️ | [Gravatar](https://gravatar.com) |
| DiceBear | DiceBear is an avatar library for designers and developers. | Yes ✅️ | [DiceBear](https://www.dicebear.com/) |
| UI Avatars | UI Avatars is an avatar library for designers and developers. | Yes ✅️ | [UI Avatars](https://ui-avatars.com/) |
| Boring Avatar | Boring avatars is a tiny JavaScript React library that generates custom, SVG-based, round avatars from any username and color palette. | Yes ✅️ | [Boring Avatar](https://boringavatars.com/) |

> Note: You can also add your own custom driver by implementing the `AmplifiedHQ\Laravatar\Contracts\AvatarInterface;` interface and extending the `AmplifiedHQ\Laravatar\Abstracts\BaseAvatar` class.


## Installation
> Note: This package requires PHP 7.4 or higher.
You can install the package via composer:

```bash
composer require amplifiedhq/laravatar
composer require martian/laravatar
```

## Register Service Provider
Expand Down Expand Up @@ -65,6 +66,8 @@ You can configure the package by editing the `config/laravatar.php` file.
## Usage
In order to use the package in your model to generate an avatar on the fly, you need to add the `AmplifiedHQ\Laravatar\Traits\HasAvatar` trait to your model.

### Using the `HasAvatar` trait

```php

namespace App\Models;
Expand Down Expand Up @@ -93,7 +96,89 @@ class User extends Authenticatable
> If you are using the `gravater` driver, you need to use the email column as the avatar column. If you are using the `dicebear` or `ui-avatars` or `boringavatar` driver, you can use any column as the avatar column, provided that the column is a string column. (e.g. `name`, `email`, `username` etc.)
> Note: The `HasAvatar` trait requires you to define the `$avatarColumn` and `$avatarStorageColumn` properties in your model. The `$avatarColumn` property is the column that will be used to generate the avatar. The `$avatarStorageColumn` property is the column that will be used to store the avatar.
> *WARNING*
### Using the Driver Methods
You can also use each driver method directly on your application either on on your controller, model or view.

#### Gravatar
```php

use AmplifiedHQ\Laravatar\Drivers\Gravatar;
use App\Http\Controllers\Controller;

class UserController extends Controller {

public function generateAvatar()
{
$gravatar = new Gravatar('jane@example.com');
$gravatar->setSize(100);
$gravatar->getUrl(); // https://www.gravatar.com/avatar/9e26471d35a78862c17e467d87cddedf?size=100
}
}

```

#### DiceBear
```php

use AmplifiedHQ\Laravatar\Drivers\DiceBear;
use App\Http\Controllers\Controller;

class UserController extends Controller {

public function generateAvatar()
{
$dicebear = new DiceBear('Jane Doe');
$dicebear->setStyle('lorelei');
$dicebear->setSize(100);
$dicebear->setFormat('svg');
$dicebear->getUrl(); // https://api.dicebear.com/7.x/lorelei/svg?seed=Jane%20Doe&size=100
}
}
```

#### Boring Avatar
```php

use AmplifiedHQ\Laravatar\Drivers\BoringAvatar;
use App\Http\Controllers\Controller;

class UserController extends Controller {

public function generateAvatar()
{
$boringAvatar = new BoringAvatar('Jane Doe');
$boringAvatar->setSize(100);
$boringAvatar->setFormat('svg');
$boringAvatar->setVariant('marble');
$boringAvatar->setSquare(true);
$boringAvatar->getUrl(); // https://boring-avatars-api.vercel.app/api/avatar?size=100&variant=marble&name=Jane%20Doe&sqaure=1
}
}

```

#### UI Avatars
```php

use AmplifiedHQ\Laravatar\Drivers\UiAvatars;
use App\Http\Controllers\Controller;

class UserController extends Controller {

public function generateAvatar()
{
$uiAvatars = new UiAvatars('Jane Doe');
$uiAvatars->setSize(100);
$uiAvatars->setFormat('svg');
$uiAvatars->setBackgroundColor('000000'); // Hexadecimal
$uiAvatars->setForegroundColor('ffffff'); // Hexadecimal
$uiAvatars->setRounded(true);
$uiAvatars->getUrl(); // https://ui-avatars.com/api/?name=Jane%20Doe&size=100&background=000000&color=ffffff&rounded=true
}
}

```

### Testing

Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
19 changes: 19 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace AmplifiedHQ\Laravatar\Tests;

use Orchestra\Testbench\TestCase as Orchestra;
use AmplifiedHQ\Laravatar\Providers\LaravatarServiceProvider;

class TestCase extends Orchestra
{
/**
* Load package service provider
* @param \Illuminate\Foundation\Application $app
* @return AmplifiedHQ\Laravatar\Providers\LaravatarServiceProvider
*/
protected function getPackageProviders($app)
{
return [LaravatarServiceProvider::class];
}
}
12 changes: 0 additions & 12 deletions tests/Unit.php

This file was deleted.

30 changes: 30 additions & 0 deletions tests/Unit/Drivers/GravatarDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace AmplifiedHQ\Laravatar\Tests;

use AmplifiedHQ\Laravatar\Drivers\Gravatar;
use AmplifiedHQ\Laravatar\Tests\TestCase;

uses(TestCase::class)->in('Unit');

it('should return a Gravatar URL', function () {
$gravatar = new Gravatar('jane@example.com');
$result = $gravatar->getUrl();
expect($result)->toBeString();
});

it('should return a Gravatar URL with a the correct hash', function () {
$gravatar = new Gravatar('jane@example.com');
$gravatar->setSize(200);
$result = $gravatar->getUrl();
expect($result)->toContain(md5(strtolower(trim('jane@example.com'))));
});

it('should return a Gravatar URL with a the correct size', function () {
$gravatar = new Gravatar('john.doe@example.com');
$gravatar->setSize(200);
$result = $gravatar->getUrl();
expect($result)->toContain('s=200');
});

0 comments on commit 8ea5595

Please sign in to comment.