Skip to content

Instantly share code, notes, and snippets.

@andreybolonin
Created May 18, 2017 09:34
Show Gist options
  • Save andreybolonin/ad7f2c8721d8d954515460272e431dc7 to your computer and use it in GitHub Desktop.
Save andreybolonin/ad7f2c8721d8d954515460272e431dc7 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types = 1);
namespace UmberFirm\Bundle\CustomerBundle\Tests\Functional\Controller;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Response;
use UmberFirm\Bundle\CommonBundle\Entity\Gender;
use UmberFirm\Bundle\CustomerBundle\Entity\Customer;
use UmberFirm\Bundle\CustomerBundle\Entity\CustomerGroup;
use UmberFirm\Bundle\CustomerBundle\Entity\CustomerProfile;
use UmberFirm\Bundle\ManufacturerBundle\Tests\BaseTestCase;
use UmberFirm\Bundle\ShopBundle\Entity\Shop;
use UmberFirm\Bundle\ShopBundle\Entity\ShopGroup;
use UmberFirm\Component\Doctrine\Uuid\UuidEntityInterface;
/**
* Class CustomerControllerTest
*
* @package UmberFirm\Bundle\CustomerBundle\Tests\Functional\Controller
*/
class CustomerControllerTest extends BaseTestCase
{
/**
* @var array|UuidEntityInterface[]
*/
private static $entities = [];
/**
* {@inheritdoc}
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
$manager = static::getObjectManager();
$customerGroup = new CustomerGroup();
$customerGroup->setName('Visitors', 'en');
$customerGroup->setName('Посетители', 'ru');
$customerGroup->mergeNewTranslations();
$manager->persist($customerGroup);
$gender = new Gender();
$gender->setName('women', 'en');
$manager->persist($gender);
$hmGroup = new ShopGroup();
$hmGroup->setName('HM Group');
$manager->persist($hmGroup);
$hmShop = new Shop();
$hmShop
->setShopGroup($hmGroup)
->setName('Helen Marlen');
$hmShop->setApiKey('00000000000000000000000000000000');
static::getObjectManager()->persist($hmShop);
$manager->persist($hmShop);
$customer = new Customer();
$customer
->setEmail('[email protected]')
->setPhone('+380951234677')
->setCustomerGroup($customerGroup)
->setShop($hmShop);
$manager->persist($customer);
$customerProfile = new CustomerProfile();
$customerProfile->setCustomer($customer)
->setFirstname('Vasya')
->setLastname('Pupkin')
->setBirthday(new \DateTime())
->setGender($gender);
$manager->persist($customerProfile);
$manager->flush();
self::$entities = [
'customer' => $customer,
'shop' => $hmShop,
];
}
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->payload = [
'phone' => '+380951234677',
'email' => '[email protected]',
'shop' => static::$entities['shop']->getId()->toString(),
];
}
/**
* Try to get list of customers
*/
public function testGetListAction()
{
$uri = $this->router->generate('umberfirm__customer__get_customers');
$this->client->request(
'GET',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
]
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_OK);
}
/**
* Try to get specified customer
*/
public function testGetAction()
{
$uri = $this->router->generate(
'umberfirm__customer__get_customer',
[
'customer' => self::$entities['customer']->getId()->toString(),
]
);
$this->client->request(
'GET',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
]
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_OK);
}
/**
* @dataProvider invalidUuidDataProvider
*
* @param string $uuid
*/
public function testTryNotFoundGetAction($uuid)
{
$uri = $this->router->generate(
'umberfirm__customer__get_customer',
[
'customer' => $uuid,
]
);
$this->client->request(
'GET',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
]
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_NOT_FOUND);
}
/**
* Try to create customer
*/
public function testPostAction()
{
$uri = $this->router->generate('umberfirm__customer__post_customer');
$this->client->request(
'POST',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
],
json_encode($this->payload)
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_CREATED);
}
/**
* Try to create customer with invalid params
*/
public function testInvalidParamsPostAction()
{
$uri = $this->router->generate('umberfirm__customer__post_customer');
//with empty params
$this->client->request(
'POST',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
],
json_encode([])
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_BAD_REQUEST);
}
/**
* Try to update customer
*/
public function testPutAction()
{
$uri = $this->router->generate(
'umberfirm__customer__put_customer',
[
'customer' => self::$entities['customer']->getId()->toString(),
]
);
$this->client->request(
'PUT',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
],
json_encode($this->payload)
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_OK);
$putResponse = json_decode($this->client->getResponse()->getContent());
$this->assertEquals($this->payload['email'], $putResponse->email);
}
/**
* @dataProvider invalidUuidDataProvider
*
* @param string $uuid
*/
public function testNotFoundPutAction($uuid)
{
$uri = $this->router->generate(
'umberfirm__customer__put_customer',
[
'customer' => $uuid,
]
);
$this->client->request(
'PUT',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
],
json_encode($this->payload)
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_NOT_FOUND);
}
public function testInvalidParamsPutAction()
{
$uri = $this->router->generate(
'umberfirm__customer__put_customer',
[
'customer' => self::$entities['customer']->getId()->toString(),
]
);
//with empty params
$this->client->request(
'PUT',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
],
json_encode([])
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_BAD_REQUEST);
}
public function testDeleteAction()
{
$uri = $this->router->generate(
'umberfirm__customer__delete_customer',
[
'customer' => static::$entities['customer']->getId()->toString(),
]
);
$this->client->request(
'DELETE',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
]
);
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$this->client->getResponse()->getStatusCode(),
$this->client->getResponse()->getContent()
);
$listUri = $this->router->generate(
'umberfirm__customer__get_customers',
[],
Router::ABSOLUTE_URL
);
$this->assertTrue(
$this->client->getResponse()->headers->contains('Location', $listUri),
$this->client->getResponse()->headers
);
}
/**
* @dataProvider invalidUuidDataProvider
*
* @param string $uuid
*/
public function testOnBadUuidFormatDeleteAction($uuid)
{
$uri = $this->router->generate(
'umberfirm__customer__delete_customer',
[
'customer' => $uuid,
]
);
$this->client->request(
'DELETE',
$uri,
[],
[],
[
'HTTP_ACCEPT' => 'application/json',
'CONTENT_TYPE' => 'application/json',
]
);
$this->assertJsonResponse($this->client->getResponse(), Response::HTTP_NOT_FOUND);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment