Testing PHP with Perl Chris Shiflett shiflett@php.net Geoffrey - - PowerPoint PPT Presentation

testing php with perl
SMART_READER_LITE
LIVE PREVIEW

Testing PHP with Perl Chris Shiflett shiflett@php.net Geoffrey - - PowerPoint PPT Presentation

Testing PHP with Perl Chris Shiflett shiflett@php.net Geoffrey Young geoff@modperlcookbook.org 1 Why Perl? Testing has become very fashionable within the Perl community Perl testing tools are mature Some of these tools were


slide-1
SLIDE 1

1

Testing PHP with Perl

Chris Shiflett

shiflett@php.net

Geoffrey Young

geoff@modperlcookbook.org

slide-2
SLIDE 2

2

Why Perl?

  • Testing has become very fashionable

within the Perl community

  • Perl testing tools are mature
  • Some of these tools were designed for

Apache

  • PHP has strong Apache roots
  • Ergo, Perl can help test PHP

– unless you're using IIS, in which case you have bigger problems than testing

slide-3
SLIDE 3

3

Really!

  • The Perl testing community has put

lots of work into our tools to

– make automating tests easy – make writing tests intuitive

  • The Perl-centric Apache community

has brought the goodness to Apache

  • There is no reason why PHP can't take

advantage of both

slide-4
SLIDE 4

4

Building Apache + PHP

$ tar -xvzf apache_1.3.31.tar.gz $ tar -xvzf php-5.0.2.tar.gz $ cd apache_1.3.31 $ ./configure $ cd ../php-5.0.2 $ ./configure --prefix=/usr/local/php \

  • -with-apache=../apache_1.3.31 --with-pear \
  • -with-gd --with-mysql=/usr/local/mysql \
  • -enable-sockets --with-zlib-dir=/usr/include

$ make $ sudo make install $ cd ../apache_1.3.31 $ ./configure --prefix=/usr/local/apache \

  • -activate-module=src/modules/php5/libphp5.a \
  • -enable-module=most --enable-shared=max

$ make $ sudo make install

slide-5
SLIDE 5

5

Building Apache + PHP

$ tar -xvzf apache_1.3.31.tar.gz $ tar -xvzf php-5.0.2.tar.gz $ cd apache_1.3.31 $ ./configure $ cd ../php-5.0.2 $ ./configure --prefix=/usr/local/php \

  • -with-apache=../apache_1.3.31 --with-pear \
  • -with-gd --with-mysql=/usr/local/mysql \
  • -enable-sockets --with-zlib-dir=/usr/include

$ make $ sudo make install $ cd ../apache_1.3.31 $ ./configure --prefix=/usr/local/apache \

  • -activate-module=src/modules/php5/libphp5.a \
  • -enable-module=most --enable-shared=max

$ make $ sudo make install

slide-6
SLIDE 6

6

Getting Apache-Test

$ cvs -d :pserver:anoncvs@cvs.apache.org:/home/cvspublic login $ cvs -d :pserver:anoncvs@cvs.apache.org:/home/cvspublic co httpd-test $ cd httpd-test/perl-framework/Apache-Test $ perl Makefile.PL $ make $ sudo make install

slide-7
SLIDE 7

7

Test Automation

  • Perl distributions typically start with a

Makefile.PL

$ perl Makefile.PL

  • This generates a Makefile with a bunch
  • f useful make targets

$ make $ sudo make install $ make test

slide-8
SLIDE 8

8

make test

  • The test target is the basis for Perl

testing

  • make test will

– search for *.t files under t/ – execute them – collect results – write out a final report

slide-9
SLIDE 9

9

So What?

  • "We don't care about Perl. How does

this help us?" you ask...

  • Enter Apache-Test
slide-10
SLIDE 10

10

Apache-Test

  • Framework for testing Apache-based

application components

  • Gives you a self-contained, pristine

Apache environment

  • Provides HTTP-centric testing tools for

client-side tests

  • Provides PHP-centric testing tools for

server-side tests

– if you use the libraries from current CVS that I added for this talk

slide-11
SLIDE 11

11

The test Target

  • With Apache-Test, make test will

– configure Apache – start Apache – execute the tests – issue the report – stop Apache

  • All you need to do is write the tests

– and get Apache-Test working

slide-12
SLIDE 12

12

Integration Mechanics

  • 1. Generate the test harness
  • 2. Configure Apache
slide-13
SLIDE 13

13

Step 1 - The Test Harness

  • Generally starts from Makefile.PL
  • There are other ways as well
slide-14
SLIDE 14

14

Makefile.PL

use Apache::TestMM qw(test clean); use Apache::TestRunPHP (); # configure tests based on incoming arguments Apache::TestMM::filter_args(); # generate the test harness (t/TEST) Apache::TestRunPHP->generate_script();

slide-15
SLIDE 15

15

Step 1 - The Test Harness

  • Don't get bogged down with

Makefile.PL details

slide-16
SLIDE 16

16

Step 1 - The Test Harness

  • Don't get bogged down with

Makefile.PL details

  • Lather, Rinse, Repeat
slide-17
SLIDE 17

17

Integration Mechanics

  • 1. Generate the test harness
  • 2. Configure Apache
slide-18
SLIDE 18

18

Step 2 - Configure Apache

  • Apache needs a basic configuration to

service requests

– ServerRoot – DocumentRoot – ErrorLog – Listen

  • Apache-Test "intuits" these
  • But uses the exact same httpd binary
slide-19
SLIDE 19

19

Apache-Test Intuition

  • Apache-Test provides server defaults

– ServerRoot t/ – DocumentRoot t/htdocs – ErrorLog t/logs/error_log – Listen 8529

  • Also provides an initial index.html

http://localhost:8529/index.html

  • You will need some PHP stuff
slide-20
SLIDE 20

20

Adding to the Default Config

  • Supplement default httpd.conf with

custom configurations

  • Create t/conf/extra.conf.in
slide-21
SLIDE 21

21

extra.conf.in

  • Same directives as httpd.conf
  • Pulled into httpd.conf via Include
  • Allow for some fancy variable

substitutions

slide-22
SLIDE 22

22

Create the Configuration

  • We will be doing PHP specific stuff
  • Let's add some standard PHP

configuration directives

slide-23
SLIDE 23

23

extra.conf.in

AddType application/x-httpd-php .php DirectoryIndex index.php index.html <IfModule @PHP_MODULE@> php_flag display_errors Off php_flag log_errors On php_value error_log @ServerRoot@/logs/php_errors </IfModule> <Files ~ "\.(inc|sqlite)"> Order allow,deny Deny from all </Files>

slide-24
SLIDE 24

24

Integration Mechanics

  • 1. Generate the test harness
  • 2. Configure Apache
  • 3. Write the tests
  • 4. Install the application into our tree
slide-25
SLIDE 25

25

The t/ Directory

  • t/ is the ServerRoot

– t/htdocs – t/cgi-bin – t/logs

  • Tests live in t/
slide-26
SLIDE 26

26

Admin Application

– t/htdocs/admin/index.php – t/htdocs/admin/add.php – t/htdocs/admin/delete.php

slide-27
SLIDE 27

27

Let's Test This Puppy

  • The old way of testing an application

was to fire up a browser

  • Browser-based testing is so pre-bubble
  • Apache-Test gives you a server just

waiting to receive requests

  • Perl provides lots of tools to automate

the client-side

  • Apache-Test provides magic for

automated server-side PHP testing

slide-28
SLIDE 28

28

Anatomy of a Test

  • In the Perl testing world everyone

does testing essentially the same way

  • create t/foo.t
  • plan() the number of tests
  • call ok() for each test you plan

– where ok() is any one of a number of different functions

  • All the rest is up to you
slide-29
SLIDE 29

29

Perl versus PHP

  • Apache-Test is a Perl tool

– uses Perl to call test scripts in t/ – t/ scripts act as a browser

  • PHP support is a bit different

– still uses Perl scripts as a browser – additional clients are autogenerated to call PHP server-side tests

slide-30
SLIDE 30

30

Client versus Server

  • Let's start with some client-side

examples

  • Show the cool server-side PHP stuff

you really care about soon

  • It's important to see the difference
slide-31
SLIDE 31

31

t/admin.t

use Apache::TestRequest; use Test::More; plan tests => 3; my $uri = '/admin/'; { my $response = GET $uri; is ($response->code, 401, "no valid password entry"); }

slide-32
SLIDE 32

32

Apache::TestRequest

  • Provides a basic HTTP interface like

PEAR::HTTP_Client

– GET() – POST() – HEAD() – etc...

  • Functions are self-aware

– know which server and port to talk to

slide-33
SLIDE 33

33

Test::More

  • Interface into the Perl testing harness
  • Provides simple functions so you don't

need to print 1..2\n1 ok\n2 ok\n –ok() –is() –like()

  • Takes care of bookkeeping

–plan()

slide-34
SLIDE 34

34

  • k()
  • Used for simple comparisons
  • k($foo == $bar, '$foo equals $bar')
  • Gives little diagnostic output on failure

not ok 1 - $foo equal to $bar # Failed test (test.pl at line 8)

slide-35
SLIDE 35

35

is()

  • Almost the same as ok()

is($foo, $bar, '$foo equals $bar')

  • Gives better diagnostic output on

failure

not ok 1 - $foo is $bar # Failed test (test.pl at line 8) # got: '1' # expected: '2'

slide-36
SLIDE 36

36

like()

  • Regular expression matching

like($foo, qr/foo/, '$foo matches /foo/)

not ok 1 - $foo matches /foo/ # Failed test (test.pl at line 7) # 'bar' # doesn't match '(?-xism:foo)'

slide-37
SLIDE 37

37

t/admin.t

{ my $response = GET $uri, username => 'geoff', password => 'foo'; is ($response->code, 401, "password mismatch"); } { my $response = GET $uri, username => 'admin', password => 'adminpass'; is ($response->code, 200, "admin allowed to proceed"); }

slide-38
SLIDE 38

38

t/admin.t

{ my $response = GET $uri, username => 'geoff', password => 'foo'; is ($response->code, 401, "password mismatch"); } { my $response = GET $uri, username => 'admin', password => 'adminpass'; is ($response->code, 200, "admin allowed to proceed"); }

slide-39
SLIDE 39

39

Drumroll...

  • And now, what you really came here to

see...

slide-40
SLIDE 40

40

test_more.inc

  • Apache-Test provides test_more.inc
  • test_more.inc is PHP's Test::More

– ok() – is() – like() – plan() – etc

  • include_path is adjusted

<?php require 'test_more.inc'; ?>

slide-41
SLIDE 41

41

PHP Server-Side Tests

  • You can use test_more.inc functions

to communicate with the Perl test harness

  • How?
slide-42
SLIDE 42

42

PHP Mechanics

  • Create PHP scripts as

t/response/TestFoo/bar.php

  • Apache-Test will automagically create a

client-side Perl script that calls bar.php

t/foo/bar.t

  • make test will

– run bar.t – which will request bar.php – which will send data to the test harness

slide-43
SLIDE 43

43

admin/index.php

<?php include '../functions.inc'; if (!check_admin($user, $password)) { echo '<p>Access Denied</p>'; exit; } ?>

slide-44
SLIDE 44

44

check_admin()

function check_admin($user, $pass) { if ($user == 'admin' && $pass == 'adminpass') { return true; } header('HTTP/1.0 401 Unauthorized'); header('WWW-Authenticate: Basic realm="foo"'); return false; }

slide-45
SLIDE 45

45

05check_admin.php

<?php require 'test_more.inc'; require "{$_SERVER['DOCUMENT_ROOT']}/ functions.inc"; plan(2); { $rc = check_admin('user', 'password');

  • k (!$rc, 'non-admin user/pass fails');

} { $rc = check_admin('admin', 'adminpass');

  • k ($rc, 'admin user/pass found');

} ?>

slide-46
SLIDE 46

46

04encrypt_password.php

require 'test_more.inc'; require "{$_SERVER['DOCUMENT_ROOT']}/functions.inc"; plan(3); { $password = 'funkyfunky'; $newpass = encrypt_password($password); # the returned password should be different isnt ($newpass, $password, 'password is at least different');

slide-47
SLIDE 47

47

04encrypt_password.php

# and that it has basic md5 characteristics, # such as being 32 characters long is (strlen($newpass), 32, 'password is a proper 32 characters'); # and all 32 characters must be within hex range like ($newpass, '/^[0-9a-fA-F]{32}$/', 'password consists of only hex characters'); } ?>

slide-48
SLIDE 48

48

04encrypt_password.php

# and that it has basic md5 characteristics, # such as being 32 characters long is (strlen($newpass), 32, 'password is a proper 32 characters'); # and all 32 characters must be within hex range like ($newpass, '/^[0-9a-fA-F]{32}$/', 'password consists of only hex characters'); } ?>

slide-49
SLIDE 49

49

Advantages

  • PHP code tested in real environment
  • Self-contained environment
  • Simple tools to lower the testing

barrier

  • No tests in your application
slide-50
SLIDE 50

50

Where is Apache-Test?

  • mod_perl 2.0
  • CPAN
  • httpd-test project

– http://httpd.apache.org/test/ – test-dev@httpd.apache.org

slide-51
SLIDE 51

51

More Information

  • perl.com
  • http://www.perl.com/pub/a/2003/05/22/testing.html
  • Apache-Test tutorial
  • http://perl.apache.org/docs/general/testing/testing.html
  • Apache-Test manpages

$ man Apache::TestRunPHP

  • mod_perl Developer's Cookbook

– http://www.modperlcookbook.org/

slide-52
SLIDE 52

52

Slides

  • These slides freely available at some long

URL you will never remember…

http://www.modperlcookbook.org/~geoff/slides/nyphp