DEV Community

loading...

Create the first phpunit test

pcreem profile image pcreem ・1 min read

Note: phpunit9 doesn't support php8^, it compatible with php7.4

$./vendor/bin/phpunit --version
PHPUnit 9.0.0 by Sebastian Bergmann and contributors. 
Enter fullscreen mode Exit fullscreen mode
  • $code composer.json
{
    "autoload": {
        "psr-4": {
            "App\\":"App/"
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^9"
    }
}
Enter fullscreen mode Exit fullscreen mode
  • $code App/Data/Database.php
<?php
namespace App\Data;

class Database {
    public function sayHi()
    {
        return 'create database\n';
    }
} 
Enter fullscreen mode Exit fullscreen mode
  • $code tests/DatabaseTest.php
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use App\Data\Database;

final class DatabaseTest extends TestCase
{
    public function testCanCreateDatabase(): void
    {
        $database = new Database();
        $this->assertSame('create database\n', $database->sayHi());
    }
} 
Enter fullscreen mode Exit fullscreen mode
  • $./vendor/bin/phpunit --testdox tests

output

PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

Database
 ✔ Can create database
the init

Time: 00:00.006, Memory: 4.00 MB

OK (1 test, 1 assertion)
Enter fullscreen mode Exit fullscreen mode

repo: https://github.com/pcreem/theFirstPhpUnitTest

Discussion (0)

pic
Editor guide