diff --git a/cookbook/email/testing.rst b/cookbook/email/testing.rst index 270f7506895..6ab1e522e7e 100644 --- a/cookbook/email/testing.rst +++ b/cookbook/email/testing.rst @@ -33,7 +33,9 @@ Start with an easy controller action that sends an email:: In your functional test, use the ``swiftmailer`` collector on the profiler to get information about the messages sent on the previous request:: - // src/AppBundle/Tests/Controller/MailControllerTest.php + // tests/AppBundle/Controller/MailControllerTest.php + namespace Tests\AppBundle\Controller; + use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class MailControllerTest extends WebTestCase diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index e27d944cb7e..3a082dfd49f 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -36,8 +36,8 @@ The Basics The simplest ``TypeTestCase`` implementation looks like the following:: - // src/AppBundle/Tests/Form/Type/TestedTypeTest.php - namespace AppBundle\Tests\Form\Type; + // tests/AppBundle/Form/Type/TestedTypeTest.php + namespace Tests\AppBundle\Form\Type; use AppBundle\Form\Type\TestedType; use AppBundle\Model\TestObject; @@ -117,15 +117,18 @@ might look like this:: // src/AppBundle/Form/Type/TestedType.php - // ... the buildForm method - $builder->add('app_test_child_type'); + // ... + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->add('app_test_child_type'); + } To create your form correctly, you need to make the type available to the form factory in your test. The easiest way is to register it manually before creating the parent form using the ``PreloadedExtension`` class:: - // src/AppBundle/Tests/Form/Type/TestedTypeTests.php - namespace AppBundle\Tests\Form\Type; + // tests/AppBundle/Form/Type/TestedTypeTests.php + namespace Tests\AppBundle\Form\Type; use AppBundle\Form\Type\TestedType; use AppBundle\Model\TestObject; @@ -158,7 +161,7 @@ before creating the parent form using the ``PreloadedExtension`` class:: be getting errors that are not related to the form you are currently testing but to its children. -Adding custom Extensions +Adding Custom Extensions ------------------------ It often happens that you use some options that are added by @@ -168,8 +171,8 @@ The ``TypeTestCase`` loads only the core form extension so an "Invalid option" exception will be raised if you try to use it for testing a class that depends on other extensions. You need to add those extensions to the factory object:: - // src/AppBundle/Tests/Form/Type/TestedTypeTests.php - namespace AppBundle\Tests\Form\Type; + // tests/AppBundle/Form/Type/TestedTypeTests.php + namespace Tests\AppBundle\Form\Type; use AppBundle\Form\Type\TestedType; use AppBundle\Model\TestObject; @@ -217,8 +220,8 @@ Testing against different Sets of Data If you are not familiar yet with PHPUnit's `data providers`_, this might be a good opportunity to use them:: - // src/AppBundle/Tests/Form/Type/TestedTypeTests.php - namespace AppBundle\Tests\Form\Type; + // tests/AppBundle/Form/Type/TestedTypeTests.php + namespace Tests\AppBundle\Form\Type; use AppBundle\Form\Type\TestedType; use AppBundle\Model\TestObject; @@ -226,7 +229,6 @@ a good opportunity to use them:: class TestedTypeTest extends TypeTestCase { - /** * @dataProvider getValidTestData */ diff --git a/cookbook/testing/database.rst b/cookbook/testing/database.rst index 04c8d07ee48..405a329f4e1 100644 --- a/cookbook/testing/database.rst +++ b/cookbook/testing/database.rst @@ -33,6 +33,7 @@ class. Suppose the class you want to test looks like this:: + // src/AppBundle/Salary/SalaryCalculator.php namespace AppBundle\Salary; use Doctrine\Common\Persistence\ObjectManager; @@ -59,14 +60,20 @@ Suppose the class you want to test looks like this:: Since the ``ObjectManager`` gets injected into the class through the constructor, it's easy to pass a mock object within a test:: + // tests/AppBundle/Salary/SalaryCalculatorTest.php + namespace Tests\AppBundle\Salary; + use AppBundle\Salary\SalaryCalculator; + use AppBundle\Entity\Employee; + use Doctrine\ORM\EntityRepository; + use Doctrine\Common\Persistence\ObjectManager; class SalaryCalculatorTest extends \PHPUnit_Framework_TestCase { public function testCalculateTotalSalary() { // First, mock the object to be used in the test - $employee = $this->getMock('\AppBundle\Entity\Employee'); + $employee = $this->getMock(Employee::class); $employee->expects($this->once()) ->method('getSalary') ->will($this->returnValue(1000)); @@ -76,7 +83,7 @@ it's easy to pass a mock object within a test:: // Now, mock the repository so it returns the mock of the employee $employeeRepository = $this - ->getMockBuilder('\Doctrine\ORM\EntityRepository') + ->getMockBuilder(EntityRepository::class) ->disableOriginalConstructor() ->getMock(); $employeeRepository->expects($this->once()) @@ -85,7 +92,7 @@ it's easy to pass a mock object within a test:: // Last, mock the EntityManager to return the mock of the repository $entityManager = $this - ->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager') + ->getMockBuilder(ObjectManager::class) ->disableOriginalConstructor() ->getMock(); $entityManager->expects($this->once()) diff --git a/cookbook/testing/doctrine.rst b/cookbook/testing/doctrine.rst index c263d18c91c..aadbe729113 100644 --- a/cookbook/testing/doctrine.rst +++ b/cookbook/testing/doctrine.rst @@ -20,12 +20,12 @@ If you need to actually execute a query, you will need to boot the kernel to get a valid connection. In this case, you'll extend the ``KernelTestCase``, which makes all of this quite easy:: - // src/AppBundle/Tests/Entity/ProductRepositoryFunctionalTest.php - namespace AppBundle\Tests\Entity; + // tests/AppBundle/Entity/ProductRepositoryTest.php + namespace Tests\AppBundle\Entity; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; - class ProductRepositoryFunctionalTest extends KernelTestCase + class ProductRepositoryTest extends KernelTestCase { /** * @var \Doctrine\ORM\EntityManager @@ -38,10 +38,10 @@ which makes all of this quite easy:: public function setUp() { self::bootKernel(); + $this->em = static::$kernel->getContainer() ->get('doctrine') - ->getManager() - ; + ->getManager(); } public function testSearchByCategoryName() @@ -60,6 +60,7 @@ which makes all of this quite easy:: protected function tearDown() { parent::tearDown(); + $this->em->close(); } } diff --git a/cookbook/testing/profiling.rst b/cookbook/testing/profiling.rst index 1789c69dbc9..d809c4febe2 100644 --- a/cookbook/testing/profiling.rst +++ b/cookbook/testing/profiling.rst @@ -15,9 +15,9 @@ spent in the framework, etc. But before writing assertions, enable the profiler and check that the profiler is indeed available (it is enabled by default in the ``test`` environment):: - class HelloControllerTest extends WebTestCase + class LuckyControllerTest extends WebTestCase { - public function testIndex() + public function testNumberAction() { $client = static::createClient(); @@ -25,7 +25,7 @@ the ``test`` environment):: // (it does nothing if the profiler is not available) $client->enableProfiler(); - $crawler = $client->request('GET', '/hello/Fabien'); + $crawler = $client->request('GET', '/lucky/number'); // ... write some assertions about the Response diff --git a/cookbook/testing/simulating_authentication.rst b/cookbook/testing/simulating_authentication.rst index f2e04acd612..8777a67e2a1 100644 --- a/cookbook/testing/simulating_authentication.rst +++ b/cookbook/testing/simulating_authentication.rst @@ -15,8 +15,8 @@ Another way would be to create a token yourself and store it in a session. While doing this, you have to make sure that an appropriate cookie is sent with a request. The following example demonstrates this technique:: - // src/AppBundle/Tests/Controller/DefaultControllerTest.php - namespace Appbundle\Tests\Controller; + // tests/AppBundle/Controller/DefaultControllerTest.php + namespace Tests\Appbundle\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\BrowserKit\Cookie;