behat 的使用

 

前提:api/v1/test/behat.yml文件下 ,写一个-LendingContext

 

1:建立文件

2:编写代码:这是一个删除的和新增的代码:这里我是分别写了lendings_details_delete.php 和lendings_details_post.php

lendings_details_post.php

<?php

if ($user_right[70][0] < 2) {
    Flight::forbidden();
} elseif (!isset($id)) {
    Flight::badRequest('id');
} elseif (!isset($data['asset_id'])) {
    Flight::badRequest('asset_id');
} elseif (!isset($data['remarks'])) {
    Flight::badRequest('remarks');
}

$query = $DB->prepare("
    INSERT INTO asset_lending_details(lending_id, asset_id, remarks, added_by, added_time)
    VALUES(:lending_id,:asset_id,:remarks, @eamic_user,:added_time)
 ");

$query->bindvalue(':lending_id', $id);
$query->bindvalue(':asset_id', $data['asset_id']);
$query->bindvalue(':remarks', $data['remarks']);
$query->bindvalue(':added_time', $tmp_now);
if (!$query->execute()) {
    Flight::error(new Exception(errorInfo($query)));
}

$new_id = $DB->lastInsertId();

Flight::json([
    'data' => [
        'id' => (int) $new_id,
        'time' => $tmp_now,
        'user' => $o_user->employee,

    ],
]);
lendings_details_delete.php
<?php

if ($user_right[70][0] < 2) {
    Flight::forbidden();
} elseif (!isset($line)) {
    Flight::badRequest('line');
}

$query = $DB->prepare("
    DELETE asset_lending_details
    FROM asset_lending_details
    LEFT JOIN asset_lending ON asset_lending.id = asset_lending_details.lending_id
    WHERE asset_lending_details.id = :line
    AND asset_lending.status < 6
");
$query->bindParam(':line', $line, PDO::PARAM_INT);
if (!$query->execute()) {
    Flight::error(new Exception(errorInfo($query)));
}

Flight::json([
    'data' => [
        'rowCount' => $query->rowCount(),
        'deleted' => $tmp_now,
    ],
]);

3:api/v1/test/features/bootstrap文件夹下建立LendingContext.php文件 ,这里就复制一个.PHP 的文件,但是记住改类名。

 

api/v1/test/features/文件夹下新建一个lending.feature,写入红框中的语句

mac 终端切换至test文件夹下,并终端执行以下命令,lending 其实就是文件名称

 ../vendor/bin/behat --tags  lending

 

 复制终端中的新增方法:进行传参数

 删除方法:

代码可复制:

新增

/**
     * @When I add a new lending detail
     */
    public function iAddANewLendingDetail()
    {
        $parameters = [
            'asset_id' => '11',
            'remarks' => 'test',
        ];
        $options = json_encode($parameters, JSON_UNESCAPED_UNICODE);
        $data = [
            'body' => $options,
            'headers' => $this->headers,
        ];

        $this->user->response = $this->client->post(
            'assets/lendings/' . $this->item->id . '/details',
            $data
        );

        return true;
    }

删除详情

/**
     * @When I delete this lending detail
     */
    public function iDeleteThisLendingDetail()
    {
        $parameters = [
            'name' => 'aadcsdfsda',
        ];
        $options = json_encode($parameters, JSON_UNESCAPED_UNICODE);
        $data = [
            'body' => $options,
            'headers' => $this->headers,
        ];
        $this->user->response = $this->client->delete(
            'assets/lendings/' .
                $this->item->id .
                '/details/' .
                $this->detail->id,
            $data
        );

        return true;
    }

切换至test目录继续终端执行

cd /Users/bingyan/htdocs/eamic/api/v1/test 
 ../vendor/bin/behat --tags  lending

按照报错一直解决,并用命令进行测试,知道一直没有报错即可成功。全是绿色的就是对的。

 

posted @ 2022-06-27 13:43  王越666  阅读(44)  评论(0编辑  收藏  举报