Running PHP Tests - Vanilla Success
<main> <article class="userContent"> <div class="embedExternal embedImage display-large float-none"> <div class="embedExternal-content"> <a class="embedImage-link" href="https://us.v-cdn.net/6030677/uploads/XHSGACIXJOHQ/microsoftteams-image-288-29.png" rel="nofollow noreferrer noopener ugc" target="_blank"> <img class="embedImage-img" src="https://us.v-cdn.net/6030677/uploads/XHSGACIXJOHQ/microsoftteams-image-288-29.png" alt="MicrosoftTeams-image (8).png" height="108" width="1356" loading="lazy" data-display-size="large" data-float="none"></img></a> </div> </div> <p>Vanilla uses <a href="https://phpunit.de/" rel="nofollow noreferrer ugc">PHPUnit</a> for testing it's backend code. Tests are written in PHP and can be easily run the from command line, or more commonly using <a href="https://www.jetbrains.com/phpstorm/" rel="nofollow noreferrer ugc">PHPStorm</a>.</p><h2 data-id="required-test-setup">Required Test Setup</h2><p>The easiest way to get the full test harness setup is to use <a href="https://github.com/vanilla/vanilla-docker" rel="nofollow noreferrer ugc">vanilla-docker</a>.</p><p>After setting up vanilla docker, you will need to ensure you have a database named <code class="code codeInline" spellcheck="false" tabindex="0">vanilla_tests</code> which will be used for tests requiring the database.</p><p>Next you will need to update your PHPUnit configuration file for your locale machine. It's recommended to copy the <code class="code codeInline" spellcheck="false" tabindex="0">phpunit.xml.dist</code> in the root vanilla folder and rename it to <code class="code codeInline" spellcheck="false" tabindex="0">phpunit.xml</code>. You can then make changes to the <code class="code codeInline" spellcheck="false" tabindex="0">phpunit.xml</code> file.</p><p>Notably you will need need to change a few connection parameters.</p><p>You should set the following env variables.</p><p><strong>phpunit.xml</strong></p><pre class="code codeBlock" spellcheck="false" tabindex="0"><env name="TEST_DB_NAME" value="vanilla_test"/> <env name="TEST_DB_USER" value="root" /> <env name="TEST_DB_PASSWORD" value="" /> <env name="TEST_DB_HOST" value="database" /> </pre><p>Some tests, may require additional services to be configured, such as tests for Memcached, Sphinx, and ElasticSearch.</p><h2 data-id="running-the-tests-from-the-command-line">Running the Tests from the Command Line</h2><p>Tests can now be run from the command line.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">cd /path/to/vanilla ./vendor/bin/phpunit --testdox # Run only certain groups of tests ./vendor/bin/phpunit --testsuite Library --textdox ./vendor/bin/phpunit --testsuite APIv2 --textdox ./vendor/bin/phpunit --testsuite APIv0 --textdox # Run only tests in a particular directory. ./vendor/bin/phpunit --testdox applications/vanilla </pre><h2 data-id="running-tests-in-phpstorm">Running Tests in PHPStorm</h2><p>If you use PHPStorm, you can easily run you tests there, and get a nicer looking output and UI for running particular tests.</p><div class="embedExternal embedImage display-large float-none"> <div class="embedExternal-content"> <a class="embedImage-link" href="https://us.v-cdn.net/6030677/uploads/WKC86RGWL290/image.png" rel="nofollow noreferrer noopener ugc" target="_blank"> <img class="embedImage-img" src="https://us.v-cdn.net/6030677/uploads/WKC86RGWL290/image.png" alt="image.png" height="374" width="1128" loading="lazy" data-display-size="large" data-float="none"></img></a> </div> </div> <div class="embedExternal embedImage display-large float-none"> <div class="embedExternal-content"> <a class="embedImage-link" href="https://us.v-cdn.net/6030677/uploads/M2C86D1DL099/image.png" rel="nofollow noreferrer noopener ugc" target="_blank"> <img class="embedImage-img" src="https://us.v-cdn.net/6030677/uploads/M2C86D1DL099/image.png" alt="image.png" height="644" width="2838" loading="lazy" data-display-size="large" data-float="none"></img></a> </div> </div> <p><br></p> </article> </main>