{"id":237721,"date":"2021-07-02T02:12:00","date_gmt":"2021-07-02T06:12:00","guid":{"rendered":"https:\/\/wordpress-756359-3782526.cloudwaysapps.com\/?p=237721"},"modified":"2024-09-21T04:53:26","modified_gmt":"2024-09-21T04:53:26","slug":"2021-07-01-tests-laravel","status":"publish","type":"post","link":"https:\/\/www.travis-ci.com\/blog\/2021-07-01-tests-laravel\/","title":{"rendered":"Running tests on MySQL with Laravel"},"content":{"rendered":"\n<p>In this weeks post, let\u2019s learn how to setup and run tests with MySQL and Laravel while using Travis CI, it\u2019s easy, educational, and for me it was a bit fun.<\/p>\n\n\n\n<p>Let\u2019s get started with our&nbsp;<code>.travis.yml<\/code>&nbsp;file, we will need a couple of things,&nbsp;<code>MySQL and PHPUnit<\/code>&nbsp;so we will include those things in our initial yaml file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>language: php # language \n\nphp:\n- 7.0\n- 7.1\n\nservices:\n- mysql # will init MySQL \n\nbefore_script:\n- cp .env.travis .env # copying .env.travis \n- mysql -e 'create database testing;' # will run this command\n- composer self-update\n- composer install --no-interaction\n- php artisan key:generate\n- php artisan migrate --seed\n\nscript:\n    - vendor\/bin\/phpunit # run PHPUnit\n<\/code><\/pre>\n\n\n\n<p>In this particular setup, you\u2019ll want to take a close look to the&nbsp;<code>before_script<\/code>, where most of the magic is happening. Now open up your text editor or IDE, in this case, I\u2019m using Vim:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>APP_ENV=testing\nAPP_KEY=SomeRandomString\n\nDB_CONNECTION=testing\nDB_TEST_DATABASE=testing_db\nDB_TEST_USERNAME=root\nDB_TEST_PASSWORD=\n\nCACHE_DRIVER=array\nSESSION_DRIVER=array\nQUEUE_DRIVER=sync<\/code><\/pre>\n\n\n\n<p>The next step is to setup&nbsp;<code>PHPUnit<\/code>&nbsp;to use the testing database connection function (this is of course assuming you are using MySQL and not something like Redis or PostGRES (I\u2019ll be making recipes for these DB frameworks as well), for this example we utilize MySQL, here is some XML to test the connection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> &lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n    &lt;Phpunit BackupGlobals=\"False\"\n     BackupStaticAttributes=\"False\"\n     Bootstrap=\"Bootstrap\/Autoload.Php\"\n     Colors=\"True\"\n     ConvertErrorsToExceptions=\"True\"\n     ConvertNoticesToExceptions=\"True\"\n     ConvertWarningsToExceptions=\"True\"\n     ProcessIsolation=\"False\"\n     StopOnFailure=\"False\"\n     SyntaxCheck=\"False\"&gt;\n&lt;Testsuites&gt;\n    &lt;Testsuite Name=\"Application Test Suite\"&gt;\n        &lt;Directory&gt;.\/tests\/&lt;\/Directory&gt;\n    &lt;\/Testsuite&gt;\n&lt;\/Testsuites&gt;\n&lt;Filter&gt;\n    &lt;Whitelist&gt;\n        &lt;Directory Suffix=\".Php\"&gt;app\/&lt;\/Directory&gt;\n    &lt;\/Whitelist&gt;\n&lt;\/Filter&gt;\n&lt;Php&gt;\n    &lt;Env Name=\"APP_ENV\" Value=\"Testing\"\/&gt; &lt;!--Make sure this matches your env var--&gt;\n    &lt;Env Name=\"DB_CONNECTION\" Value=\"Testing\"\/&gt;\n    &lt;Env Name=\"CACHE_DRIVER\" Value=\"Array\"\/&gt;\n    &lt;Env Name=\"SESSION_DRIVER\" Value=\"Array\"\/&gt;\n    &lt;Env Name=\"QUEUE_DRIVER\" Value=\"Sync\"\/&gt;\n&lt;\/Php&gt;\n&lt;\/Phpunit&gt;<\/code><\/pre>\n\n\n\n<p>Now add the testing config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'mysql' =&gt; &#91;\n        'driver' =&gt; 'mysql',\n        'host' =&gt; env('DB_HOST', 'localhost'),\n        'database' =&gt; env('DB_DATABASE', 'homestead'),\n        'username' =&gt; env('DB_USERNAME', 'homestead'),\n        'password' =&gt; env('DB_PASSWORD', 'secret'),\n        'charset' =&gt; 'utf8',\n        'collation' =&gt; 'utf8_unicode_ci',\n        'prefix' =&gt; '',\n        'strict' =&gt; false,\n    ],\n'testing' =&gt; &#91;\n        'driver' =&gt; 'mysql',\n        'host' =&gt; env('DB_TEST_HOST', 'localhost'),\n        'database' =&gt; env('DB_TEST_DATABASE', 'testing_db'),\n        'username' =&gt; env('DB_TEST_USERNAME', 'root'),\n        'password' =&gt; env('DB_TEST_PASSWORD', 'secret'),\n        'charset' =&gt; 'utf8',\n        'collation' =&gt; 'utf8_unicode_ci',\n        'prefix' =&gt; '',\n        'strict' =&gt; false,\n    ],\n],\n<\/code><\/pre>\n\n\n\n<p>Perfect, now we want to push this code to GitHub, using the classic flow:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git init\ngit add . \ngit commit -m \"Laravel push\" \ngit remote add origin remote repository URL\ngit remote -v \ngit push -u origin master<\/code><\/pre>\n\n\n\n<p>Make sure this repo is synced with Travis, and you\u2019re all set! I hope this recipe was as enjoyable as it was for you as it was for me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this weeks post, let\u2019s learn how to setup and run tests with MySQL and Laravel while using Travis CI, it\u2019s easy, educational, and for me it was a bit fun. Let\u2019s get started with our&nbsp;.travis.yml&nbsp;file, we will need a couple of things,&nbsp;MySQL and PHPUnit&nbsp;so we will include those things in our initial yaml file: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_breakdance_hide_in_design_set":false,"_breakdance_tags":"","footnotes":""},"categories":[16],"tags":[7,19,20,5],"class_list":["post-237721","post","type-post","status-publish","format-standard","hentry","category-news","tag-community","tag-feature","tag-infrastructure","tag-news"],"_links":{"self":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/237721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/comments?post=237721"}],"version-history":[{"count":1,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/237721\/revisions"}],"predecessor-version":[{"id":242658,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/237721\/revisions\/242658"}],"wp:attachment":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/media?parent=237721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/categories?post=237721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/tags?post=237721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}