{"id":4753,"date":"2021-01-25T09:00:49","date_gmt":"2021-01-25T00:00:49","guid":{"rendered":"https:\/\/www.sria.co.jp\/blog\/?p=4753"},"modified":"2021-01-25T21:50:06","modified_gmt":"2021-01-25T12:50:06","slug":"unittest-for-laravel","status":"publish","type":"post","link":"https:\/\/www.sria.co.jp\/blog\/2021\/01\/unittest-for-laravel\/","title":{"rendered":"UnitTest for Laravel"},"content":{"rendered":"<p>\u3069\u3082\u3001\u30ab\u30ed\u30fc\u30b8\u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/www.sria.co.jp\/blog\/2021\/01\/various-tests-with-laravel\/\">\u524d\u56de\u306eLaravel \u306e\u30c6\u30b9\u30c8\u306e\u7a2e\u985e\u3068\u305d\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066<\/a>\u306e\u7d9a\u304d\u306e\u8a18\u4e8b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001Laravel Unit \u30c6\u30b9\u30c8\u306e\u5177\u4f53\u7684\u306a\u5185\u5bb9\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>Laravel\u3067\u306e\u30c6\u30b9\u30c8\u5b9f\u884c\u306b\u306f\u4ee5\u4e0b\u306e3\u7a2e\u985e\u304c\u5b58\u5728\u3057\u307e\u3059\u304c\u3001<\/p>\n<p>\u3053\u306e\uff13\u3064\u306e\u30c6\u30b9\u30c8\u306e\u5185\u306e1\u756a\u76ee\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ol>\n<li>UnitTest<\/li>\n<li>FeatureTest<\/li>\n<li>BrowserTest<\/li>\n<\/ol>\n<p>\u74b0\u5883\u9762\u3084\u524d\u63d0\u3068\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5fc5\u8981\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u306a\u3069\u306f\u3001<\/p>\n<p><a href=\"https:\/\/www.sria.co.jp\/blog\/2021\/01\/various-tests-with-laravel\/\">\u524d\u56de\u306e\u8a18\u4e8b<\/a>\u3092\u3054\u53c2\u7167\u4e0b\u3055\u3044\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u304a\u3055\u3089\u3044<\/h2>\n<h3>Unit Test<\/h3>\n<p>\u5358\u4f53\uff08Unit\uff09\u30c6\u30b9\u30c8\u306f\u4e00\u3064\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u7126\u70b9\u3067\u3001\u6700\u3082\u7d30\u304b\u3044\u7c92\u5ea6\u3067\u306e\u30c6\u30b9\u30c8\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u614b\u3068\u3057\u3066\u306f\u3001PHPUnit\u306e\u62e1\u5f35\u7248\u3067\u3001\u6700\u521d\u304b\u3089Laravel\u306b\u542b\u307e\u308c\u3066\u304a\u308a\u3001<\/p>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d5\u30a1\u30a4\u30eb\u76f4\u4e0b\u306b<code>phpunit.xml<\/code>\u30d5\u30a1\u30a4\u30eb\u3082\u6700\u521d\u304b\u3089\u88c5\u5099\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u500b\u4eba\u7684\u306b\u306f\u3001\u958b\u767a\u3057\u305f\u30af\u30e9\u30b9\u3068\u30c6\u30b9\u30c8\u7528\u30af\u30e9\u30b9\u304c\u3001\uff11\u5bfe\uff11\u306b\u306a\u308b\u69d8\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u751f\u6210\u3059\u308b\u3068\u7ba1\u7406\u3057\u3084\u3059\u3044\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>Unit Test \u5b9f\u88c5\u306b\u3064\u3044\u3066<\/h3>\n<p>\u5b9f\u88c5\u65b9\u6cd5\u306f\u3001\u8272\u3005\u3042\u308a\u307e\u3059\u304c\u3001\u79c1\u304c\u826f\u304f\u3084\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d5\u30a9\u30eb\u30c0\u306eapp \u76f4\u4e0b\u306b\u300clibs\u300d\u3068\u3044\u3046\u30d5\u30a9\u30eb\u30c0\u304c\u3042\u308a\u3001<\/p>\n<p>libs\u30d5\u30a9\u30eb\u30c0\u306e\u4e0b\u306b\u5404\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u306a\u308bPHP\u30d5\u30a1\u30a4\u30eb\u3084\u3001\u30e6\u30fc\u30c6\u30a3\u30ea\u30c6\u30a3\u3068\u306a\u308bPHP\u30d5\u30a1\u30a4\u30eb\u3092\u683c\u7d0d\u3057\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u3053\u3093\u306a\u611f\u3058\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210\u3067\u3059\u306d\u3002<\/p>\n<p>\u30eb\u30fc\u30c8\uff08\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d5\u30a9\u30eb\u30c0\uff09-&gt; app -&gt;\u00a0libs -&gt;\u00a0Utility.php<\/p>\n<p>&nbsp;<\/p>\n<p>Utility.php \u306e\u4e2d\u8eab\u306f\u3001\u4ee5\u4e0b\u306e\u69d8\u306a\u611f\u3058\u3060\u3068\u3057\u307e\u3059\u3002<\/p>\n<div><\/div>\n<pre class=\"lang:php decode:true \" title=\"Utility.php\">&lt;?php\n\nnamespace App\\Libs;\n\nuse Illuminate\\Support\\Str;\nuse Exception;\n\nclass Utility\n{\n\u00a0\u00a0\u00a0\u00a0\/*\n\u00a0\u00a0\u00a0\u00a0\u00a0*\n\u00a0\u00a0\u00a0\u00a0\u00a0*\/\n\u00a0\u00a0\u00a0\u00a0public function __construct(){\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\/*\n\u00a0\u00a0\u00a0\u00a0\u00a0*\n\u00a0\u00a0\u00a0\u00a0\u00a0*\/\n\u00a0\u00a0\u00a0\u00a0public static function getToken( $num = 32 )\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return substr(str_shuffle('1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $num );\n\u00a0\u00a0\u00a0\u00a0}\n}\n\n<\/pre>\n<p>\u30b7\u30f3\u30d7\u30eb\u306b\u3001\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3068\u300cgetToken\u300d\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3057\u304b\u3042\u308a\u307e\u305b\u3093\u306d\u3002<\/p>\n<div>\u3053\u306e\u300cgetToken\u300d\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u306b\u5bfe\u3057\u3001\u30c6\u30b9\u30c8\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/div>\n<div><\/div>\n<p>\u624b\u9806\u7684\u306b\u306f\u4ee5\u4e0b\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<ol>\n<li>\u30c6\u30b9\u30c8\u7528\u30d5\u30a1\u30a4\u30eb\u306e\u751f\u6210<\/li>\n<li>\u751f\u6210\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u306b\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u8ffd\u8a18<\/li>\n<li>\u30c6\u30b9\u30c8\u306e\u5b9f\u884c<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>\uff11\uff0e\u30c6\u30b9\u30c8\u7528\u30d5\u30a1\u30a4\u30eb\u306e\u751f\u6210<\/h3>\n<p>\u307e\u305a\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u306b\u3088\u308a\u300cUtility.php\u300d\u306eUnit\u30c6\u30b9\u30c8\u7528\u30d5\u30a1\u30a4\u30eb\u3092\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" title=\"Shell\">&gt; php artisan make:test UtilityTest\u00a0--unit\n\nTest created successfully.<\/pre>\n<div class=\"crayon-line\"><\/div>\n<div>\n<div><\/div>\n<\/div>\n<div>\n<div>\u30b3\u30de\u30f3\u30c9\u5b9f\u884c\u306b\u3088\u308a\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u300cUtilityTest.php \u300d\u30d5\u30a1\u30a4\u30eb\u304c\u751f\u6210\u3055\u308c\u307e\u3059\u3002<\/div>\n<div>\u203b\u30eb\u30fc\u30c8\uff08\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d5\u30a9\u30eb\u30c0\uff09-&gt; tests-&gt;Unit-&gt;UtilityTest.php<\/div>\n<div><\/div>\n<\/div>\n<div><\/div>\n<div>\n<p>\u300cUtilityTest.php\u300d\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u3092\u8997\u3044\u3066\u898b\u308b\u3068\u4ee5\u4e0b\u306e\u69d8\u306a\u8a18\u8ff0\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u3053\u308c\u306b\u8ffd\u8a18\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<\/div>\n<pre class=\"lang:php decode:true \" title=\"UtilityTest.php\">&lt;?php\n \nnamespace Tests\\Unit;\n \nuse PHPUnit\\Framework\\TestCase;\n \nclass UtilityTest extends TestCase\n{\n    \/**\n     * A basic unit test example.\n     *\n     * @return void\n     *\/\n    public function testExample()\n    {\n        $this-&gt;assertTrue(true);\n    }\n}<\/pre>\n<div><\/div>\n<div>\n<h3>\uff12\uff0e\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u8ffd\u8a18<\/h3>\n<\/div>\n<div>\u4f8b\u3048\u3070\u3067\u3059\u304c\u2026<br \/>\n\u300cgetToken\u300d\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092\u898b\u308b\u3068\u3001\u6570\u5024\u3092\u5f15\u6570\u306b\u30e9\u30f3\u30c0\u30e0\u306a\u6587\u5b57\u5217\u3092\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002<\/div>\n<div>\u305d\u3057\u3066\u3001\u751f\u6210\u3057\u305f\u6587\u5b57\u5217\u3092\u547c\u3073\u5143\u306b\u8fd4\u5374\u3059\u308b\u3002\u3068\u3044\u3046\u30b7\u30f3\u30d7\u30eb\u306a\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/div>\n<div>\u60f3\u5b9a\u3055\u308c\u308b\u30c6\u30b9\u30c8\u9805\u76ee\u3068\u3057\u3066\u306f\u3001\u4ee5\u4e0b\u304c\u601d\u3044\u3064\u3044\u3061\u3083\u3044\u307e\u3059\u3088\u306d\u3002<\/div>\n<div>\n<ul>\n<li>\u5f15\u6570\u306b\u300c11\u300d\u3092\u8a2d\u5b9a\u3057\u305f\u3089\u3001\u30e9\u30f3\u30c0\u30e0\u306a11\u6587\u5b57\u304c\u8fd4\u3063\u3066\u304f\u308b\u3088\u306d\uff1f<\/li>\n<li>\u5f15\u6570\u306b\u300c0\u300d\u4ee5\u4e0b\u306e\u5024\u3092\u8a2d\u5b9a\u3057\u305f\u3089\uff1f<\/li>\n<li>62\u6587\u5b57\u304b\u3089\u6307\u5b9a\u5f15\u6570\u306e\u5206\u3001\u6587\u5b57\u5217\u3092\u53d6\u5f97\u3057\u3066\u3044\u308b\u3051\u3069\u3001\u5f15\u6570\u306b\u300c63\u300d\u4ee5\u4e0a\u3092\u8a2d\u5b9a\u3057\u305f\u3089\uff1f<\/li>\n<li>\u305d\u3082\u305d\u3082\u5f15\u6570\u304c\u6570\u5024\u4ee5\u5916\u3060\u3063\u305f\u3089\uff1f<\/li>\n<\/ul>\n<\/div>\n<p>\u3068\u8a00\u3046\u3053\u3068\u3067\u3001\u3053\u308c\u3092\u691c\u8a3c\u3059\u308b\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:php decode:true \" title=\"UtilityTest.php Add\">&lt;?php\n\n\nnamespace Tests\\Unit;\n\n\nuse PHPUnit\\Framework\\TestCase;\nuse App\\Libs\\Utility;\n\n\nclass UtilityTest extends TestCase\n{\n\u00a0\u00a0\u00a0\u00a0\/**\n\u00a0\u00a0\u00a0\u00a0 * A basic unit test example.\n\u00a0\u00a0\u00a0\u00a0 *\n\u00a0\u00a0\u00a0\u00a0 * @return void\n\u00a0\u00a0\u00a0\u00a0 *\/\n\u00a0\u00a0\u00a0\u00a0public function testExample()\n\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;assertTrue(true);\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\/**\n\u00a0\u00a0\u00a0\u00a0 * \u5f15\u6570\u306b\u300c11\u300d\u3092\u8a2d\u5b9a\u3057\u305f\u3089\u3001\u30e9\u30f3\u30c0\u30e0\u306a11\u6587\u5b57\u304c\u8fd4\u3063\u3066\u304f\u308b\u3088\u306d\uff1f\u3092\u691c\u8a3c\n\u00a0\u00a0\u00a0\u00a0 * \u3053\u306e\u30c6\u30b9\u30c8\u7528\u30e1\u30bd\u30c3\u30c9\u306e\u540d\u79f0\u306f\u3001\u4efb\u610f\u306b\u8a2d\u5b9a\n\u00a0\u00a0\u00a0\u00a0 * @return void\n\u00a0\u00a0\u00a0\u00a0 *\/\n\u00a0\u00a0\u00a0\u00a0public function testGetToken11()\n\u00a0\u00a0\u00a0\u00a0{\n        $utility = new Utility();\n        $token = $utility-&gt;getToken( 11 );\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;assertSame( 11 , strlen( $token ) );\n\u00a0\u00a0\u00a0\u00a0}\n}\n<\/pre>\n<h3><\/h3>\n<h5>\u300cassertSame\u300d\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3067\u3001\u5024\u304c\u540c\u3058\u304b\uff1f\u3068\u3044\u3046\u5224\u5b9a\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/h5>\n<h5>\u3093\u3067\u3001\u4ed6\u306b\u3082\u6ca2\u5c71\u3001\u5224\u5b9a\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u3089\u306e\u4e8b\u3092\u300cAssert\u300d\uff08\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\uff09\u3068\u8a00\u3044\u307e\u3059\u3002<\/h5>\n<h5><\/h5>\n<h5>\u203b\u4ed6\u306e\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u306f\u3001\u4ee5\u4e0bURL\u3092\u3054\u53c2\u7167\u4e0b\u3055\u3044\u3002\uff08\u6ca2\u5c71\u3042\u308a\u904e\u304e\u3066\u30d3\u30d3\u308a\u307e\u3059\u3002\uff09<\/h5>\n<h5><a href=\"https:\/\/phpunit.readthedocs.io\/ja\/latest\/assertions.html\" target=\"_blank\" rel=\"noopener\">https:\/\/phpunit.readthedocs.io\/ja\/latest\/assertions.html<\/a><\/h5>\n<h5><\/h5>\n<h3>\uff13\uff0e\u30c6\u30b9\u30c8\u306e\u5b9f\u884c<\/h3>\n<p>\u53d6\u308a\u5408\u3048\u305a\u3001\u30c6\u30b9\u30c8\u306e\u5b9f\u884c\u3092\u3057\u307e\u3059\u3002<br \/>\n\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u4ee5\u4e0b\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" title=\"Unit Test Run\">PS C:\\sample&gt; .\/vendor\/bin\/phpunit tests\/Unit<\/pre>\n<p>\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u69d8\u306b\u5b9f\u65bd\u7d50\u679c\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \" title=\"Unit Test Result\">PS C:\\sample&gt; .\/vendor\/bin\/phpunit tests\/Unit\nPHPUnit 8.5.8 by Sebastian Bergmann and contributors.\n\n................................................................. 65 \/ 65 (100%)\n\n\nTime: 254 ms, Memory: 10.00 MB\n\nOK (65 tests, 66 assertions)\n<\/pre>\n<p>\u5f8c\u306f\u3053\u308c\u3092\u5404\u30af\u30e9\u30b9\u306e\u30e1\u30bd\u30c3\u30c9\u5206\u3001\u7e70\u308a\u8fd4\u3057\u3066\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3057\u3066\u5b9f\u65bd\u3044\u304f\u611f\u3058\u3067\u3059\u3002<\/p>\n<p>\u3067\u308f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3082\u3001\u30ab\u30ed\u30fc\u30b8\u3067\u3059\u3002 \u524d\u56de\u306eLaravel \u306e\u30c6\u30b9\u30c8\u306e\u7a2e\u985e\u3068\u305d\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u306e\u7d9a\u304d\u306e\u8a18\u4e8b\u306b\u306a\u308a\u307e\u3059\u3002 \u4eca\u56de\u306f\u3001Laravel Unit \u30c6\u30b9\u30c8\u306e\u5177\u4f53\u7684\u306a\u5185\u5bb9\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 &nbsp; \u306f\u3058\u3081\u306b Lara&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,16,18,8,23],"tags":[186,50,210],"_links":{"self":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4753"}],"collection":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/comments?post=4753"}],"version-history":[{"count":22,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4753\/revisions"}],"predecessor-version":[{"id":4856,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/4753\/revisions\/4856"}],"wp:attachment":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/media?parent=4753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/categories?post=4753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/tags?post=4753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}