Our module has two controller classes that we want to test for responses. We want to make sure that the authorize
, capture
, and cancel
methods of the CardController
and CheckMoneyController
classes are working.
We first create asrc/Foggyline/PaymentBundle/Tests/Controller/CardControllerTest.php
file, with content as follows:
namespace FoggylinePaymentBundleTestsController; use SymfonyBundleFrameworkBundleTestWebTestCase; class CardControllerTest extends WebTestCase { private $client; private $router; public function setUp() { $this->client = static::createClient(); $this->router = $this->client->getContainer()->get('router'); } public function testAuthorizeAction() { $this->client->request('GET', $this->router->generate('foggyline_payment_card_authorize')); $this->assertTests(); } public function testCaptureAction() { $this->client->request('GET', $this->router->generate('foggyline_payment_card_capture')); $this->assertTests(); } public function testCancelAction() { $this->client->request('GET', $this->router->generate('foggyline_payment_card_cancel')); $this->assertTests(); } private function assertTests() { $this->assertSame(200, $this->client->getResponse()->getStatusCode()); $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); $this->assertContains('success', $this->client->getResponse()->getContent()); $this->assertNotEmpty($this->client->getResponse()->getContent()); } }
We then create src/Foggyline/PaymentBundle/Tests/Controller/CheckMoneyControllerTest.php
, with content as follows:
namespace FoggylinePaymentBundleTestsController; use SymfonyBundleFrameworkBundleTestWebTestCase; class CheckMoneyControllerTest extends WebTestCase { private $client; private $router; public function setUp() { $this->client = static::createClient(); $this->router = $this->client->getContainer()->get('router'); } public function testAuthorizeAction() { $this->client->request('GET', $this->router->generate('foggyline_payment_check_money_authorize')); $this->assertTests(); } public function testCaptureAction() { $this->client->request('GET', $this->router->generate('foggyline_payment_check_money_capture')); $this->assertTests(); } public function testCancelAction() { $this->client->request('GET', $this->router->generate('foggyline_payment_check_money_cancel')); $this->assertTests(); } private function assertTests() { $this->assertSame(200, $this->client->getResponse()->getStatusCode()); $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); $this->assertContains('success', $this->client->getResponse()->getContent()); $this->assertNotEmpty($this->client->getResponse()->getContent()); } }
Both tests are nearly identical. They contain a test for each of the authorize
, capture
, and cancel
methods. Since our methods are implemented with a fixed success JSON response, there are no surprises here. However, we can easily play around with it by extending our payment methods into something more robust.
3.144.84.155