-
Notifications
You must be signed in to change notification settings - Fork 2
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 #4 from Mane-Olawale/development
feat: add TDD support to the package and write test for it
- Loading branch information
Showing
8 changed files
with
872 additions
and
4 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
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
class TermiiMessage extends Message | ||
{ | ||
|
||
/** | ||
* Array of content lines. | ||
* | ||
|
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,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); | ||
}); | ||
} | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.