satopian / Petit_Note

「お絵かき掲示板PHPスクリプトPetit Note」 for PaintBBS NEO, tegaki.js, ChickenPaint, and Klecks. (PHP5.6 - PHP8.3)
https://paintbbs.sakura.ne.jp/
Other
25 stars 2 forks source link

test: add unit tests with phpunit #11

Closed minchao closed 2 years ago

minchao commented 2 years ago

THIS PR IS WORKING IN THE PROCESS.

Running PHPUnit in local development environment

Install PHPUnit 9.5 by composer (requires PHP 7.3+):

composer install

Run tests:

$ ./vendor/bin/phpunit
PHPUnit 9.5.20

Runtime:       PHP 8.1.6
Configuration: /Petit_Note/phpunit.xml

..............                                                    14 / 14 (100%)

Time: 00:00.005, Memory: 6.00 MB

[Optional] Run tests with code coverage report (requires Xdebug extension):

$ ./vendor/bin/phpunit --coverage-html code-coverage-report
PHPUnit 9.5.20

Runtime:       PHP 8.1.6 with Xdebug 3.1.4
Configuration: /Petit_Note/phpunit.xml

..............                                                    14 / 14 (100%)

Time: 00:00.059, Memory: 8.00 MB

OK (14 tests, 14 assertions)

Generating code coverage report in HTML format ... done [00:00.121]

You will find the HTML report in the code-coverage-report folder.

Open the code-coverage-report/index.html file with a browser and you will see:

Screen Shot 2022-06-06 at 23 04 51

Click functions.php link for more detailed report.

The green lines mean this line was executed in a test.

Screen Shot 2022-06-06 at 23 05 37

References

satopian commented 2 years ago

I installed ubuntu somehow and now I can enter PHPUnit commands. But I'm still not sure about Linux commands. It will take a long time to understand. I hope I can check it with GitHub action for the time being.

minchao commented 2 years ago

@satopian This is the convenience brought by Continuous Integration (GitHub Actions).

satopian commented 2 years ago

@minchao

./vendor/bin/phpunit
PHPUnit 9.5.20 

Runtime:       PHP 8.1.6
Configuration: /mnt/c/~/220607/Petit_Note/phpunit.xml

..............                                                    14 / 14 (100%)

Time: 00:00.167, Memory: 6.00 MB

OK (14 tests, 14 assertions)

I managed to test it locally. Thank you.

satopian commented 2 years ago

@minchao I was researching the test method for the time being, and somehow I understood it. But maybe it's time to rewrite the function. At that time, if I can't rewrite the phpunit test description myself, I may not be able to maintain this test. I'm still investigating, so maybe I'm just bearish, but I'll tell you.

minchao commented 2 years ago

@satopian Please let me know if there is anything you do not understand. 😃

satopian commented 2 years ago

@minchao I add functions, exchange arguments, increase / decrease. At that time, I am not confident that I will properly investigate and rewrite the relevant part. Sometimes I don't know the code I wrote myself, but if a third party writes the code, it's more likely to be even more incomprehensible. I can get a rough understanding from the PHPUnit manual. However, I'm not sure if phpUnit can be used for future development. Maintenance seems to be more difficult than I expected. The biggest problem is my poor development ability.

satopian commented 2 years ago

@minchao If I can't maintain it myself, I might discard this test. Or I may continue to use it. Can you accept it? I can try using what I can, but I can't maintain PHPUnit test if that can be a burden to me.

minchao commented 2 years ago

@satopian Sure! Please feel free to use them.

satopian commented 2 years ago

@minchao Thank you for understanding. is this all of it? If so, merge.

minchao commented 2 years ago

@satopian Yes! with the basic tests, we can start refactoring with a safety net.

satopian commented 2 years ago

@minchao I didn't know what to do when the error handling in functions.php came in, so I asked in the code review. I'm sorry to have just told you, but please tell me if it is within the range you can understand. If it's a problem that can't be helped, please let me know. thank you.

minchao commented 2 years ago

when the error handling in functions.php came in

@satopian I don't understand the meaning of the paragraph. Could you please provide more detailed information?

satopian commented 2 years ago

@minchao

//縮小表示
function image_reduction_display($w,$h,$max_w,$max_h){

    if(!is_numeric($w)||!is_numeric($h)){
        return error('失敗しました');//Rewrite; from return to error ('失敗しました')l
    }

Rewrite the function to. The return value is not returned and an error message is displayed by echo.

    ob_start();
~
    $buf = ob_get_contents();
    ob_end_clean();

It may be possible to use these to prevent echo, but if you assume that an echo error is included in the return value at the same time as other reduced display test patterns, how can you fix it? The question is whether it can be determined that the expected value has returned.

minchao commented 2 years ago

@satopian The error() uses the exit() function, which means script will terminate immediately, so we can't test it. Usually I would use trigger_error() to trigger a user error and handle by set_error_handler().

satopian commented 2 years ago

@minchao I decided to avoid putting error () in the function tested by PHPUnit Test. All I have to do is look at the return value and write the error error () in index.php ... Thank you.