{"id":1103,"date":"2024-04-05T12:12:20","date_gmt":"2024-04-05T03:12:20","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1103"},"modified":"2024-04-09T15:13:44","modified_gmt":"2024-04-09T06:13:44","slug":"laravel%e3%81%a7%e3%81%aephpunit%e9%96%a2%e9%80%a3%e3%81%ae%e5%80%8b%e4%ba%ba%e7%9a%84%e3%81%be%e3%81%a8%e3%82%81","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1103","title":{"rendered":"Laravel\u3067\u306ePHPUnit\u95a2\u9023\u306e\u500b\u4eba\u7684\u307e\u3068\u3081"},"content":{"rendered":"\n<h2 id=\"toc0\" class=\"wp-block-heading\">PHPUnit\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d\uff08\u3082\u3057\u5fc5\u8981\u306a\u3089\uff09<\/h2>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Laravel\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30eb\u30fc\u30c8\u3067\u5b9f\u884c\u3059\u308c\u3070\u826f\u3044\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# .\/vendor\/bin\/phpunit -v\n<\/pre><\/div>\n\n\n<h2 id=\"toc1\" class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u306e\u4f5c\u6210<\/h2>\n\n\n\n<p>\u65b0\u3057\u3044\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<br>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u30c6\u30b9\u30c8\u306ftests\/Feature\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3078\u914d\u7f6e\u3055\u308c\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\/\/ \u30c6\u30b9\u30c8\u306ftests\/Feature\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3078\u914d\u7f6e\u3055\u308c\u308b\u3002\n# php artisan make:test \u30c6\u30b9\u30c8\u540d\n\n\/\/ \u30c6\u30b9\u30c8\u306ftests\/Unit\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3078\u914d\u7f6e\u3055\u308c\u308b\u3002\n# php artisan make:test \u30c6\u30b9\u30c8\u540d --unit\n<\/pre><\/div>\n\n\n<h3 id=\"toc2\" class=\"wp-block-heading\">Feature\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3068Unit\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u9055\u3044<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u7d99\u627f\u3059\u308bTestCase\u30af\u30e9\u30b9\u304c\u9055\u3046\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Feature<br>Tests\\TestCase \u3092\u7d99\u627f\u3059\u308b\u306e\u3067\u3001\u30d5\u30a1\u30b5\u30fc\u30c9\u7b49\u306eLaravel\u306e\u6a5f\u80fd\u304c\u5229\u7528\u3067\u304d\u308b\u3002<\/li>\n\n\n\n<li>Unit<br>PHPUnit\\Framework\\TestCase \u3092\u7d99\u627f\u3059\u308b\u306e\u3067\u3001\u30d5\u30a1\u30b5\u30fc\u30c9\u7b49\u306eLaravel\u306e\u6a5f\u80fd\u304c\u5229\u7528\u3067\u304d\u306a\u3044\u3002<\/li>\n<\/ul>\n\n\n\n<h3 id=\"toc3\" class=\"wp-block-heading\">\u898f\u5247<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30af\u30e9\u30b9\u540d\u306f\u6700\u5f8c\u306bTest\u3092\u3064\u3051\u308b\u3002<\/li>\n\n\n\n<li>\u30e1\u30bd\u30c3\u30c9\u306fpublic\u306b\u3059\u308b\u3002<\/li>\n\n\n\n<li>\u30e1\u30bd\u30c3\u30c9\u540d\u306ftest\u3067\u306f\u3058\u3081\u308b\u3002\uff08Laravel\u306f\u30b9\u30cd\u30fc\u30af\u30b1\u30fc\u30b9\u3092\u5229\u7528\u3057\u3066\u3044\u308b\uff09<\/li>\n<\/ul>\n\n\n\n<h2 id=\"toc4\" class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30d7\u30ed\u30d0\u30a4\u30c0<\/h2>\n\n\n\n<h3 id=\"toc5\" class=\"wp-block-heading\">\u7d50\u8ad6<\/h3>\n\n\n\n<p>\u6ce8\u610f\u70b9\u3092\u8003\u3048\u308b\u3068\u3001Laravel\u3067\u306f\u4f7f\u3046\u30e1\u30ea\u30c3\u30c8\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u3002<\/p>\n\n\n\n<h3 id=\"toc6\" class=\"wp-block-heading\">\u6982\u8981<\/h3>\n\n\n\n<p>\u30e1\u30bd\u30c3\u30c9\u5358\u4f4d\u3067\u4f5c\u6210\u3067\u304d\u308b\u3057\u3001\u30af\u30e9\u30b9\u5358\u4f4d\u3067\u3082\u4f5c\u6210\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<p>\u4e0b\u8a18\u4f8b\u306e\u3088\u3046\u306b\u3001\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u5185\u306b\u30c7\u30fc\u30bf\u30d7\u30ed\u30d0\u30a4\u30c0\u3092\u4f5c\u6210\u3057\u305f\u5834\u5408\u306f\u3001DataProvider\u30af\u30e9\u30b9\u3092\u5229\u7528\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u30c6\u30b9\u30c8\u30af\u30e9\u30b9\u3068\u306f\u5225\u306e\u30c7\u30fc\u30bf\u30d7\u30ed\u30d0\u30a4\u30c0\u7528\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u305f\u5834\u5408\u306f\u3001DataProviderExternal\u30af\u30e9\u30b9\u3092\u5229\u7528\u3059\u308b\u3002<\/p>\n\n\n\n<h3 id=\"toc7\" class=\"wp-block-heading\">\u6ce8\u610f\u70b9<\/h3>\n\n\n\n<p>\u30c7\u30fc\u30bf\u30d7\u30ed\u30d0\u30a4\u30c0\u5185\u3067\u30d5\u30a1\u30b5\u30fc\u30c9\u3092\u5229\u7528\u3059\u308b\u3068\u3001\u4e0b\u8a18\u5185\u5bb9\u304c\u8868\u793a\u3055\u308c\u3001\u30c6\u30b9\u30c8\u304c\u5b9f\u884c\u3055\u308c\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u4ed6\u306b\u3082bcrypt\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3059\u308b\u3068\u540c\u3058\u73fe\u8c61\u304c\u767a\u751f\u3059\u308b\u3002<\/p>\n\n\n\n<p>DataProvider\u306fPHPUnit\\Framework\\Attributes\\DataProvider\u304c\u62c5\u3063\u3066\u3044\u308b\u306e\u3067\u3001<br>\u304a\u305d\u3089\u304fLaravel\u306e\u6a5f\u80fd\u3092\u5229\u7528\u3059\u308b\u3068\u30c6\u30b9\u30c8\u304c\u5b9f\u884c\u3055\u308c\u306a\u304f\u306a\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n INFO  No tests found.\n<\/pre><\/div>\n\n\n<h3 id=\"toc8\" class=\"wp-block-heading\">\u898f\u5247<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c7\u30fc\u30bf\u30d7\u30ed\u30d0\u30a4\u30c0\u30e1\u30bd\u30c3\u30c9\u306fpublic \u3067static\u306b\u3059\u308b\u3002<\/li>\n\n\n\n<li>\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u306fiterable\u306b\u3059\u308b\u3002\uff08foreach\u3067\u30eb\u30fc\u30d7\u3067\u304d\u308b\u3082\u306e\u306b\u3059\u308b\uff09<\/li>\n<\/ul>\n\n\n\n<h3 id=\"toc9\" class=\"wp-block-heading\">\u4f7f\u7528\u4f8b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nnamespace Tests\\Feature;\n\nuse PHPUnit\\Framework\\Attributes\\DataProvider;\nuse Tests\\TestCase;\n\nclass AuthServiceTest extends TestCase\n{\n    public static function temp_provider(): array\n    {\n        return &#x5B;\n            &#039;first&#039; =&gt; &#x5B;1, 2, 3],\n            &#039;second&#039; =&gt; &#x5B;4, 5, 6],\n            &#039;third&#039; =&gt; &#x5B;7, 8, 9],\n        ];\n    }\n\n    #&#x5B;DataProvider(&#039;temp_provider&#039;)]\n    public function test_temp(int $a, int $b, int $c): void\n    {\n        echo $a.&#039; : &#039;.$b.&#039; : &#039;.$c.&quot;\\n&quot;;\n    }\n}\n<\/pre><\/div>\n\n\n<p>\u4e0a\u8a18\u30af\u30e9\u30b9\u304c\u5b58\u5728\u3059\u308b\u72b6\u614b\u3067\u3001\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# php artisan test tests\/Feature\/AuthServiceTest.php --filter &#039;test_temp&#039;\n<\/pre><\/div>\n\n\n<p>\u4e0b\u8a18\u306e\u7d50\u679c\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n1 : 2 : 3\n4 : 5 : 6\n7 : 8 : 9\n\n   WARN  Tests\\Feature\\AuthServiceTest\n  ! temp with data set &quot;first&quot; \u2192 This test did not perform any assertions                                                      0.25s  \n  ! temp with data set &quot;second&quot; \u2192 This test did not perform any assertions                                                     0.12s  \n  ! temp with data set &quot;third&quot; \u2192 This test did not perform any assertions                                                      0.11s  \n\n  Tests:    3 risky (0 assertions)\n  Duration: 0.62s\n<\/pre><\/div>\n\n\n<h2 id=\"toc10\" class=\"wp-block-heading\">setUp\u30e1\u30bd\u30c3\u30c9<\/h2>\n\n\n\n<p>\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u5404\u30c6\u30b9\u30c8\u7528\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u524d\u306b\u5b9f\u884c\u3055\u308c\u308b\u30e1\u30bd\u30c3\u30c9\u3002<\/p>\n\n\n\n<p>\u4e8b\u524d\u6e96\u5099\u3092\u8a18\u8ff0\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n\n<h2 id=\"toc11\" class=\"wp-block-heading\">tearDown\u30e1\u30bd\u30c3\u30c9<\/h2>\n\n\n\n<p>\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u5404\u30c6\u30b9\u30c8\u7528\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u5f8c\u306b\u5b9f\u884c\u3055\u308c\u308b\u30e1\u30bd\u30c3\u30c9\u3002<\/p>\n\n\n\n<p>\uff08\u30c6\u30b9\u30c8\u304c\u6210\u529f\u3057\u3066\u3082\u5931\u6557\u3057\u3066\u3082\u5b9f\u884c\u3055\u308c\u308b\uff09<\/p>\n\n\n\n<p>\u5f8c\u7247\u4ed8\u3051\u7684\u306a\u3082\u306e\u3092\u8a18\u8ff0\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n\n<h2 id=\"toc12\" class=\"wp-block-heading\">RefreshDatabase\u30c8\u30ec\u30a4\u30c8<\/h2>\n\n\n\n<p>\u30c6\u30b9\u30c8\u7528\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u6e96\u5099\u3057\u305f\u65b9\u304c\u958b\u767a\u74b0\u5883\u3092\u6c5a\u3055\u305a\u306b\u6e08\u3080\u3002<br>\uff08DB\u30b3\u30f3\u30c6\u30ca\u3092\u8ffd\u52a0\u3057\u3001.env.testing\u306b\u8a18\u8f09\u3059\u308b\u3084\u308a\u65b9\u3082\u3042\u308b\uff09<\/p>\n\n\n\n<p>\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u5404\u30c6\u30b9\u30c8\u7528\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u5f8c\u306b\u6bce\u56deDB\u3092\u30ea\u30bb\u30c3\u30c8\u3057\u3066\u304f\u308c\u308b\u3002<\/p>\n\n\n\n<h3 id=\"toc13\" class=\"wp-block-heading\">\u6ce8\u610f\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u30c6\u30b9\u30c8\u7528\u30e1\u30bd\u30c3\u30c9\u5168\u3066\u304c\u5b8c\u4e86\u5f8c\u3001DB\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u30ea\u30bb\u30c3\u30c8\u3055\u308c\u308b\u304c\u3001<br>\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u306e\u30c6\u30b9\u30c8\u7528\u30e1\u30bd\u30c3\u30c9\u3054\u3068\u306b\u30b7\u30fc\u30b1\u30f3\u30b9\u306f\u30ea\u30bb\u30c3\u30c8\u3055\u308c\u306a\u3044\u306e\u3067\u6ce8\u610f\u3002<br>\uff08\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306bid\u3092\u6307\u5b9a\u3059\u308b\u306e\u304c\u7121\u96e3\uff09<\/li>\n\n\n\n<li>\u3053\u306e\u30c8\u30ec\u30a4\u30c8\u3092\u5229\u7528\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u30ea\u30bb\u30c3\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u304c\u3001<br>\u65e2\u5b58\u306e\u30c7\u30fc\u30bf\u3082\u524a\u9664\u3055\u308c\u3066\u3057\u307e\u3046\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>PHPUnit\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u78ba\u8a8d\uff08\u3082\u3057\u5fc5\u8981\u306a\u3089\uff09 \u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Laravel\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30eb\u30fc\u30c8\u3067\u5b9f\u884c\u3059\u308c\u3070\u826f\u3044\u3002 \u30c6\u30b9\u30c8\u306e\u4f5c\u6210 \u65b0\u3057\u3044\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3001\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u30c6\u30b9\u30c8\u306ftests\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[96,80],"tags":[88,81,102],"class_list":["post-1103","post","type-post","status-publish","format-standard","hentry","category-laravel","category-php","tag-laravel","tag-php","tag-phpunit"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1103"}],"version-history":[{"count":3,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1103\/revisions"}],"predecessor-version":[{"id":1110,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1103\/revisions\/1110"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}