Aside from the auto-generated Customer
entity and its CRUD controller, there are only two custom service classes that we created as part of this module. Since we are not going after full code coverage, we will merely cover CustomerOrders
and CustomerMenu
service classes as part of the unit testing.
We start off by adding the following line under the testsuites
element of our phpunit.xml.dist
file:
<directory>src/Foggyline/CustomerBundle/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/CustomerBundle/Tests/
directory.
Now let's go ahead and create a test for our CustomerOrders
service. We do so by creating a src/Foggyline/CustomerBundle/Tests/Service/CustomerOrders.php
file with content as follows:
namespace FoggylineCustomerBundleTestsService; use SymfonyBundleFrameworkBundleTestKernelTestCase; class CustomerOrders extends KernelTestCase { private $container; public function setUp() { static::bootKernel(); $this->container = static::$kernel->getContainer(); } public function testGetItemsViaService() { $orders = $this->container->get('foggyline_customer.customer_orders'); $this->assertNotEmpty($orders->getOrders()); } public function testGetItemsViaClass() { $orders = new FoggylineCustomerBundleServiceCustomerOrders(); $this->assertNotEmpty($orders->getOrders()); } }
Here we have two tests in total, one instantiating the class through the service and the other directly. We are using the setUp
method merely to set the container
property which we then reuse in the testGetItemsViaService
method.
Next, we create the CustomerMenu
test within the directory as follows:
namespace FoggylineCustomerBundleTestsServiceMenu; use SymfonyBundleFrameworkBundleTestKernelTestCase; class CustomerMenu extends KernelTestCase { private $container; private $tokenStorage; private $router; public function setUp() { static::bootKernel(); $this->container = static::$kernel->getContainer(); $this->tokenStorage = $this->container->get('security.token_storage'); $this->router = $this->container->get('router'); } public function testGetItemsViaService() { $menu = $this->container->get('foggyline_customer.customer_menu'); $this->assertNotEmpty($menu->getItems()); } public function testGetItemsViaClass() { $menu = new FoggylineCustomerBundleServiceMenuCustomerMenu( $this->tokenStorage, $this->router ); $this->assertNotEmpty($menu->getItems()); } }
Now, if we run the phpunit
command, we should see our test being picked up and executed alongside other tests. We can even target these two tests specifically by executing a phpunit
command with full class path, as shown here:
phpunit src/Foggyline/CustomerBundle/Tests/Service/CustomerOrders.php phpunit src/Foggyline/CustomerBundle/Tests/Service/Menu/CustomerMenu.php
3.145.93.210