Skip to content

Commit

Permalink
Testing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Nov 28, 2015
1 parent ef613f6 commit df20095
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
4 changes: 3 additions & 1 deletion cookbook/email/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions cookbook/form/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -217,16 +220,15 @@ 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;
use Symfony\Component\Form\Test\TypeTestCase;

class TestedTypeTest extends TypeTestCase
{

/**
* @dataProvider getValidTestData
*/
Expand Down
13 changes: 10 additions & 3 deletions cookbook/testing/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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())
Expand All @@ -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())
Expand Down
11 changes: 6 additions & 5 deletions cookbook/testing/doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -60,6 +60,7 @@ which makes all of this quite easy::
protected function tearDown()
{
parent::tearDown();

$this->em->close();
}
}
6 changes: 3 additions & 3 deletions cookbook/testing/profiling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ 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();

// Enable the profiler for the next request
// (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

Expand Down
4 changes: 2 additions & 2 deletions cookbook/testing/simulating_authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit df20095

Please sign in to comment.