{"id":1774,"date":"2015-02-21T17:20:42","date_gmt":"2015-02-21T16:20:42","guid":{"rendered":"http:\/\/denbeke.be\/blog\/?p=1774"},"modified":"2015-02-25T13:41:32","modified_gmt":"2015-02-25T12:41:32","slug":"php-unit-tests-with-travis-phpunit-and-composer","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/","title":{"rendered":"PHP: Unit tests with Travis, PHPUnit and Composer"},"content":{"rendered":"<p><em>In a perfect world, every software developer writes tons of unit tests, and uses continuous integration to make sure everything keeps working. Travis, PHPUnit and Composer are there to save you a lot of time!<\/em><\/p>\n<p>In this blogpost I will explain how to setup Travis and\u00a0run PHPUnit to run unit tests in a PHP project with Composer.<\/p>\n<h2>Composer<a href=\"http:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/attachment\/composer-logo\/\" rel=\"attachment wp-att-1786\"><img loading=\"lazy\" class=\"alignright wp-image-1786 \" src=\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Composer-logo-252x300.png\" alt=\"Composer logo\" width=\"203\" height=\"242\" srcset=\"https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Composer-logo-252x300.png 252w, https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Composer-logo.png 300w\" sizes=\"(max-width: 203px) 100vw, 203px\" \/><\/a><\/h2>\n<p><a title=\"Composer\" href=\"https:\/\/getcomposer.org\" target=\"_blank\">Composer<\/a> is a super awesome dependency manager for PHP. If you don&#8217;t use it yet, you are doing it wrong!\u00a0\ud83d\ude42 \u00a0For this guide I assume you already have a working <code>composer.json<\/code> file and you know the basics of Composer.<\/p>\n<p>Before you can actually test something you must of course run <code>composer install<\/code> to download all the dependencies.<\/p>\n<h2>PHPUnit<\/h2>\n<p><a title=\"PHPUnit\" href=\"https:\/\/phpunit.de\/index.html\" target=\"_blank\">PHPUnit<\/a> is the ultimate unit testing framework for PHP. Installation instructions can be found <a title=\"Install PHPUnit\" href=\"https:\/\/phpunit.de\/getting-started.html\" target=\"_blank\">here<\/a>.<\/p>\n<p>Next thing to do is create the <code>phpunit.xml<\/code> file. This file will contain your PHPUnit configuration and will make it very easy to run the unit tests from the command line.<\/p>\n<pre><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n\r\n&lt;phpunit backupGlobals=\"false\"\r\n         backupStaticAttributes=\"false\"\r\n         bootstrap=\"vendor\/autoload.php\"\r\n         colors=\"true\"\r\n         convertErrorsToExceptions=\"true\"\r\n         convertNoticesToExceptions=\"true\"\r\n         convertWarningsToExceptions=\"true\"\r\n         processIsolation=\"false\"\r\n         stopOnFailure=\"false\"\r\n         syntaxCheck=\"false\"\r\n        &gt;\r\n\r\n    &lt;testsuites&gt;\r\n        &lt;testsuite name=\"My package's test suit\"&gt;\r\n            &lt;directory&gt;.\/tests\/&lt;\/directory&gt;\r\n        &lt;\/testsuite&gt;\r\n    &lt;\/testsuites&gt;\r\n\r\n&lt;\/phpunit&gt;<\/code><\/pre>\n<p>In this <code>phpunit.xml<\/code> file I have set some basic variables which might be different for your project. But note the <code>bootstrap<\/code> argument.\u00a0Assign the path to your Composer <code>autoload.php<\/code> file to the <code>bootstrap<\/code> variable.<\/p>\n<p>Next you must specify the directory where your PHP test files are located.<\/p>\n<p>Run your unit tests (and check if PHPUnit is correctly configured): <code>phpunit<\/code><\/p>\n<p>This should yield an output like this:<\/p>\n<pre><code>~\/S\/MyPackage (master) $ phpunit\r\nPHPUnit 4.5.0 by Sebastian Bergmann and contributors.\r\n\r\nConfiguration read from \/Path\/To\/My\/Package\/phpunit.xml\r\n\r\n.......\r\n\r\nTime: 92 ms, Memory: 3.50Mb\r\n\r\nOK (7 tests, 24 assertions)<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>Travis<a href=\"http:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/attachment\/travis-logo\/\" rel=\"attachment wp-att-1787\"><img loading=\"lazy\" class=\"alignright wp-image-1787 size-thumbnail\" src=\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Travis-logo-150x150.png\" alt=\"Travis logo\" width=\"150\" height=\"150\" srcset=\"https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Travis-logo-150x150.png 150w, https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Travis-logo-125x125.png 125w, https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Travis-logo.png 201w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/a><\/h2>\n<p>Composer and PHPUnit configured, it&#8217;s time to create the <code>.travis.yml<\/code> file to configure <a title=\"Travis\" href=\"https:\/\/travis-ci.org\" target=\"_blank\">Travis<\/a>.<\/p>\n<pre><code>language: php\r\nphp:\r\n  - 5.4\r\n  - 5.5\r\n  - 5.6\r\n\r\ninstall:\r\n  - composer install --no-interaction\r\n<\/code><\/pre>\n<p>The Travis configuration is just a normal PHP config, but before installing, Travis must run <code>composer install<\/code>.<\/p>\n<p>Push everything to Github and let Travis run your unit tests.<\/p>\n<p>If you want strict synchronization between your local Composer dependencies and remote installations, you must also commit the <code>composer.lock<\/code> file to your repo. Most people place it in their <code>.gitignore<\/code>, but it is actually meant to be committed to Git.<\/p>\n<h2>Example<\/h2>\n<p>If you need a live example of such a configuration, take a look at my ORM (<a title=\"DenBeke\/ORM Github\" href=\"https:\/\/github.com\/DenBeke\/ORM\" target=\"_blank\">Github<\/a> \/ <a title=\"DenBeke\/ORM Travis\" href=\"https:\/\/travis-ci.org\/DenBeke\/ORM\" target=\"_blank\">Travis<\/a>) package. In this package I specified an other Composer vendor directory (<code>core\/lib<\/code>), so I also changed the <code>bootstrap<\/code> variable in the PHPUnit configuration. You may also ignore the database section in the <code>.travis.yml<\/code> file&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a perfect world, every software developer writes tons of unit tests, and uses continuous integration to make sure everything keeps working. Travis, PHPUnit and Composer are there to save you a lot of time! In this blogpost I will explain how to setup Travis and\u00a0run PHPUnit to run unit tests in a PHP project [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[235],"tags":[248,215,247,246],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP: Unit tests with Travis, PHPUnit and Composer &ndash; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP: Unit tests with Travis, PHPUnit and Composer &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"In a perfect world, every software developer writes tons of unit tests, and uses continuous integration to make sure everything keeps working. Travis, PHPUnit and Composer are there to save you a lot of time! In this blogpost I will explain how to setup Travis and\u00a0run PHPUnit to run unit tests in a PHP project [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2015-02-21T16:20:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-02-25T12:41:32+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Composer-logo-252x300.png\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/02\/Composer-logo-252x300.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/\",\"name\":\"PHP: Unit tests with Travis, PHPUnit and Composer &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/#primaryimage\"},\"datePublished\":\"2015-02-21T16:20:42+00:00\",\"dateModified\":\"2015-02-25T12:41:32+00:00\",\"author\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/programming\/php-unit-tests-with-travis-phpunit-and-composer\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/015ba35e6ce4f5859e3888ca99807575?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1774"}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=1774"}],"version-history":[{"count":15,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1774\/revisions"}],"predecessor-version":[{"id":1791,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1774\/revisions\/1791"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=1774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=1774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}