Cours du 15/11 Couche application Perl, php, javascript ftp.
Testing Php With Perl
-
Upload
gobara-dhan -
Category
Documents
-
view
236 -
download
0
Transcript of Testing Php With Perl
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 1/35
1
Testing PHP with PerlChris Shiflett
Geoffrey Young
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 2/35
2
PHP and Perl?
• Testing a basic PHP application
• Using the Apache-Test framework
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 3/35
3
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 4/35
4
The Code
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 5/35
5
admin/add.php
<?php
include '../functions.inc';
...
if (add_user($_POST['username'], $_POST['password']))
{
echo '<p>User Added</p>';
echo '<p><a href="/admin/">Admin Home</a></p>';
}?>
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 6/35
6
The Testing Paradigm
• dopted from the time!tested Perlmythology "sic#
• plan() the number of tests
• call ok() for each test you plan
– or is()$ or like()$ or unlike()$ etc%%%
• &ramework keeps track of the resultsand writes out the report
• Test nything Protocol "TP#
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 7/357
The Test
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 8/358
add_user.php
<?phprequire 'test-more.php';
require "{$_SERVER['DOCUMENT_ROOT']}/functions.inc";
plan(2);
{ # no user or password
$rc = add_user('', '');
ok (!$rc, 'no user/pass fails');
}
{
# some generic user/password $rc = add_user('user', 'password');
ok ($rc, 'user/pass successfully added');
# cleanup
delete_user('user');
}?>
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 9/359
test-more.php
• utomagically generated
• 'nterface into Apache-Test
•
Pro(ides simple$ intuiti(e functions–ok()
–is()
–like()
• Takes care of bookkeeping
–plan()
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 10/3510
ok()
• Used for boolean comparisons
ok($foo == $bar, '$foo equals $bar');
• Gi(es some diagnostic output on failurenot ok 1 - no user/pass fails
# Failed test (add_user.php at line 10)
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 11/3511
Goodness
• )o tests in application code
• Simple interface
•
*epeatable– tests are self!contained
• )o Perl in(ol(ed
– Chris particularly likes this aspect
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 12/3512
Testing 'deology
• good testing en(ironment shouldpro(ide
– tools to make writing tests simple
– a self!contained and pristine en(ironment
– test automation
• +asically do e(erything for you e,ceptwrite your tests
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 13/3513
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 14/3514
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 15/3515
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 16/3516
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 17/3517
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 18/3518
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 19/3519
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 20/3520
+ehold the Power of Perl
• How did we do it?
• Apache-Test
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 21/35
21
Apache-Test
• &ramework for testing pache!basedapplication components
• Part of the httpd-test S& pro-ect
• Pro(ides tools to make testing pachesimple
•
.ritten in Perl
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 22/35
22
pache &oo
• pache needs a basic configuration toser(ice re/uests– ServerRoot
– DocumentRoot– ErrorLog
– Listen
•
Apache-Test 0intuits0 these andcreates its own httpd.conf
• Uses an httpd binary you specify
–
patience$ young grasshopper
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 23/35
23
Cross Pollination
• Apache-Test pro(ides a defaultphp.ini
– php.ini-recommended
• lso pro(ides test-more.php
<?php require 'test-more.php'; ?>
– modified include_path
• &ertile soil so your PHP can grow
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 24/35
24
ltering the 1efaults
• httpd.conf and php.ini areautogenerated
– don2t touch them
• Supplement default httpd.conf and php.ini with custom configurations
•
Create t/conf/extra.conf.in
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 25/35
25
extra.conf.in
• Same directi(es as httpd.conf
• Pulled into httpd.conf (ia Include
•
llow for some fancy (ariablesubstitutions
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 26/35
26
extra.conf.in
AddType application/x-httpd-php .php
DirectoryIndex index.php index.html
<IfModule @PHP_MODULE@>
php_flag register_globals On
</IfModule>
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 27/35
27
extra.conf
AddType application/x-httpd-php .php
DirectoryIndex index.php index.html
<IfModule mod_php5.c>
php_flag register_globals On
</IfModule>
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 28/35
28
So &ar%%%
• .e ha(e
– PHP test script "add_user.php#
– test library "test-more.php#
– httpd.conf
– php.ini
– local o(errides
•.e still need– a client to call the PHP script
– a running ser(er
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 29/35
29
The Gory 1etails
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 30/35
30
The Gory 1etails
• Create PHP scripts ast/response/TestFunc/add_user.php
• Apache-Test will automagically createa client script that calls add_user.php
t/func/add_user.t
•
make test will– run add_user.t
– which will re/uest add_user.php
–
which will send data to Apache-Test
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 31/35
31
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 32/35
32
Makefile.PL
• .e still need to create the Makefile
– so make test works
•
.e also need to choose an pacheinstallation
• Taken care of in one single step
perl Makefile.PL -httpd /path/to/httpd
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 33/35
33
Cool3
• The glory will sink in tomorrow
– we hope
• PHP de(elopment will ne(er be thesame
• See what happens when a Perl guy anda PHP guy start drinking?
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 34/35
34
Code
• ll the code from this presentation canbe found here
http://www.modperlcookbook.org/~geoff/slides/ApacheCon/2004/perl-php-test.tar.gz
• +e sure to read the *4154 and')ST66 docs
7/27/2019 Testing Php With Perl
http://slidepdf.com/reader/full/testing-php-with-perl 35/35
+rought To You +y%%%
http://shiflett.org/
http://modperlcookbook.org/~geoff/