php unit 的使用

安装步骤,参见以下链接:

https://www.cnblogs.com/lxz88/p/6264463.html

1: 新建文件user case   文件名称为:AddTest.php

 

namespace Domain\Test\UseCase;

use Domain\UseCase;
use  Domain\UseCaseInterface;
use InvalidArgumentException;
use PDO;
use Shared\Json;
use Exception;
class AddTest extends UseCase implements UseCaseInterface { public function execute(array $data) { if (!isset($data['test_code'])) { throw new InvalidArgumentException('test_code not provided'); } $query = $this->db->prepare(" INSERT INTO phpUnit_test ( test_name,test_code) VALUES ( :test_name, :test_code )

"); $query->bindParam(':test_name', $data['test_name'], PDO::PARAM_STR); $query->bindParam(':test_code', $data['test_code'], PDO::PARAM_STR); if (!$query->execute()) { throw new Exception(Json::errorInfo($query)); } $new_id = $this->db->lastInsertId(); return [ 'test_id' => $new_id, ]; } }

2.在phpunit /文件下新建 文件名+Test.php

 

 TrainingTest.php文件代码:

<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Domain\Test\UseCase\AddTest;
use Domain\Test\UseCase\GetTestCode;
use Domain\Test\UseCase\DeleteTest;

final class TrainingTest extends TestCase
{
    private static $db;

    // 数据库
    public static function setUpBeforeClass(): void
    {
        require __DIR__ . '/../../../../config/_config.inc.php';
        require __DIR__ . '/db.php';

        self::$db->query("SET @eamic_user = 'PHPUnit';");
    }

    public function testAddtest(): void
    {

        global $DB_connection_admin;

        $test = new AddTest(self::$db);
        $msg = $test->execute([
            'test_code' => 'E' . Date('mdhis'),
            'test_name' => 'PHPUnit' . Date('mdhis'),
        ]);
// 返回的ID应该大于0
        $this->assertGreaterThan(0, $msg['test_id']);
    }

    public function testGetTestcode(): void
    {
        $test = new GetTestCode(self::$db);
        $msg = $test->execute([
            'test_id' => 9
        ]);

        // 应该得到东西
        $this->assertArrayHasKey('test_id', $msg);
        $this->assertArrayHasKey('test_code', $msg);
    }

    public function testDeleteTest(): void
    {

        $test=new DeleteTest(self::$db);
        $msg = $test->execute([
            'test_id' => 24
        ]);
 // 受影响行应该等于1
        $this->assertEquals(1, $msg['rowCount']);
    }

}

终端输入:切换到 test 目录

phpunit 终端命令:

 phpunit-watcher watch  是检查所有的关于文件名+Test.php 的文件。

 phpunit-watcher watch  --filter=TrainingTest   是检查TrainingTest.php 的文件。

终端提示错误:

 

 上图提示在TrainingTest.php文件中57行有错误

 

成功的提示:

新建一个删除的use case,文件名称为DeleteTest.php

<?php

namespace Domain\Test\UseCase;

use PDO;
use Exception;
use Domain\UseCase;
use Domain\UseCaseInterface;
use InvalidArgumentException;
use Shared\Json;

class DeleteTest extends UseCase implements UseCaseInterface
{
    public function execute(array $data)
    {
        if (!isset($data['test_code'])) {
            throw new InvalidArgumentException('test_code not provided');
        }

        $delete = $this->db->prepare("
            DELETE
            FROM phpUnit_test
            WHERE test_code = :test_code;");
        $delete->bindvalue(':test_code', $data['test_code']);
        if (!$delete->execute()) {
            throw new Exception(Json::errorInfo($delete));
        }

        $rowCount = $delete->rowCount();

        return [
            'rowCount' => $rowCount,
            'test_code' => $data['test_code'],
        ];
    }
}

TrainingTest.php 

<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Domain\Test\UseCase\AddTest;
use Domain\Test\UseCase\DeleteTest;

final class TrainingTest extends TestCase
{
    private static $db;

    // 数据库
    public static function setUpBeforeClass(): void
    {
        require __DIR__ . '/../../../../config/_config.inc.php';
        require __DIR__ . '/db.php';

        self::$db->query("SET @eamic_user = 'PHPUnit';");
    }

    public function testAddtest(): void
    {
        // When I create a test
        $addTest = new AddTest(self::$db);
        $test_code = 'test' . Date('mdhis');
        $msg = $addTest->execute([
            'test_code' => $test_code,
            'test_name' => 'PHPUnit' . Date('mdhis'),
        ]);

        // Then I check that the wo_id is numeric 
        // 返回的应该是一个数字
        $this->assertIsNumeric($msg['test_id']);
        // And its value should be greater than 0
        // 返回的ID应该大于0
        $this->assertGreaterThan(0, $msg['test_id']);

        // When I delete a test
        // 当我删除一个test
        $deleteTest = new DeleteTest(self::$db);
        $msg = $deleteTest->execute([
            'test_code' => $test_code
        ]);
        // Then I should have a rowCount equal to 1
        // 受影响行应该等于1
        $this->assertEquals(1, $msg['rowCount']);
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2023-02-02 18:05  王越666  阅读(60)  评论(0编辑  收藏  举报