Photon

Photon Git Source Tree

Root/doc/testing.mdtext

1title: Testing a Photon Component, Application or Project
2description:
3Automated testing improves the software quality by preventing regressions
4in the code. After and while implementing new features, you create and run
5the software unit tests to ensure that you do not break anything and that
6the software runs as expected. Photon provides an easy way to run your tests.
7+++
8keywords: tests, unit testing, continuous integration
9content:
10
11# Introduction
12
13Software testing is a very large field but the goal is always the
14same, getting a software with the minimum number of bugs and doing
15what it is supposed to do.
16
17**Photon** provides you with some tools to help you perform some of
18the testing of your software, the tools covers:
19
20- Unit testing with [PHPUnit][phpunit];
21- Code coverage analysis with [PHPUnit][phpunit] and [X-Debug][xdebug];
22- Continuous integration (planned);
23- Performance analysis.
24
25You will need to manage yourself all the end user testing and in
26browser testing.
27
28# How to Run the Unit Tests
29
30The unit tests with **Photon** are conventional [PHPUnit][phpunit]
31tests. Go in project folder, where the `config.test.php` is available
32and simply run:
33
34 $ photon runtests
35 PHPUnit 3.5.5 by Sebastian Bergmann.
36
37 ..
38
39 Time: 1 second, Memory: 4.50Mb
40
41 OK (2 tests, 5 assertions)
42
43 Writing code coverage data to XML file, this may take a moment.
44 Code coverage 12/12 (100%)
45
46You see here that it automatically find the tests, run them and
47perform a code coverage analysis of your code.
48
49A couple of options are available if you want to run You
50
51# How to Write the Unit Tests
52
53First you need to know a bit [PHPUnit][phpunit], because this is the
54framework used to write the tests. The choice of *PHPUnit* was
55influenced by the active development and ecosystem around this
56framework. Basically, this is the standard unit testing library for
57PHP and it had the nice idea to decrease its level of bloat for the
58programmers in the last release.
59
60The simplest test is a `tests.php` file in your application folder,
61for example: `yourproject\apps\helloworld\tests.php`. Just put:
62
63 <?php
64 namespace helloworld\tests;
65 class MySimpleTest extends \PHPUnit_Framework_TestCase
66 {
67 /**
68 * Simply test that foo returns true.
69 *
70 * It does not try to retrieve the data afterwards.
71 */
72 public function testHelloFoo()
73 {
74 $this->assertEquals(true, \helloworld\hello\Base::foo('yeah'));
75 }
76
77 /**
78 * Simple failed test.
79 */
80 public function testHelloFailed()
81 {
82 $this->assertEquals(true, false);
83 }
84 }
85
86Then run:
87
88 $ photon runtests
89
90and enjoy the testing. What you need to notice is:
91
92- you extend `\PHPUnit_Framework_TestCase` and not
93 `PHPUnit_Framework_TestCase`. The backslash in front is important as
94 the class lives in the global namespace.
95
96- you can also have more test namespaces, as long as they are sub
97 namespaces of 'yourapplication\tests` and the files in the
98 `yourproject/apps/yourapplication/tests/` folder.
99
100At the end, you write your tests exactly as you would write them with
101[PHPUnit][phpunit]. You can even run *PHPUnit* yourself from the
102command line to test your application. This is very important to allow
103you to reuse code from other projects into your own **Photon**
104projects. That is, **Photon** will not force you to rewrite your tests
105just to go well with it. **Photon** is just bootstrapping *PHPUnit*
106for you.
107
108# Complex Unit Tests
109
110In the real world, unit tests need to interact with your database,
111other web services and maybe other ØMQ components. This means that you
112need to provide your unit tests with some special configuration. You may also want to run some complex operations at the start of the tests.
113
114By default, **Photon** uses:
115
116- `config.test.php` file in your project folder for your project configuration;
117- `photon/testbootstrap.php` to setup the auto loading of the classes and load your configuration.
118
119You can use different configuration files, to run your tests with
120different setup, by using the `--conf` option, for example:
121`--conf=other/config.test.php`. For a small level of security, if the
122configuration file is named `config.php` the tests will not run. This
123is just to avoid you the bad luck of running your tests with your
124production settings by mistake...
125
126You can use a different bootstrap file with the
127`--bootstrap=path/to/bootstrap.php` option.
128
129# Self Testing of Photon
130
131To test your **Photon** installation, you can run the self test procedure:
132
133 $ photon selftest
134
135if you find a bug in **Photon**, please run the self test procedure
136and if you can write a test to showcase the bug, then, you are going
137to make the developers very happy!
138
139[phpunit]:
140[xdebug]:

Archive Download this file

Branches

Tags