1
0
Fork
You've already forked put
0
PHP Unit Testing for Minimalists
  • PHP 99.7%
  • Makefile 0.3%
2025年12月25日 15:27:15 +01:00
.phan more strict phan config 2019年12月15日 23:19:22 +01:00
bin catch Throwable instead of Exception 2025年05月02日 10:09:41 +02:00
src add TestCase::assertNotFalse 2025年12月25日 15:27:15 +01:00
tests source formatting 2025年06月24日 15:45:14 +02:00
.gitignore update style 2022年04月02日 22:07:34 +02:00
.php-cs-fixer.dist.php Revert "source formatting" 2025年06月24日 15:44:18 +02:00
CHANGES.md update CHANGES 2025年05月02日 10:10:23 +02:00
composer.json require PHP >= 7.4 2025年05月02日 10:08:20 +02:00
composer.lock require PHP >= 7.4 2025年05月02日 10:08:20 +02:00
LICENSE add LICENSE file 2019年12月15日 23:36:43 +01:00
Makefile fix Makefile 2023年01月06日 10:41:31 +01:00
psalm.xml.dist update (C), php-cs-fixer file 2022年11月06日 21:26:50 +01:00
README.md update README to point to codeberg.org 2024年11月13日 17:37:29 +01:00
TODO.md implement assertEquals 2019年12月07日 12:12:20 +01:00

PHP Unit Testing for Minimalists

A simple PHPUnit replacement with support for PHP >= 5.4. It implements the most common PHPUnit assertions. In addition, it supports PHP Code Coverage using pcov on PHP >= 7. For pcov to work, make sure you enable it.

NOTE: this is NOT meant as a (full) replacement for phpunit/phpunit! It is a barebones PHPUnit clone that you can use to run your unit tests in minimalist environments when you don't need the full feature set of PHPUnit.

We support a few common assertions, and the recommended way to test exceptions, nothing more. No mocks, and no PHPUnit annotations. Those should not be used anyway as that reduces the effectiveness of static code analysis.

Assertions

The following assertions are implemented, more can be easily added:

  • addToAssertionCount()
  • assertArrayHasKey()
  • assertContains()
  • assertCount()
  • assertEmpty()
  • assertEquals()
  • assertFalse()
  • assertGreaterThan()
  • assertGreaterThanOrEqual()
  • assertInstanceOf()
  • assertInternalType()
  • assertNotContains()
  • assertNotEmpty()
  • assertNotEquals()
  • assertNotNull()
  • assertNotSame()
  • assertNull()
  • assertRegexp()
  • assertMatchesRegularExpression()
  • assertSame()
  • assertStringContainsString()
  • assertStringStartsWith()
  • assertTrue()
  • fail()

Exceptions

For testing exceptions we implemented:

  • expectException()
  • expectExceptionMessage()
  • expectExceptionCode()

For example:

$this->expectException('RangeException');

Or when using PHP >= 5.5:

$this->expectException(RangeException::class);

Using

This project requires PHP >= 5.4. It has no other dependencies, but optionally ext-pcov for code coverage reporting.

In your project's composer.json:

"repositories": [
 {
 "type": "vcs",
 "url": "https://codeberg.org/fkooman/put"
 }
],
...
"require-dev": {
 "fkooman/put": "^0"
},

Do not forget to run composer update.

Writing Tests

Writing tests is exactly the same as for PHPUnit. Put them in the tests/ directory of your project. A simple example:

<?php
namespace my\app;
use DateTime;
use PHPUnit\Framework\TestCase;
class SimpleTest extends TestCase
{
 public function testDate()
 {
 $dateTime = new DateTime('2019年01月01日 08:00:00');
 $this->assertSame('2019年01月01日', $dateTime->format('Y-m-d'));
 }
}

This makes it easy to support both phpunit and put with the same test suite.

Running Tests

Assuming you added put to your composer.json, you can simply run it:

$ vendor/bin/put
.............
#Tests : 13
#Assertions : 14
$ echo $?
0

See vendor/bin/put --help for a description of the configuration options.

As an example, with the defaults:

$ vendor/bin/put --bootstrap vendor/autoload.php --suffix Test.php tests

To run code coverage reporting, using the defaults:

$ vendor/bin/put --coverage report.html

You can view the report.html in your web browser. If this does not work, make sure you have the pcov extension installed and enabled, and pcov.enabled set to 1.

Test Failure

$ vendor/bin/put
....E.
#Tests : 6
#Assertions : 6
#Errors : 1
**** ERROR ****
[fkooman\Put\Exception\AssertSameException]
--- EXPECTED ---
'2019年01月02日'
--- ACTUAL ---
'2019年01月01日'
--- END ---
#0 /home/fkooman/Projects/put/tests/SimpleTest.php(16): PHPUnit\Framework\TestCase->assertSame('2019年01月02日', '2019年01月01日')
#1 /usr/share/php/fkooman/Put/TestCase.php(288): fkooman\Put\SimpleTest->testDate()
#2 /usr/share/php/fkooman/Put/Put.php(85): PHPUnit\Framework\TestCase->run()
#3 /usr/bin/put(22): fkooman\Put\Put->run(Array, '/home/fkooman/P...')
#4 {main}
$ echo $?
1

From the trace your can determine where it went wrong. The output is not as sophisticated as the PHPUnit output, but hey!