{"id":2650,"date":"2022-05-23T11:45:31","date_gmt":"2022-05-23T08:45:31","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=2650"},"modified":"2022-05-23T12:07:16","modified_gmt":"2022-05-23T09:07:16","slug":"writing-unit-test-at-php-laravel","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/","title":{"rendered":"Writing Unit Test at PHP Laravel"},"content":{"rendered":"\n<p>It is important for us to write <strong>unit test<\/strong> if we want the codes we developed in a software project to deploy production with the least bugs. Especially if we are going to work in corporate and enterprise companies, we are asked to test the code we have written.<\/p>\n\n\n\n<p>In this post, I will explain how to write a <strong>unit test<\/strong> in <strong>PHP Laravel <\/strong>and how to run the written test. Writing a test can feel intimidating and a waste of time at first. But as we get used to test writing and practice, we will improve that skill.<\/p>\n\n\n\n<p>Laravel comes with the <strong>PHPUnit <\/strong>package where we will run the unit tests. PHPUnit is PHP&#8217;s unit testing framework.<\/p>\n\n\n\n<p>First, let&#8217;s look at where to write test codes in Laravel. The tests we will write in Laravel are located in the <strong>tests <\/strong>folder.<\/p>\n\n\n\n<p> <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"326\" height=\"370\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2022\/05\/image-1.png\" alt=\"\" class=\"wp-image-2653\"\/><\/figure><\/div>\n\n\n\n<p>This folder contains <strong>Feature <\/strong>and <strong>Unit<\/strong> folders and sample tests in these folders.<\/p>\n\n\n\n<p>There are 2 methods of running these tests.<\/p>\n\n\n\n<ol><li><code>.\/vendor\/bin\/phpunit<\/code><\/li><li><code>php artisan test<\/code><\/li><\/ol>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"704\" height=\"319\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2022\/05\/image-2.png\" alt=\"\" class=\"wp-image-2654\"\/><\/figure><\/div>\n\n\n\n<p>If we only want to run <strong>Unit tests<\/strong>, we run <code>php artisan test --testsuite=Unit<\/code> command on the terminal screen.<\/p>\n\n\n\n<p>General settings for the tests to be run in Laravel are made in the <strong>phpunit.xml<\/strong> file.<\/p>\n\n\n\n<h3>Writing Our First Unit Test<\/h3>\n\n\n\n<p>In this post, we will write a test for our <strong>add_post()<\/strong> method inside our <strong>PostsController<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">public function add_post(Request $request)\n    { \n        $name = $request-&gt;title;\n        $content = $request-&gt;content;\n        $status = $request-&gt;status;\n        \n        $post = Posts::create([\n                'name'=&gt;$name,\n                'content'=&gt;$content,\n                'status' =&gt;$status ?? 0\n            ]);\n       \n        return response()-&gt;json($post, 200);\n    }<\/pre><\/div>\n\n\n\n<p>Kendi testimizi yazmak i\u00e7in ilk \u00f6nce terminalde <code>php artisan make:test PostTest --unit<\/code> komutlar\u0131n\u0131 \u00e7al\u0131\u015ft\u0131r\u0131yoruz. <strong>Unit<\/strong> klas\u00f6r\u00fcn\u00fcn alt\u0131nda olu\u015fan <strong>PostTest.php<\/strong> dosyas\u0131na ilk testimizi yazal\u0131m.<\/p>\n\n\n\n<p>To write our own test, we first run <code>php artisan make:test PostTest --unit<\/code> commands in the terminal. Let&#8217;s write our first test in the <strong>PostTest.php<\/strong> file created under the Unit folder.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;PHP&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n\nnamespace Tests\\Unit;\n\nuse Tests\\TestCase;\n\nclass PostTest extends TestCase\n{\n    public function test_add_post()\n    {\n        $response = $this-&gt;call('POST', '\/admin\/add-post', [\n            'name' =&gt; 'Some post title 123',\n            'content' =&gt; 'Some post content 123',\n            'status' =&gt; false\n        ]);\n\n        $response-&gt;assertStatus($response-&gt;status(), 200);\n    }\n}\n<\/pre><\/div>\n\n\n\n<p>To run the test, we run the <code>php artisan test<\/code> command in the terminal.<\/p>\n\n\n\n<p>Good luck&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is important for us to write unit test if we want the codes we developed in a software project to deploy production with the least bugs. Especially if we are going to work in corporate and enterprise companies, we are asked to test the code we have written. In this post, I will explain [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Writing Unit Test at PHP Laravel - blog website<\/title>\n<meta name=\"description\" content=\"It is important for us to write unit test if we want the codes we developed in a software project to deploy production with the least bugs. Especially if we are going to work in corporate and enterprise companies, we are asked to test the code we have written.In this post, I will explain how to write a unit test in PHP Laravel and how to run the written test. Writing a test can feel intimidating and a waste of time at first. But as we get used to test writing and practice, we will improve that skill.Laravel comes with the PHPUnit package where we will run the unit tests. PHPUnit is PHP&#039;s unit testing framework.First, let&#039;s look at where to write test codes in Laravel. The tests we will write in Laravel are located in the tests folder.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing Unit Test at PHP Laravel - blog website\" \/>\n<meta property=\"og:description\" content=\"It is important for us to write unit test if we want the codes we developed in a software project to deploy production with the least bugs. Especially if we are going to work in corporate and enterprise companies, we are asked to test the code we have written.In this post, I will explain how to write a unit test in PHP Laravel and how to run the written test. Writing a test can feel intimidating and a waste of time at first. But as we get used to test writing and practice, we will improve that skill.Laravel comes with the PHPUnit package where we will run the unit tests. PHPUnit is PHP&#039;s unit testing framework.First, let&#039;s look at where to write test codes in Laravel. The tests we will write in Laravel are located in the tests folder.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-23T08:45:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-05-23T09:07:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2022\/05\/image-1.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2022\/05\/image-1.png\",\"contentUrl\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2022\/05\/image-1.png\",\"width\":326,\"height\":370},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/\",\"name\":\"Writing Unit Test at PHP Laravel - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/#primaryimage\"},\"datePublished\":\"2022-05-23T08:45:31+00:00\",\"dateModified\":\"2022-05-23T09:07:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"It is important for us to write unit test if we want the codes we developed in a software project to deploy production with the least bugs. Especially if we are going to work in corporate and enterprise companies, we are asked to test the code we have written.In this post, I will explain how to write a unit test in PHP Laravel and how to run the written test. Writing a test can feel intimidating and a waste of time at first. But as we get used to test writing and practice, we will improve that skill.Laravel comes with the PHPUnit package where we will run the unit tests. PHPUnit is PHP's unit testing framework.First, let's look at where to write test codes in Laravel. The tests we will write in Laravel are located in the tests folder.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2022\/05\/23\/writing-unit-test-at-php-laravel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing Unit Test at PHP Laravel\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/2650"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=2650"}],"version-history":[{"count":32,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/2650\/revisions"}],"predecessor-version":[{"id":2691,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/2650\/revisions\/2691"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=2650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=2650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=2650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}