没有不值得去解决的问题,也没有不值得去学习的技术!

Implementing a Delete Cache Data Test Mutation With Laravel 6, LightHouse 5, and PHPUnit

In the program implementation, first request the API to generate a cache identifier. Then request the API again to delete the cache identifier.

1. In the program implementation, first request the API to generate a cache identifier. Then request the API again to delete the cache identifier. See Figure 1.

In the program implementation, first request the API to generate a cache identifier. Then request the API again to delete the cache identifier.
Figure 1

2. To pass the test, the prerequisite is that an explicit cache identifier must exist to serve as the parameter for the delete request.

3. Reference URL: https://learnku.com/laravel/t/22690 . Building a Laravel REST API using TDD. When testing the delete route, it first directly operates the database to generate a record, and then calls the HTTP API to test the delete operation.

PHP

// 测试删除路由
public function testDelete(){
    $token = $this->authenticate();
    $recipe = Recipe::create([
        'title' => 'Jollof Rice',
        'procedure' => 'Parboil rice, get pepper and mix, and some spice and serve!'
    ]);
    $this->user->recipes()->save($recipe);
    $response = $this->withHeaders([
        'Authorization' => 'Bearer '. $token,
    ])->json('POST',route('recipe.delete',['recipe' => $recipe->id]));
    $response->assertStatus(200);
    // 断言没有食谱
    $this->assertEquals(0,$this->user->recipes()->count());
}


4. Therefore, I decided to apply the same design. First directly operate the cache to generate the corresponding cache identifier, and then execute the HTTP API request for testing.

PHP


    public function testDeleteThemeEditorCode(): void
    {
        $random = Str::random(36);
        $value = [
            'theme_id' => 'vogue'
        ];
        Cache::tags([ThemeEditorResolver::TAG_THEME_EDITOR, ThemeEditorResolver::TAG_THEME_EDITOR_CODE])->put($random, $value, 86400);

        $response = $this->graphQL('
            mutation DeleteThemeEditorCode($themeEditorCode: String!) {
                onlineStoreThemeEditorCodeDelete(themeEditorCode: $themeEditorCode) {
                    deletedThemeEditorCode
                }
            }
        ', [
            'themeEditorCode' => $random,
        ]);

        $response->assertJson(
            [
                'data' => [
                    'onlineStoreThemeEditorCodeDelete' => [
                        'deletedThemeEditorCode' => $random
                    ],
                ],
            ]
        );
    }


5. Run the test, and the test passes. See Figure 2.

Run the test, and the test passes
Figure 2
Plaintext

PS E:\wwwroot\object> .\vendor\bin\phpunit .\Modules\ThemeStore\Tests\Functional\GraphQl\ThemeEditorGraphQlApiTest.php
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 1.31 seconds, Memory: 72.00 MB

OK (2 tests, 4 assertions)

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系