Skip to content

Commit

Permalink
create BitlyClientFake for the unit test support (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul authored and Shivella committed Jan 28, 2020
1 parent 3b5b50a commit bd93e72
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
67 changes: 60 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Laravel Bitly Package
=====================

A laravel package for generating Bitly urls
A laravel package for generating Bitly short URLs.

For more information see [Bitly](https://bitly.com/)

Expand Down Expand Up @@ -53,29 +53,82 @@ BITLY_ACCESS_TOKEN=your_secret_bitly_access_token
Register the Bitly Facade in: **config/app.php**
``` php
'aliases' => [
```php
<?php
return [
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
...
// ...
'Bitly' => Shivella\Bitly\Facade\Bitly::class,
],
// ...
];
````
Usage
-----
``` php
```php
<?php
$url = app('bitly')->getUrl('https://www.google.com/'); // http://bit.ly/nHcn3
````
Or if you want to use facade, add this in your class after namespace declaration:
``` php
```php
<?php
use Bitly;
```

Then you can use it directly by calling `Bitly::` like:
``` php

```php
<?php

$url = Bitly::getUrl('https://www.google.com/'); // http://bit.ly/nHcn3
````

### Testing

In your unit tests you may use `BitlyClientFake` class instead of regular client.
It will create a fake short URLs using hashing without calling an external REST API, which will speed up your unit tests.
Fake might be setup via DI at your `\Tests\TestCase::createApplication()` implementation:

```php
<?php

namespace Tests;

use Illuminate\Contracts\Console\Kernel;
use Shivella\Bitly\Testing\BitlyClientFake;

trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';

$app->make(Kernel::class)->bootstrap();

// swap Bitly client by a fake
$app->singleton('bitly', function () {
return new BitlyClientFake();
});

return $app;
}
}
```

As an alternative you may use `\Shivella\Bitly\Facade\Bitly::fake()` method to swap regular client by a fake.
28 changes: 28 additions & 0 deletions Tests/Testing/BitlyClientFakeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Shivella\Bitly\Test\Testing;

use PHPUnit\Framework\TestCase;
use Shivella\Bitly\Testing\BitlyClientFake;

class BitlyClientFakeTest extends TestCase
{
/** @var \Shivella\Bitly\Testing\BitlyClientFake */
private $bitlyClient;

protected function setUp() : void
{
$this->bitlyClient = new BitlyClientFake();
}

public function testGetUrl()
{
$shortUrlFoo = $this->bitlyClient->getUrl('https://www.test.com/foo');

$this->assertTrue(strlen($shortUrlFoo) < 22);
$this->assertStringContainsString('://bit.ly', $shortUrlFoo);

$shortUrlBar = $this->bitlyClient->getUrl('https://www.test.com/bar');
$this->assertNotSame($shortUrlFoo, $shortUrlBar);
}
}
13 changes: 13 additions & 0 deletions src/Facade/Bitly.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Shivella\Bitly\Facade;

use Illuminate\Support\Facades\Facade;
use Shivella\Bitly\Testing\BitlyClientFake;

/**
* Bitly is a facade for the Bitly client.
Expand All @@ -26,4 +27,16 @@ protected static function getFacadeAccessor()
{
return 'bitly';
}

/**
* Replace the bound instance with a fake.
*
* @return \Shivella\Bitly\Testing\BitlyClientFake
*/
public static function fake()
{
static::swap($fake = new BitlyClientFake);

return $fake;
}
}
26 changes: 26 additions & 0 deletions src/Testing/BitlyClientFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Shivella\Bitly\Testing;

/**
* BitlyClientFake is a mock for the regular Bitly client.
*
* It creates a fake short URLs using hashing.
* This class may be used in unit tests to speed up their execution, removing REST API calls.
*
* > Attention: URLs generated via this class will not respond correctly, do not use it in production environment.
*
* @see \Shivella\Bitly\Client\BitlyClient
* @see \Shivella\Bitly\Facade\Bitly::fake()
*/
class BitlyClientFake
{
/**
* @param string $url raw URL.
* @return string shorten URL.
*/
public function getUrl(string $url): string
{
return 'http://bit.ly/'.substr(sha1($url), 0, 6);
}
}

0 comments on commit bd93e72

Please sign in to comment.