Questions tagged [phpt]

PHPT scripts are unit and integration tests for the PHP interpreter and user applications.

The PHP Quality Assurance Team uses these scripts to test the engine, verify bug fixes, and maintain a stable build. Everyone is encouraged to write and submit tests to the PHP QA team.

They can also be used to test your own command-line and web applications. You can simulate HTTP requests, file uploads, and terminal input and validate script output and HTTP responses.

Writing Tests

The .phpt file combines section markers, raw text, and PHP code to direct the test runner. Most sections are optional, and most tests will require only a small subset based on the type of test being run. Some of the more common sections include

  • --TEST-- and --DESCRIPTION-- to describe the test
  • --GET--, --POST--, and --PUT-- to simulate an HTTP request
  • --STDIN-- to provide raw input to the script
  • --FILE-- to contain the test script itself
  • --EXPECT-- and --EXPECTREGEX-- to validate the output
  • --CLEAN-- to remove any artifacts created while running the test

Here is a simple example that tests processing standard input using stream_get_contents.

--TEST--
STDIN input
--FILE--
<?php
var_dump(stream_get_contents(STDIN));
?>
--STDIN--
fooBar
use this to input some thing to the php script
--EXPECT--
string(54) "fooBar
use this to input some thing to the php script
"

Running Tests

Tests are run from the command-line using run-tests.php or PEAR's run-tests command.

$ pear run-tests stdin.phpt

Running 1 tests
PASS STDIN input[stdin.phpt]
TOTAL TIME: 00:00
1 PASSED TESTS
0 SKIPPED TESTS

They can also be integrated with PHPUnit using PhptTestCase and PhptTestSuite or run directly.

$ phpunit stdin.phpt

PHPUnit 3.6.12 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 6.25Mb

OK (1 test, 1 assertion)
13 questions
13
votes
2 answers

Integrate PHPT test cases with PHPUnit

How can I get PHPUnit to run my PHPT test cases and integrate the pass/fail status into the overall metrics? I am already aware of how to run these tests using run-phpt from the command line, but I want to run them from within PHPUnit with my other…
edorian
  • 38,542
  • 15
  • 125
  • 143
4
votes
2 answers

How do I get php code coverage to run over phpt test cases?

I have a test environment that runs the component tests for a product. I found that recently it was tough to test and successfully mock php's is_uploaded_file() and move_uploaded_file() but after a lot of searching and research I came upon PHPT.…
Etienne Marais
  • 1,660
  • 1
  • 22
  • 40
4
votes
2 answers

PHPT - Unable to run sample tests that use --POST_RAW--

This .PHPT test completes: (from PHPT docs) File: strtr.phpt --TEST-- strtr() function - basic test for strstr() --FILE-- "hi", "hi"=>"hello", "a"=>"A",…
Ian
  • 11,920
  • 27
  • 61
  • 77
3
votes
1 answer

Why doesn't pear recognize regex in phpt tests?

I'm trying to write a test for a PHPUnit extiontion I'm working on which needs a regex matcher in the expected section of the test. The documentations of PHPT file states that there is a %r flag which can be used to match regex in the --EXPECTF--…
Maher4Ever
  • 1,270
  • 11
  • 26
3
votes
0 answers

PHPT vs Phpunit

What are the advantages and disadvantages of using one of these two types of tests instead of the other? PS: I'm starting a project that isn't a class or a website. I have to choose a test system for files and a file may contain only a class, 2+…
Emanuele Minotto
  • 415
  • 5
  • 10
2
votes
2 answers

Configuration PHPUnit with phpt

I'm totally stucked. At first my code: File sample000.phpt --TEST-- Basic test --DESCRIPTION-- Lowlevel basic test --FILE-- --EXPECT-- Hello World! File PhptTestCase.php
Ralf Albert
  • 139
  • 1
  • 7
2
votes
0 answers

Erroneous failure running PHPT with PHPUnit on PHP 5.4.6

PHPUnit runs regular tests without trouble, but it mistakenly reports this basic PHPT example as failing when it should pass: --TEST-- strtr() function - basic test for strstr() --FILE--
Brad Kent
  • 4,982
  • 3
  • 22
  • 26
1
vote
0 answers

self-updating *.phpt tests with phpunit?

Background I: *.phpt in phpunit I recently read an article about *.phpt support in PhpUnit: https://www.moxio.com/blog/32/start-testing-with-phpt-tests-in-phpunit The big advantages here: Supported out of the box with phpunit, no need for custom…
donquixote
  • 4,877
  • 3
  • 31
  • 54
1
vote
0 answers

How do I format email headers

How do I format the information so when it's displayed in the Email client, it'll show line breaks and other html tags that' i've set in the .php file? //proceed with PHP email. $headers = 'From: '.$user_Email.'' . "\r\n" . 'Reply-To:…
user3717272
1
vote
1 answer

PHP insert won't work

I have been battling with phpMyAdmin insert for sometime now, It doesn't give me an error, yet it won't insert into the database. My code goes like this
1
vote
1 answer

Why do I get "BORKED --CLEAN-- section" using PHPT in PHPUnit?

I have some PHPT tests that are being run in PHPUnit. These tests include a --CLEAN-- section that handles the deletion of files created during the test. Running the PHPT tests at the command line using pear run-tests works fine, but when they are…
shanethehat
  • 15,460
  • 11
  • 57
  • 87
1
vote
1 answer

Is there a way to have multiple phpt tests in one file?

Is there a way to have multiple blocks of --TEST-- ... --FILE-- ... --EXPECT-- ... in one phpt file? Like this: --TEST-- ... --FILE-- ... --EXPECT-- ... --TEST-- ... --FILE-- ... --EXPECT-- ... I found instructions and examples to have many…
Alojz Janez
  • 530
  • 1
  • 3
  • 13
0
votes
1 answer

PHPT avoid constructor and target functions directly

I have the following class: class Validator { public function __construct($file){ $errors = $this->errors($file_array[CSV]['errors']); } public function errors_func($errors){ if($errors != 0){ throw new Exception('Error…
Liam Fell
  • 1,308
  • 3
  • 21
  • 39