Skip to content

Instantly share code, notes, and snippets.

@AleksKu
Created August 5, 2016 13:51
Show Gist options
  • Save AleksKu/39e6040b1916cf74562844e5cbc12d80 to your computer and use it in GitHub Desktop.
Save AleksKu/39e6040b1916cf74562844e5cbc12d80 to your computer and use it in GitHub Desktop.
Юнит-тесты отправки постбеков вебмастерам(Notifications) с использованием мокирования ответов HTTP сервера (заглушка для http-сервера).
<?php
namespace Tests;
use Doctrine\ORM\EntityManager;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PostbackBundle\Entity\Notification;
use PostbackBundle\Exception\NotificationMaxTryCountException;
use PostbackBundle\Exception\NotificationNotFoundException;
use PostbackBundle\Service\NotificationManager;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Tests\UnitTester;
class NotificationManagerTest extends \Codeception\Test\Unit
{
/**
* @var UnitTester
*/
protected $tester;
/**
* @var ContainerInterface
*/
private $container;
/**
* @var EntityManager
*/
private $em;
/**
* @var NotificationManager
*/
private $notificationManager;
/**
* @var Notification
*/
private $notification;
// executed before each test
protected function _before()
{
// accessing container
$this->container = $this->tester->grabService('service_container');
$this->em = $this->container->get('doctrine.orm.entity_manager');
$this->notificationManager = $this->container->get('postback.service.notification_manager');
$rep = $this->em->getRepository(Notification::class);
$this->notification = $rep->findOneBy([
'status' => Notification::STATUS_NEW,
]);
}
// executed after each test
protected function _after()
{
}
public function testAddNewNotification()
{
$nm = $this->notificationManager;
$I = $this->tester;
$url = 'http://testnote';
$newNote = $nm->addNewNotification($url);
$I->assertInstanceOf(Notification::class, $newNote);
$I->assertEquals($url, $newNote->getNotificationUrl());
$I->seeInRepository(Notification::class, ['notificationUrl' => $url]);
}
public function testSendNotification()
{
$nm = $this->notificationManager;
$I = $this->tester;
$note = $this->notification;
$client = $this->createHttpClient(new Response(200));
$nm->setClient($client);
$newNote = $nm->sendNotification($note->getId());
$I->assertEquals($newNote->getId(), $note->getId());
$I->assertEquals($newNote->getStatus(), Notification::STATUS_DELIVERED);
$I->assertEquals($newNote->getTryCount(), 1);
}
public function testSendNotificationNotFoundException()
{
$nm = $this->notificationManager;
$I = $this->tester;
$note = $this->notification;
$I->expectException(NotificationNotFoundException::class, function () use ($nm) {
$nm->sendNotification('435345');
});
}
/**
* тесты на кол-во попыток
*
*/
public function testSendNotificationNotificationMaxTryCountException()
{
$I = $this->tester;
$nm = $this->notificationManager;
$client = $this->createHttpClient([
new Response(500),
new Response(500),
new Response(404),
]);
$nm->setClient($client);
$note = $this->notification;
$repeat = function ($count = 1) use ($nm, $note) {
for ($i = 1; $i <= $count; $i++) {
try {
$nm->sendNotification($note->getId());
} catch (\Exception $e) {
}
}
};
$repeat(2);
$I->assertEquals(2, $note->getTryCount());
$I->assertEquals(Notification::STATUS_NEW, $note->getStatus());
try {
$nm->sendNotification($note->getId());
} catch (\Exception $e) {
$I->assertInstanceOf(NotificationMaxTryCountException::class, $e);
$I->assertEquals(Notification::STATUS_CANCELED, $note->getStatus());
$I->seeInRepository(Notification::class, [
'id' => $note->getId(),
'status' => Notification::STATUS_CANCELED,
]);
}
}
/**
* тесты на ошибки сети
*
*/
public function testSendNotificationHttp404Error()
{
$I = $this->tester;
$nm = $this->notificationManager;
$client = $this->createHttpClient([
new Response(500),
new Response(404),
]);
$nm->setClient($client);
$I->expectException(RequestException::class, function () use ($nm) {
$nm->sendNotification($this->notification->getId());
});
try {
$newNote = $nm->sendNotification($this->notification->getId());
} catch (RequestException $e) {
$I->assertEquals(Notification::ERROR_WRONG_RESPONSE, $this->notification->getErrorCode());
$I->assertEquals(Notification::STATUS_NEW, $this->notification->getStatus());
$I->assertEquals(2, $this->notification->getTryCount());
}
}
/**
* @param $responses
* @return Client
*/
protected function createHttpClient($responses)
{
$queue = [];
if (!is_array($responses)) {
$responses = [$responses];
}
foreach ($responses as $response) {
if ($response instanceof Response) {
$queue[] = $response;
} else {
$queue[] = new Response(200, ['Content-Type' => 'application/json'], $response);
}
}
$mock = new MockHandler($queue);
$handler = HandlerStack::create($mock);
$httpClient = new Client(['handler' => $handler]);
return $httpClient;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment