Skip to content
This repository has been archived by the owner on Jul 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #14 from herdphp/doc-changes
Browse files Browse the repository at this point in the history
Doc changes
  • Loading branch information
brunoric committed Jun 25, 2017
2 parents 5a597d9 + 7fc8052 commit 58fda8e
Showing 1 changed file with 45 additions and 38 deletions.
83 changes: 45 additions & 38 deletions docs/how_to_PHPT.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ First, let's set up the 'enviroment'.
``` mkdir phpt/username ```


PHPT have some conventions for file name
- {function-name}_basic.phpt
- {function-name}_error.phpt
- {function-name}_variation.phpt
- {ext-name}{number}.phpt
- bug{bug-id}.phpt
PHPT have some conventions for file names
- {function-name}_basic.phpt (Tests for the default behaviour of the function)
- {function-name}\_error\_{number}.phpt (Tests for the errors of the function)
- {function-name}\_variation\_{number}.phpt (Tests for non default behaviours of the functions)
- {ext-name}{number}.phpt (Tests for functions that belongs to an extension)
- bug{bug-id}.phpt (Tests for bugs reported)

- Create a new file to build you first PHPT and check in_array usage
- Create a new file to build you first PHPT, for this tutorial we will write a test to check in_array usage

``` vim phpt/username/in_array_basic.phpt ```

Content of the .phpt file
##Content of the .phpt file
Sample:
```php
--TEST--
Testing correct usage of in_array
Expand Down Expand Up @@ -73,10 +74,12 @@ EXPECTF for the error usage
``` ./phpqa phpt/username ```

Console Output

```
=====================================================================
Running selected tests.
PASS Testing correct usage of in_array [/usr/src/phpt/ivan/in_array_basic.phpt]
PASS Testing correct usage of in_array [/usr/src/phpt/username/in_array_basic.phpt]
=====================================================================
Number of tests : 1 1
Tests skipped : 0 ( 0.0%) --------
Expand All @@ -89,23 +92,23 @@ Time taken : 0 seconds
=====================================================================
```

All good! Now let's test for an error.
All good! Now let's test the function for an error.

First, we need to change the --EXPECT-- tag to --EXPECTF--
this is the correct block name to validate an error and the --TEST-- block description
First, we need to change the **--EXPECT--** tag to **--EXPECTF--**
this is the correct block name to validate an error and the **--TEST--** block description

- Create a file to build a in_array error PHPT

``` vim phpt/username/in_array_error.phpt ```

Change something that will generate an error output, in this example, change $os var to something that is not an array
Change something that will generate an error output, in this example, change _$os_ var to something that is not an array

Content of the .phpt file
```php
--TEST--
Testing wrong usage of in_array
--CREDITS--
Ivan Rosolen <ivanrosolen [at] gmail [dot] com>
Your Name <user [at] domain [dot] com>
#phptestfest2017
--FILE--
<?php
Expand All @@ -126,10 +129,11 @@ Mac Found

You should get an console output like this
```
=====================================================================
Running selected tests.
PASS Testing correct usage of in_array [/usr/src/phpt/ivan/in_array_basic.phpt]
FAIL Testing wrong usage of in_array [/usr/src/phpt/ivan/in_array_error.phpt]
PASS Testing correct usage of in_array [/usr/src/phpt/username/in_array_basic.phpt]
FAIL Testing wrong usage of in_array [/usr/src/phpt/username/in_array_error.phpt]
=====================================================================
Number of tests : 2 2
Tests skipped : 0 ( 0.0%) --------
Expand All @@ -144,14 +148,14 @@ Time taken : 0 seconds
=====================================================================
FAILED TEST SUMMARY
---------------------------------------------------------------------
Testing wrong usage of in_array [/usr/src/phpt/ivan/in_array_error.phpt]
Testing wrong usage of in_array [/usr/src/phpt/username/in_array_error.phpt]
=====================================================================
```

## Now let's see the **log** files

#### .EXP
Result of the --EXPECT-- or --EXPECTF-- block of the phpt file
Result of the **--EXPECT--** or **--EXPECTF--** block of the phpt file

```less phpt/username/in_array_error.exp```

Expand All @@ -161,17 +165,17 @@ Mac Found

#### .OUT

PHP output message
PHP output message for the code that was executed

```less phpt/username/in_array_error.out```

```
Warning: in_array() expects parameter 2 to be array, string given in /usr/src/phpt/ivan/in_array_error.phpt on line 4
Warning: in_array() expects parameter 2 to be array, string given in /usr/src/phpt/username/in_array_error.phpt on line 4
```

### .PHP

--FILE-- Block code
**--FILE--** Block code, the PHP code that was executed during the test

```less phpt/username/in_array_error.phpt```

