Sunday 14 November 2010 1:19:17 am
By : Jean-Luc Chassaing
In my last post, I described how to get a testing environment for your eZ Publish developments. This was in my previous post.
Now it's time to go further on write your first test and run it.
This is not a PHPUnit tutorial the goal is to give you a first step and some clues to avoid brainstorming gapes.
Ok so lets try and write our first test.
The statement is, you're coding your new extension called mynewextension. As your are making some special things out of eZ Publish, you've written your own class MyClass. So the folder structure of your extension should look like this :
mynewextension
|__ classes
|__ myclass.php
...
So first you'll have to create a tests folder where all your tests will be. Then replicate your code folder structure in your test folder. so things should look like :
mynewextension
|___ classes
| |__ myclass.php
|___ tests
|__ classes
| |__ myclass_test.php
|__ suite.php
...
So as you can see, I've added a tests folder with a classes folder where is the test case for my class : myclass_test.php and then in the tests folder there is a suite.php script.
So to write an run your first test, you need two files :
suite.php must extend ezpTestSuite or ezpDatabaseTestSuite if you need database access for your test which could be the case in many situations. So your suite.php script should look like this :
class MyFirstTestSuite extends ezpDatabaseTestSuite { public function __construct() { parent::__construct(); $this->insertDefaultData= false; $this->sqlFiles= array('my_schema.sql','my_data.sql'); $this->setName( "My Own Test Suite" ); $this->addTestSuite( 'MyClassTest' ); } public static function suite() { return new self(); } }
Ok so, the script above show a suite.php. I've made the choice to show you a suite script extending ezpDatabaseTestSuite because it will be the most interesting. I'll give you more details about databases below. At this point the important line is :
this->addTestSuite( 'MyClassTest' );
This is how to register your test case in the test suite.
On the same base as the suite, the test case will extend the ezpTestCase class or the ezpDatabaseTestCase. As you can gesse, we will use the second one in our example.
So our testCase will look like this :
class MyClassTest extends ezpDatabaseTestCase { /** * @dataProvider provider */ public function testAdd($a, $b, $c) { $this->assertEquals($c, $a + $b); } public function provider() { return array(array(0, 0, 0), array(0, 1, 1), array(1, 0, 1), array(1, 1, 3)); } }
so that's it. I must be frank with you the test I wrote above is taken from the phpunit documentation.
Ok so now we have a suite and a testcase we can run them and sell how it works. I'll have some things to tell you.
Just before lets perform our first test. Create a database on your development server data will be dedicated to your test we'll say it's called testdb. know here's how to run the tests :
php tests/runtest.php -D mysql://dbuser@localhost/testdb extension/mynewextension
Ok I'll make a stop here... the second part will be in the next post...
Timing: | Jan 29 2025 23:48:45 |
Script start | |
Timing: | Jan 29 2025 23:48:45 |
Module start 'layout' | |
Timing: | Jan 29 2025 23:48:45 |
Module start 'content' | |
Timing: | Jan 29 2025 23:48:45 |
Module end 'content' | |
Timing: | Jan 29 2025 23:48:45 |
Script end |
Total runtime | 0.0878 sec |
Peak memory usage | 4,096.0000 KB |
Database Queries | 42 |
Checkpoint | Start (sec) | Duration (sec) | Memory at start (KB) | Memory used (KB) |
---|---|---|---|---|
Script start | 0.0000 | 0.0049 | 588.1563 | 151.2109 |
Module start 'layout' | 0.0049 | 0.0024 | 739.3672 | 36.6563 |
Module start 'content' | 0.0074 | 0.0795 | 776.0234 | 376.0391 |
Module end 'content' | 0.0869 | 0.0009 | 1,152.0625 | 15.3906 |
Script end | 0.0878 | 1,167.4531 |
Accumulator | Duration (sec) | Duration (%) | Count | Average (sec) |
---|---|---|---|---|
Ini load | ||||
Load cache | 0.0030 | 3.4259 | 15 | 0.0002 |
Check MTime | 0.0012 | 1.3906 | 15 | 0.0001 |
Mysql Total | ||||
Database connection | 0.0007 | 0.8051 | 1 | 0.0007 |
Mysqli_queries | 0.0341 | 38.8130 | 42 | 0.0008 |
Looping result | 0.0002 | 0.2628 | 40 | 0.0000 |
Template Total | 0.0593 | 67.5 | 2 | 0.0296 |
Template load | 0.0025 | 2.8292 | 2 | 0.0012 |
Template processing | 0.0568 | 64.6492 | 2 | 0.0284 |
Template load and register function | 0.0001 | 0.1137 | 1 | 0.0001 |
states | ||||
state_id_array | 0.0009 | 1.0008 | 1 | 0.0009 |
state_identifier_array | 0.0006 | 0.7277 | 2 | 0.0003 |
Override | ||||
Cache load | 0.0017 | 1.8917 | 47 | 0.0000 |
Sytem overhead | ||||
Fetch class attribute name | 0.0017 | 1.9628 | 3 | 0.0006 |
class_abstraction | ||||
Instantiating content class attribute | 0.0000 | 0.0090 | 3 | 0.0000 |
General | ||||
dbfile | 0.0016 | 1.7918 | 10 | 0.0002 |
String conversion | 0.0000 | 0.0079 | 4 | 0.0000 |
Note: percentages do not add up to 100% because some accumulators overlap |
Usage | Requested template | Template | Template loaded | Edit | Override |
---|---|---|---|---|---|
1 | node/view/full.tpl | blog_entry/full.tpl | extension/community_design/design/suncana/override/templates/blog_entry/full.tpl | ||
2 | content/datatype/view/ezxmltext.tpl | <No override> | extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tpl | ||
12 | content/datatype/view/ezxmltags/paragraph.tpl | <No override> | extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tpl | ||
4 | content/datatype/view/ezxmltags/header.tpl | <No override> | design/standard/templates/content/datatype/view/ezxmltags/header.tpl | ||
2 | content/datatype/view/ezxmltags/line.tpl | <No override> | design/standard/templates/content/datatype/view/ezxmltags/line.tpl | ||
1 | content/datatype/view/ezxmltags/li.tpl | <No override> | design/standard/templates/content/datatype/view/ezxmltags/li.tpl | ||
1 | content/datatype/view/ezxmltags/ul.tpl | <No override> | design/standard/templates/content/datatype/view/ezxmltags/ul.tpl | ||
5 | content/datatype/view/ezxmltags/literal.tpl | <No override> | extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tpl | ||
1 | content/datatype/view/ezkeyword.tpl | <No override> | extension/community_design/design/suncana/templates/content/datatype/view/ezkeyword.tpl | ||
1 | print_pagelayout.tpl | <No override> | extension/community/design/community/templates/print_pagelayout.tpl | ||
Number of times templates used: 30 Number of unique templates used: 10 |
Time used to render debug report: 0.0001 secs