testing php with perl

35
1 Testing PHP with Perl Chr is Shiflett [email protected] Geoffrey Young [email protected]

Upload: gobara-dhan

Post on 13-Apr-2018

244 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

[email protected]

Geoffrey Young

[email protected]

Page 2: Testing Php With Perl

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

Page 3: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 3/35

3

Page 4: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 4/35

4

The Code

Page 5: Testing Php With Perl

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>';

}?>

Page 6: Testing Php With Perl

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#

Page 7: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 7/357

The Test

Page 8: Testing Php With Perl

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');

}?>

Page 9: Testing Php With Perl

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()

Page 10: Testing Php With Perl

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)

Page 11: Testing Php With Perl

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

Page 12: Testing Php With Perl

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

Page 13: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 13/3513

Page 14: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 14/3514

Page 15: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 15/3515

Page 16: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 16/3516

Page 17: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 17/3517

Page 18: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 18/3518

Page 19: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 19/3519

Page 20: Testing Php With Perl

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

Page 21: Testing Php With Perl

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

Page 22: Testing Php With 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

Page 23: Testing Php With Perl

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

Page 24: Testing Php With Perl

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

Page 25: Testing Php With Perl

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

Page 26: Testing Php With Perl

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>

Page 27: Testing Php With Perl

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>

Page 28: Testing Php With Perl

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

Page 29: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 29/35

29

The Gory 1etails

Page 30: Testing Php With Perl

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

Page 31: Testing Php With Perl

7/27/2019 Testing Php With Perl

http://slidepdf.com/reader/full/testing-php-with-perl 31/35

31

Page 32: Testing Php With Perl

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

Page 33: Testing Php With Perl

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?

Page 34: Testing Php With Perl

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

Page 35: Testing Php With Perl

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/