Expand All @@ -188,57 +192,57 @@ if (in_array('Mac', $os)) {

### .SH

Command used to run the test
Command used to run the test to reproduce what the suit did

```less phpt/username/in_array_error.sh```

```
#!/bin/sh
/usr/src/php/sapi/cli/php -n -c '/usr/src/php/tmp-php.ini' -d "output_handler=" -d "open_basedir=" -d "disable_functions=" -d "output_buffering=Off" -d "error_reporting=32767" -d "display_errors=1" -d "display_startup_errors=1" -d "log_errors=0" -d "html_errors=0" -d "track_errors=1" -d "report_memleaks=1" -d "report_zend_debug=0" -d "docref_root=" -d "docref_ext=.html" -d "error_prepend_string=" -d "error_append_string=" -d "auto_prepend_file=" -d "auto_append_file=" -d "ignore_repeated_errors=0" -d "precision=14" -d "memory_limit=128M" -d "log_errors_max_len=0" -d "opcache.fast_shutdown=0" -d "opcache.file_update_protection=0" -d "extension_dir=/usr/src/php/modules/" -d "zend_extension=/usr/src/php/modules/opcache.so" -d "session.auto_start=0" -d "zlib.output_compression=Off" -d "mbstring.func_overload=0" -f "/usr/src/phpt/ivan/in_array_error.phpt" 2>&1
/usr/src/php/sapi/cli/php -n -c '/usr/src/php/tmp-php.ini' -d "output_handler=" -d "open_basedir=" -d "disable_functions=" -d "output_buffering=Off" -d "error_reporting=32767" -d "display_errors=1" -d "display_startup_errors=1" -d "log_errors=0" -d "html_errors=0" -d "track_errors=1" -d "report_memleaks=1" -d "report_zend_debug=0" -d "docref_root=" -d "docref_ext=.html" -d "error_prepend_string=" -d "error_append_string=" -d "auto_prepend_file=" -d "auto_append_file=" -d "ignore_repeated_errors=0" -d "precision=14" -d "memory_limit=128M" -d "log_errors_max_len=0" -d "opcache.fast_shutdown=0" -d "opcache.file_update_protection=0" -d "extension_dir=/usr/src/php/modules/" -d "zend_extension=/usr/src/php/modules/opcache.so" -d "session.auto_start=0" -d "zlib.output_compression=Off" -d "mbstring.func_overload=0" -f "/usr/src/phpt/username/in_array_error.phpt" 2>&1
```

## Useful **logs**
Those files can help you to see the exact output and what you need to change on your script
Those files can help you to see the exact output and what you need to change on your test

```less phpt/username/in_array_error.log```
```less phpt/username/in_array_error.diff```

```
001+ Warning: in_array() expects parameter 2 to be array, string given in /usr/src/phpt/ivan/in_array_error.phpt on line 4
001+ Warning: in_array() expects parameter 2 to be array, string given in /usr/src/phpt/username/in_array_error.phpt on line 4
001- Mac Found
```

```less phpt/username/in_array_error.diff```
```less phpt/username/in_array_error.log```

```
---- EXPECTED OUTPUT
Mac Found
---- ACTUAL OUTPUT
Warning: in_array() expects parameter 2 to be array, string given in /usr/src/phpt/ivan/in_array_error.phpt on line 4
Warning: in_array() expects parameter 2 to be array, string given in /usr/src/phpt/username/in_array_error.phpt on line 4
---- FAILED
```

## Fixing .phpt file
In this example, you need to change your --EXPECTF-- block to
In this example, you need to change your **--EXPECTF--** block to
```
Warning: in_array() expects parameter 2 to be array, string given in /usr/src/phpt/ivan/in_array_error.phpt on line 4
Warning: in_array() expects parameter 2 to be array, string given in /usr/src/phpt/username/in_array_error.phpt on line 4
```

But, in order to this test be able to run smoothly in any version and the changes easier, let's get rid of the static things like
But, to make this test run smoothly in all version and to make it easier to be changed, let's get rid of the static things like
numbers and types.

Change 'string' to the wildcard '%s', this way the test will be good to validate no only string but the wrong parameter type.
Change 'string' to the wildcard '%s', this way the test will be good to validate not only strings but also the wrong parameter type.
```
Warning: in_array() expects parameter 2 to be array, %s given in /usr/src/phpt/ivan/in_array_error.phpt on line 4
Warning: in_array() expects parameter 2 to be array, %s given in /usr/src/phpt/username/in_array_error.phpt on line 4
```

Let's change the script name and line just to make the output cleaner and changes easier
Let's change the script name and line just to make the output cleaner and future changes easier
```
Warning: in_array() expects parameter 2 to be array, %s given in %s on line %d
```

The wilscards are:
The wildcards are:
- %s - string
- %d - number
- %c - single char
Expand All @@ -255,10 +259,11 @@ Run again
If all works good, the log files will be deleted and you will get something like this

```
=====================================================================
Running selected tests.
PASS Testing correct usage of in_array [/usr/src/phpt/ivan/in_array_basic.phpt]
PASS Testing wrong usage of in_array [/usr/src/phpt/ivan/in_array_error.phpt]
PASS Testing correct usage of in_array [/usr/src/phpt/username/in_array_basic.phpt]
PASS Testing wrong usage of in_array [/usr/src/phpt/username/in_array_error.phpt]
=====================================================================
Number of tests : 2 2
Tests skipped : 0 ( 0.0%) --------
Expand All @@ -269,4 +274,6 @@ Tests passed : 2 (100.0%) (100.0%)
---------------------------------------------------------------------
Time taken : 0 seconds
=====================================================================
```
```

Good testing!

0 comments on commit 58fda8e

Please sign in to comment.