Skip to content

Commit

Permalink
Merge pull request #4 from Mane-Olawale/development
Browse files Browse the repository at this point in the history
feat: add TDD support to the package and write test for it
  • Loading branch information
Mane-Olawale authored Feb 16, 2022
2 parents 424c8d1 + 1ff20e7 commit 669def9
Show file tree
Hide file tree
Showing 8 changed files with 872 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/HttpManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
namespace ManeOlawale\Laravel\Termii;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\App;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Http\Client\Factory;
use Illuminate\Support\Facades\Http;
use ManeOlawale\Laravel\Termii\Termii;
use ManeOlawale\Termii\HttpClient\HttpManagerInterface;
Expand Down
1 change: 0 additions & 1 deletion src/Messages/TermiiMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class TermiiMessage extends Message
{

/**
* Array of content lines.
*
Expand Down
74 changes: 73 additions & 1 deletion src/Termii.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@

namespace ManeOlawale\Laravel\Termii;

use GuzzleHttp\Psr7\Response;
use ManeOlawale\Laravel\Termii\Testing\AssertTrait;
use ManeOlawale\Laravel\Termii\Testing\Sequence;
use ManeOlawale\Laravel\Termii\Testing\TermiiFake;
use ManeOlawale\Termii\Client;

class Termii
{
use AssertTrait;

/**
* The custom Termii client instance.
*
* @var \ManeOlawale\Termii\Client
*/
protected $client;

/**
* The custom Termii client instance.
*
* @var \ManeOlawale\Laravel\Termii\Testing\TermiiFake
*/
protected $fake;

/**
* The custom Termii client instance.
*
* @var \ManeOlawale\Laravel\Termii\Testing\TermiiFake
*/
protected $test = false;

/**
* The custom Termii client instance.
*
Expand All @@ -33,7 +53,7 @@ public function __call(string $tag, array $argv)
*
* @return \ManeOlawale\Termii\Client
*/
public function client()
public function client(): Client
{
return $this->client;
}
Expand Down Expand Up @@ -66,4 +86,56 @@ public function send(string $to, string $message, string $from = null, string $c
{
return $this->client->sms->send($to, $message, $from, $channel);
}

/**
* Switch termii to test mode
*
* @param array
*/
public function fake(array $fakes = null)
{
if (!$this->fake) {
$this->test = true;
$this->fake = new TermiiFake($this);
$this->fake->setUpTestMode();
}

if ($fakes) {
foreach ($fakes as $alias => $sequence) {
$this->fake->mock($alias, $sequence);
}
}

return $this->fake;
}

/**
* Mock a response with sequence
* @since 0.0.2
*
* @param string $alias
* @param \ManeOlawale\Laravel\Termii\Testing\Sequence $sequence
* @return void
*/
public function mock(string $alias, Sequence $sequence = null)
{
$this->fake->mock($alias, $sequence);
return $this;
}

/**
* Get or Set fallback response
* @since 0.0.2
*
* @param \GuzzleHttp\Psr7\Response $response
* @return \GuzzleHttp\Psr7\Response
*/
public function fallbackResponse(Response $response = null)
{
if ($response) {
$this->fake()->fallbackResponse($response);
}

return $this->fake()->fallbackResponse();
}
}
162 changes: 162 additions & 0 deletions src/Testing/AssertTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/*
* This file is part of the Termii Client.
*
* (c) Ilesanmi Olawale Adedoun Twitter: @mane_olawale
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ManeOlawale\Laravel\Termii\Testing;

use Closure;
use PHPUnit\Framework\Assert as PHPUnit;

/**
* Assertion functions
*/
trait AssertTrait
{
/**
* Assert sent requests
* @param string $alias
*/
public function assertSent(string $alias)
{
PHPUnit::assertTrue(
$this->fake()->responses($alias)->count() > 0,
sprintf('Expected request to be sent to {%s} at least once.', $this->fake()->getPath($alias))
);
}

/**
* Assert sent requests
* @param string $alias
*/
public function assertNotSent(string $alias)
{
PHPUnit::assertTrue(
$this->fake()->responses($alias)->count() < 1,
sprintf('Expected no request to be sent to {%s}.', $this->fake()->getPath($alias))
);
}

/**
* Assert number of sent requests
* @param string $alias
*/
public function assertSentTimes(string $alias, int $times = 1)
{
$count = $this->fake()->responses($alias)->count();

PHPUnit::assertSame(
$times,
$count,
sprintf(
'The expected {%s} endpoint received request {%s} times instead of {%s} times.',
$this->fake()->getPath($alias),
$times,
$count
)
);
}

/**
* Assert successful requests
* @param string $alias
*/
public function assertSentSuccessful(string $alias)
{
$count = $this->fake()->responses($alias)->where('successful', true)->count();

PHPUnit::assertTrue(
$count > 0,
sprintf(
'The expected {%s} endpoint should return success response at least once.',
$this->fake()->getPath($alias)
)
);
}

/**
* Assert successful requests times
* @param string $alias
*/
public function assertSentSuccessfulTimes(string $alias, int $times)
{
$count = $this->fake()->responses($alias)->where('successful', true)->count();

PHPUnit::assertSame(
$times,
$count,
sprintf(
'The expected {%s} endpoint return success response {%s} times instead of {%s} times.',
$this->fake()->getPath($alias),
$times,
$count
)
);
}

/**
* Assert failed requests
* @param string $alias
*/
public function assertSentFailed(string $alias)
{
$count = $this->fake()->responses($alias)->where('successful', false)->count();

PHPUnit::assertTrue(
$count > 0,
sprintf(
'The expected {%s} endpoint should return success response at least once.',
$this->fake()->getPath($alias)
)
);
}

/**
* Assert failed requests times
* @param string $alias
*/
public function assertSentFailedTimes(string $alias, int $times = 1)
{
$count = $this->fake()->responses($alias)->where('successful', false)->count();

PHPUnit::assertSame(
$times,
$count,
sprintf(
'The expected {%s} endpoint return failed response {%s} times instead of {%s} times.',
$this->fake()->getPath($alias),
$times,
$count
)
);
}

/**
* Assert sent requests with a closure of sequence of closures
* @param string $alias
* @param \Closure|Sequence $callable
*/
public function assert(string $alias, $callable)
{
if ($this->fake()->responses($alias)->count()) {
if ($callable instanceof Closure) {
$callable($this->fake()->responses($alias)->first());
}

if ($callable instanceof Sequence) {
$this->fake()->responses($alias)
->take($callable->count())
->each(function ($item) use ($callable) {
$closure = $callable->next();
$closure($item);
});
}
}
}
}
54 changes: 54 additions & 0 deletions src/Testing/FakeHttpManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Termii Client.
*
* (c) Ilesanmi Olawale Adedoun Twitter: @mane_olawale
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ManeOlawale\Laravel\Termii\Testing;

use Psr\Http\Message\ResponseInterface;
use ManeOlawale\Laravel\Termii\Testing\TermiiFake;
use ManeOlawale\Termii\HttpClient\HttpManagerInterface;

class FakeHttpManager implements HttpManagerInterface
{
/**
* Termii instance
* @var \ManeOlawale\Laravel\Termii\Testing\TermiiFake
*/
protected $fake;

/**
* Contructor
*
* @since 1.0
*
* @param \GuzzleHttp\Client $http
*/
public function __construct(TermiiFake $fake)
{
$this->fake = $fake;
}

/**
* Handle requests
*
* @since 1.0
*
* @param string $method
* @param string $route
* @param array $data
* @return \Psr\Http\Message\ResponseInterface
*/
public function request(string $method, string $url, array $data): ResponseInterface
{
$alias = $this->fake->getPathAlias(trim(parse_url($url, PHP_URL_PATH), '/'));

return $this->fake->request($alias, $method, $url, $data);
}
}
Loading

0 comments on commit 669def9

Please sign in to comment.