Unit testing

Our FoggylinePaymentBundle module is really simple. It provides only two payment methods: card and check money. It does so via two simple service classes. Since we are not going after full code coverage tests, we will only cover the CardPayment and CheckMoneyPayment service classes as part of unit testing.

We will start off by adding the following line under the testsuites element of our phpunit.xml.dist file:

<directory>src/Foggyline/PaymentBundle/Tests</directory>

With that in place, running the phpunit command from the root of our shop should pick up any test we have defined under the src/Foggyline/PaymentBundle/Tests/ directory.

Now, let's go ahead and create a test for our CardPayment service. We will do so by creating a src/Foggyline/PaymentBundle/Tests/Service/CardPaymentTest.php file, with content as follows:

namespace FoggylinePaymentBundleTestsService;

use SymfonyBundleFrameworkBundleTestKernelTestCase;

class CardPaymentTest extends KernelTestCase
{
  private $container;
  private $formFactory;
  private $router;

  public function setUp()
  {
    static::bootKernel();
    $this->container = static::$kernel->getContainer();
    $this->formFactory = $this->container->get('form.factory');
    $this->router = $this->container->get('router');
  }

  public function testGetInfoViaService()
  {
    $payment = $this->container->get('foggyline_payment.card_payment');
    $info = $payment->getInfo();
    $this->assertNotEmpty($info);
    $this->assertNotEmpty($info['payment']['form']);
  }

  public function testGetInfoViaClass()
  {
    $payment = new FoggylinePaymentBundleServiceCardPayment(
       $this->formFactory,
       $this->router
    );

    $info = $payment->getInfo();
    $this->assertNotEmpty($info);
    $this->assertNotEmpty($info['payment']['form']);
  }
}

Here, we are running two simple tests to see if we can instantiate a service, either via a container or directly, and simply call its getInfo method. The method is expected to return a response that contains the ['payment']['form'] key.

Now, let's go ahead and create a test for our CheckMoneyPayment service. We will do so by creating a src/Foggyline/PaymentBundle/Tests/Service/CheckMoneyPaymentTest.php file, with content as follows:

namespace FoggylinePaymentBundleTestsService;

use SymfonyBundleFrameworkBundleTestKernelTestCase;

class CheckMoneyPaymentTest extends KernelTestCase
{
  private $container;
  private $router;

  public function setUp()
  {
    static::bootKernel();
    $this->container = static::$kernel->getContainer();
    $this->router = $this->container->get('router');
  }

  public function testGetInfoViaService()
  {
    $payment = $this->container->get('foggyline_payment.check_money');
    $info = $payment->getInfo();
    $this->assertNotEmpty($info);
  }

  public function testGetInfoViaClass()
  {
    $payment = new FoggylinePaymentBundleServiceCheckMoneyPayment(
        $this->router
      );

    $info = $payment->getInfo();
    $this->assertNotEmpty($info);
  }
}

Similarly, here we also have two simple tests: one fetching the payment method via a container, and the other directly via a class. The difference being that we are not checking for the presence of a form key under the getInfo method response.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.189.177