Skip to content

Commit df20095

Browse files
committed
Testing changes
1 parent ef613f6 commit df20095

File tree

6 files changed

+38
-26
lines changed

6 files changed

+38
-26
lines changed

cookbook/email/testing.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ Start with an easy controller action that sends an email::
3333
In your functional test, use the ``swiftmailer`` collector on the profiler
3434
to get information about the messages sent on the previous request::
3535

36-
// src/AppBundle/Tests/Controller/MailControllerTest.php
36+
// tests/AppBundle/Controller/MailControllerTest.php
37+
namespace Tests\AppBundle\Controller;
38+
3739
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
3840

3941
class MailControllerTest extends WebTestCase

cookbook/form/unit_testing.rst

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ The Basics
3636

3737
The simplest ``TypeTestCase`` implementation looks like the following::
3838

39-
// src/AppBundle/Tests/Form/Type/TestedTypeTest.php
40-
namespace AppBundle\Tests\Form\Type;
39+
// tests/AppBundle/Form/Type/TestedTypeTest.php
40+
namespace Tests\AppBundle\Form\Type;
4141

4242
use AppBundle\Form\Type\TestedType;
4343
use AppBundle\Model\TestObject;
@@ -117,15 +117,18 @@ might look like this::
117117

118118
// src/AppBundle/Form/Type/TestedType.php
119119

120-
// ... the buildForm method
121-
$builder->add('app_test_child_type');
120+
// ...
121+
public function buildForm(FormBuilderInterface $builder, array $options)
122+
{
123+
$builder->add('app_test_child_type');
124+
}
122125

123126
To create your form correctly, you need to make the type available to the
124127
form factory in your test. The easiest way is to register it manually
125128
before creating the parent form using the ``PreloadedExtension`` class::
126129

127-
// src/AppBundle/Tests/Form/Type/TestedTypeTests.php
128-
namespace AppBundle\Tests\Form\Type;
130+
// tests/AppBundle/Form/Type/TestedTypeTests.php
131+
namespace Tests\AppBundle\Form\Type;
129132

130133
use AppBundle\Form\Type\TestedType;
131134
use AppBundle\Model\TestObject;
@@ -158,7 +161,7 @@ before creating the parent form using the ``PreloadedExtension`` class::
158161
be getting errors that are not related to the form you are currently
159162
testing but to its children.
160163

161-
Adding custom Extensions
164+
Adding Custom Extensions
162165
------------------------
163166

164167
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"
168171
exception will be raised if you try to use it for testing a class that depends
169172
on other extensions. You need to add those extensions to the factory object::
170173

171-
// src/AppBundle/Tests/Form/Type/TestedTypeTests.php
172-
namespace AppBundle\Tests\Form\Type;
174+
// tests/AppBundle/Form/Type/TestedTypeTests.php
175+
namespace Tests\AppBundle\Form\Type;
173176

174177
use AppBundle\Form\Type\TestedType;
175178
use AppBundle\Model\TestObject;
@@ -217,16 +220,15 @@ Testing against different Sets of Data
217220
If you are not familiar yet with PHPUnit's `data providers`_, this might be
218221
a good opportunity to use them::
219222

220-
// src/AppBundle/Tests/Form/Type/TestedTypeTests.php
221-
namespace AppBundle\Tests\Form\Type;
223+
// tests/AppBundle/Form/Type/TestedTypeTests.php
224+
namespace Tests\AppBundle\Form\Type;
222225

223226
use AppBundle\Form\Type\TestedType;
224227
use AppBundle\Model\TestObject;
225228
use Symfony\Component\Form\Test\TypeTestCase;
226229

