Quantcast
Channel: test.ical.ly » Symfony Live 2010
Viewing all articles
Browse latest Browse all 9

Alles neu in Symfony 2.0. Jetzt auch das Testen mit PHPUnit!

$
0
0

Gerade erst gestern habe ich mich hier öffentlich gewundert, warum das Thema Testing auf der Symfony Live 2010 keine Rolle spielte als Symfony 2.0 enthüllt wurde. In den Sourcen von Symfony 2.0 war nur lime bzw. Lime2 zu entdecken.

Twitter

Nun scheint die Antwort gefunden zu sein.

Benjamin Eberlei, seines Zeichens beteiligt am Zend Framework und Doctrine, machte mich gestern darauf aufmerksam, dass erst am vergangenen Wochenende eine Entscheidung zum Thema Testing gefallen sei.

Symfony 2.0 wird auf PHPUnit umsteigen!

Ich habe mir tatsächlich mal die Mühe gemacht, ein wenig auf GitHub herumzuschauen, ob denn da irgendein Commit zu finden ist, der diese Entscheidung bestätigt.

Nichts im Symfony Repository, nichts bei Fabien aber dann doch was!

Unter dieser URL gibt es die Symfony 2 Testing Conventions zu finden. Darin ist nur noch von PHPUnit die Rede!

Was mich auch freut ist, dass es sich dabei um Commits von Bernhard Schussek handelt und man wohl davon ausgehen kann, dass hier niemand vor den Kopf gestossen wurde. Immerhin war er die treibende Kraft hinter Lime2.

Ich finde es toll, dass sich Symfony mit der Verwendung zuerst einiger Zend Framework Komponenten und jetzt von PHPUnit immer mehr “fremden” PHP Standards öffnet.

Hier nochmal für alle zum Nachlesen. Was daraus zu lernen ist, werde ich mir ein anderes mal ansehen.

Symfony 2 Testing Conventions

Symfony 2 is programmatically tested using unit tests. You can read more about unit testing on Wikipedia.

Test Organization

Directory Structure

The src/ directory contains the three subdirectories Components/Foundation/ and Framework/, which contains the core bundles. The components and core bundles should have a subdirectory Tests/ which contains all the tests for the component/bundle. The foundation tests belong in the subdirectory Tests/ of the Foundation/ directory.

Some examples:

  • src/Symfony/Components/EventDispatcher/EventDispatcher.php should be tested insrc/Symfony/Components/EventDispatcher/Tests/EventDispatcherTest.php
  • src/Symfony/Framework/WebBundle/User.php should be tested in src/Symfony/Framework/WebBundle/Tests/UserTest.php
  • src/Symfony/Foundation/ClassLoader.php should be tested in src/Symfony/Foundation/Tests/ClassLoaderTest.php

The subdirectory structure in the Tests/ directories should match the directory structure of the classes.

Example:

  • src/Symfony/Foundation/Bundle/Bundle.php should be tested in src/Symfony/Foundation/Tests/Bundle/BundleTest.php

Namespaces

The namespaces of the test classes should match their directory structure.

Example:

  • The namespace of src/Symfony/Foundation/Tests/Bundle/BundleTest.php is Symfony\Foundation\Tests\Bundle

Test Suites

Every Tests/ directory should contain a configuration.xml file that tells PHPUnit where to find the test classes. You can use the following template for a new component or bundle.

Test Classes

Test classes should have the suffix Test and generally inherit PHPUnit_Framework_TestCase. The name of a test class should refer to a class or the state or aspect of a class.

Some examples:

  • FormFieldTest is a good name because it refers to the FormField class
  • FormFieldUnboundTest is a good name because it refers to the “unbound” state of the FormField class
  • FormFieldCreate is a bad name, because it’s too generic

Test Methods

Methods should support agile documentation and should be named so that if it fails, it is obvious what failed. They should also give information of the system they test

For example the method test name testBindInvalidData() is a good name.

Test method names can be long, but the method content should not be. If you need several assert-calls, divide the method into smaller methods. There should never be assertions within any loops, and rarely within functions.

NOTE Commonly used testing method naming convention test[methodName] is not allowed in Symfony 2. So in this casetestBind() would not be allowed!

Test Fixtures

Shared test fixtures should be set up and deleted in the methods setUp() and tearDown().

Running Tests

Tests can be executed via the phpunit command. If you have not already, install PHPUnit via PEAR.

Then you can execute all tests of Symfony 2:

$ phpunit --configuration=src/Symfony/Tests/configuration.xml

You can also execute the tests of the foundation or of a specific component or bundle:

$ phpunit src/Symfony/Foundation/Tests/AllTests.php
$ phpunit src/Symfony/Components/EventDispatcher/Tests/AllTests.php
$ phpunit src/Symfony/Framework/WebBundle/Tests/AllTests.php

The last option is to execute the tests of all components or all core bundles:

$ phpunit src/Symfony/Components/Tests/AllTests.php
$ phpunit src/Symfony/Framework/Tests/AllTests.php

Viewing all articles
Browse latest Browse all 9

Trending Articles