227230
class TestedTypeTest extends TypeTestCase
228231
{
229-
230232
/**
231233
* @dataProvider getValidTestData
232234
*/

cookbook/testing/database.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class.
3333

3434
Suppose the class you want to test looks like this::
3535

36+
// src/AppBundle/Salary/SalaryCalculator.php
3637
namespace AppBundle\Salary;
3738

3839
use Doctrine\Common\Persistence\ObjectManager;
@@ -59,14 +60,20 @@ Suppose the class you want to test looks like this::
5960
Since the ``ObjectManager`` gets injected into the class through the constructor,
6061
it's easy to pass a mock object within a test::
6162

63+
// tests/AppBundle/Salary/SalaryCalculatorTest.php
64+
namespace Tests\AppBundle\Salary;
65+
6266
use AppBundle\Salary\SalaryCalculator;
67+
use AppBundle\Entity\Employee;
68+
use Doctrine\ORM\EntityRepository;
69+
use Doctrine\Common\Persistence\ObjectManager;
6370

6471
class SalaryCalculatorTest extends \PHPUnit_Framework_TestCase
6572
{
6673
public function testCalculateTotalSalary()
6774
{
6875
// First, mock the object to be used in the test
69-
$employee = $this->getMock('\AppBundle\Entity\Employee');
76+
$employee = $this->getMock(Employee::class);
7077
$employee->expects($this->once())
7178
->method('getSalary')
7279
->will($this->returnValue(1000));
@@ -76,7 +83,7 @@ it's easy to pass a mock object within a test::
7683

7784
// Now, mock the repository so it returns the mock of the employee
7885
$employeeRepository = $this
79-
->getMockBuilder('\Doctrine\ORM\EntityRepository')
86+
->getMockBuilder(EntityRepository::class)
8087
->disableOriginalConstructor()
8188
->getMock();
8289
$employeeRepository->expects($this->once())
@@ -85,7 +92,7 @@ it's easy to pass a mock object within a test::
8592

8693
// Last, mock the EntityManager to return the mock of the repository
8794
$entityManager = $this
88-
->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
95+
->getMockBuilder(ObjectManager::class)
8996
->disableOriginalConstructor()
9097
->getMock();
9198
$entityManager->expects($this->once())

cookbook/testing/doctrine.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ If you need to actually execute a query, you will need to boot the kernel
2020
to get a valid connection. In this case, you'll extend the ``KernelTestCase``,
2121
which makes all of this quite easy::
2222

23-
// src/AppBundle/Tests/Entity/ProductRepositoryFunctionalTest.php
24-
namespace AppBundle\Tests\Entity;
23+
// tests/AppBundle/Entity/ProductRepositoryTest.php
24+
namespace Tests\AppBundle\Entity;
2525

2626
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2727

28-
class ProductRepositoryFunctionalTest extends KernelTestCase
28+
class ProductRepositoryTest extends KernelTestCase
2929
{
3030
/**
3131
* @var \Doctrine\ORM\EntityManager
@@ -38,10 +38,10 @@ which makes all of this quite easy::
3838
public function setUp()
3939
{
4040
self::bootKernel();
41+
4142
$this->em = static::$kernel->getContainer()
4243
->get('doctrine')
43-
->getManager()
44-
;
44+
->getManager();
4545
}
4646

4747
public function testSearchByCategoryName()
@@ -60,6 +60,7 @@ which makes all of this quite easy::
6060
protected function tearDown()
6161
{
6262
parent::tearDown();
63+
6364
$this->em->close();
6465
}
6566
}

cookbook/testing/profiling.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ spent in the framework, etc. But before writing assertions, enable the profiler
1515
and check that the profiler is indeed available (it is enabled by default in
1616
the ``test`` environment)::
1717

18-
class HelloControllerTest extends WebTestCase
18+
class LuckyControllerTest extends WebTestCase
1919
{
20-
public function testIndex()
20+
public function testNumberAction()
2121
{
2222
$client = static::createClient();
2323

2424
// Enable the profiler for the next request
2525
// (it does nothing if the profiler is not available)
2626
$client->enableProfiler();
2727

28-
$crawler = $client->request('GET', '/hello/Fabien');
28+
$crawler = $client->request('GET', '/lucky/number');
2929

3030
// ... write some assertions about the Response
3131

cookbook/testing/simulating_authentication.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Another way would be to create a token yourself and store it in a session.
1515
While doing this, you have to make sure that an appropriate cookie is sent
1616
with a request. The following example demonstrates this technique::
1717

18-
// src/AppBundle/Tests/Controller/DefaultControllerTest.php
19-
namespace Appbundle\Tests\Controller;
18+
// tests/AppBundle/Controller/DefaultControllerTest.php
19+
namespace Tests\Appbundle\Controller;
2020

2121
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
2222
use Symfony\Component\BrowserKit\Cookie;

0 commit comments

Comments
 (0)