<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>testing Archives - Alexandros Georgiou</title>
	<atom:link href="https://www.alexgeorgiou.gr/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.alexgeorgiou.gr/tag/testing/</link>
	<description>Balancing brackets for a living</description>
	<lastBuildDate>Mon, 22 Apr 2024 10:19:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.alexgeorgiou.gr/wp-content/uploads/2021/07/cropped-alexgeorgiou-icon-32x32.png</url>
	<title>testing Archives - Alexandros Georgiou</title>
	<link>https://www.alexgeorgiou.gr/tag/testing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>☑ WordPress checklists to go through before publishing a theme or plugin</title>
		<link>https://www.alexgeorgiou.gr/wordpress-checklists-publish-theme-plugin/</link>
					<comments>https://www.alexgeorgiou.gr/wordpress-checklists-publish-theme-plugin/#respond</comments>
		
		<dc:creator><![CDATA[alexg]]></dc:creator>
		<pubDate>Mon, 07 Nov 2016 11:48:55 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[checklist]]></category>
		<category><![CDATA[codecanyon]]></category>
		<category><![CDATA[coding standards]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[envato]]></category>
		<category><![CDATA[publish]]></category>
		<category><![CDATA[quality assurance]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[themeforest]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress.org]]></category>
		<guid isPermaLink="false">http://www.alexgeorgiou.gr/?p=142</guid>

					<description><![CDATA[<p>In this meta-article I will list eight WordPress checklists and a book that you will want to go through, before you publish a theme or plugin.</p>
<p>The post <a href="https://www.alexgeorgiou.gr/wordpress-checklists-publish-theme-plugin/">☑ WordPress checklists to go through before publishing a theme or plugin</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this meta-article I will list <em>eight WordPress checklists</em> and a book that you will want to go through, before you publish a theme or plugin. So before committing to the <em>wordpress.org</em> SVN or uploading to <em>Envato</em>, bookmark this page and start double-checking all the resources listed here!</p>
<h2><a title="Read 'Ten Things Every WordPress Plugin Developer Should Know'" href="https://www.smashingmagazine.com/2011/03/ten-things-every-wordpress-plugin-developer-should-know/" target="_blank" rel="bookmark noopener noreferrer">Ten Things Every WordPress Plugin Developer Should Know</a></h2>
<p>Actually you&#8217;ll probably want to read this before even starting your project; but better late than never! It&#8217;s packed with essential info that you should know. If you&#8217;ve missed any of the info in this article, go back to your code and fix it. There&#8217;s stuff about common practices, coding standards, etc.</p>
<h2 class="handbook-page-title"><a href="https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/" target="_blank" rel="noopener noreferrer">PHP Coding Standards</a></h2>
<p>This is a list of PHP coding standards that WordPress developers follow, both in core and in themes and plugins. It&#8217;s a requirement for submission to <em>wordpress.org</em> but it would be really nice to follow as many of these as possible even if you&#8217;re not planning to submit your code there.</p>
<p>Ideally you&#8217;ll want to use some tools for this. A quick search on <em>GitHub</em> reveals a number of syntax checkers. Here&#8217;s one that I use with <em>Eclipse</em>: <a href="https://github.com/edpittol/wordpress-eclipse-formatter" target="_blank" rel="noopener noreferrer">https://github.com/edpittol/wordpress-eclipse-formatter</a>, and another one for <em>CodeSniffer</em> (which can integrate nicely with a number of IDEs): <a href="https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards" target="_blank" rel="noopener noreferrer">https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards</a>.</p>
<p>Whatever you use, it doesn&#8217;t hurt to double check your code one last time. In <em>Eclipse Formatter</em> for instance, there is no way to check for <a href="https://en.wikipedia.org/wiki/Yoda_conditions" target="_blank" rel="noopener noreferrer">&#8220;Yoda&#8221; conditions</a> as far as I know, so you&#8217;ll want to manually check for those if you&#8217;re using it.</p>
<h2 class="handbook-page-title"><a href="https://make.wordpress.org/core/handbook/best-practices/coding-standards/javascript/">JavaScript Coding Standards</a></h2>
<p>Have you checked your PHP code for compliance to the coding standards? Good! Get ready to do it again, this time for your JavaScript files; at least the unminified copies of them. Here&#8217;s a project that uses <em>ESLint</em> to help you enforce the standards: <a href="https://www.npmjs.com/package/eslint-plugin-wordpress" target="_blank" rel="noopener noreferrer">https://www.npmjs.com/package/eslint-plugin-wordpress</a>. If you have a lot of JavaScript in your code, it might pay to integrate this into your pre-build checks, but as with PHP, always have one last look using your eyeballs.</p>
<h2><a href="https://wordpress.org/plugins/about/" target="_blank" rel="noopener noreferrer">WordPress plugins</a></h2>
<p>If you&#8217;re writing a plugin, this is a short list of conditions that your project must meet before you publish to <em>WordPress.org</em>. It has more to do with functionality than standards, and essentially it&#8217;s about not being evil with your code.</p>
<h2><a href="https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/" target="_blank" rel="noopener noreferrer">Detailed Plugin Guidelines</a></h2>
<p>This is essentially a longer version of the above. A definite must read if you&#8217;re publishing to <em>WordPress.org</em>. It&#8217;s about licensing, versioning, what you can and can&#8217;t do, etc.</p>
<h2 id="theme-review-process" class="toc-heading" tabindex="-1"><a href="https://make.wordpress.org/themes/handbook/review/#theme-review-process" target="_blank" rel="noopener noreferrer">Theme Review Process</a></h2>
<p>If you&#8217;re writing a theme that you plan to publish to <em>WordPress.org</em>, check that it at least conforms to all the <a href="https://make.wordpress.org/themes/handbook/review/required/" target="_blank" rel="noopener noreferrer">required items</a> and try to conform to as many <a href="https://make.wordpress.org/themes/handbook/review/recommended/" target="_blank" rel="noopener noreferrer">recommended items</a> as possible. The write-up for the recommended guidelines is not complete as of the writing of this article, but there&#8217;s some very useful guidelines in there.</p>
<h2><a href="https://codex.wordpress.org/Theme_Unit_Test" target="_blank" rel="noopener noreferrer">Theme Unit Test</a></h2>
<p>Again, if you&#8217;re writing a theme for <em>WordPress.org</em>, you will also want to check a number of things against the provided <a href="https://codex.wordpress.org/Theme_Unit_Test" target="_blank" rel="noopener noreferrer">Theme Unit Test</a>. It&#8217;s probably a good idea to do this even if you&#8217;re publishing elsewhere.</p>
<h2><a href="https://help.market.envato.com/hc/en-us/articles/202500774-ThemeForest-General-File-Preparation-Guidelines" target="_blank" rel="noopener noreferrer">ThemeForest General File Preparation Guidelines</a></h2>
<p>If you&#8217;re publishing to <em>ThemeForest</em>, you will have to follow these guidelines on how to bundle your files.</p>
<h2><a href="https://help.market.envato.com/hc/en-us/articles/202501494-WordPress-Phase-1-Submission-Requirements-Q-A" target="_blank" rel="noopener noreferrer">WordPress Phase 1 Submission Requirements &#8211; Q&amp;A</a></h2>
<p>This is a list of guidelines to follow before submitting to <em>ThemeForest</em>. There&#8217;s a number of things listed that are not allowed, so make sure to check.</p>
<h2><a href="https://help.market.envato.com/hc/en-us/articles/203269650-A-step-by-step-guide-to-the-upload-process" target="_blank" rel="noopener noreferrer">A step-by-step guide to the upload process</a></h2>
<p>When uploading to <em>ThemeForest</em>, you will also want to follow these guidelines. They are mostly about the upload process but also include some information about how to bundle your files.</p>
<h2>Professional WordPress: Design and Development</h2>
<p><a style="width: 120px; height: 240px; float: right; margin-left: 1em;" href="https://amzn.to/3W4EePm" width="300" height="150"><br />
<img decoding="async" src="https://m.media-amazon.com/images/I/71OD8C2NZNL._SY466_.jpg" /><br />
</a><br />
Finally, this is a book, not an actual checklist, but still I recommend that you at least glance through the table of contents. I&#8217;ve found this book very helpful. It covers all topics related to WordPress development and it&#8217;s very likely that whatever it is you&#8217;re trying to do, there might be a better way of doing it. WordPress is a complex system and it&#8217;s only getting more complex by the day.</p>
<p>My recommendation is that you go through the table of contents and read any chapter that talks about things you do in your theme or plugin. There are things you might have missed.</p>
<div style="clear: right;"></div>
<hr />
<p>Too meta? Perhaps! But no matter what and where you&#8217;re publishing, if you want to be professional about it, don&#8217;t assume you know everything. At least glance through all of these guidelines.</p>
<p>The post <a href="https://www.alexgeorgiou.gr/wordpress-checklists-publish-theme-plugin/">☑ WordPress checklists to go through before publishing a theme or plugin</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.alexgeorgiou.gr/wordpress-checklists-publish-theme-plugin/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>🐳 Test your WordPress themes and plugins with Docker</title>
		<link>https://www.alexgeorgiou.gr/test-wordpress-with-docker/</link>
					<comments>https://www.alexgeorgiou.gr/test-wordpress-with-docker/#comments</comments>
		
		<dc:creator><![CDATA[alexg]]></dc:creator>
		<pubDate>Tue, 10 May 2016 08:38:10 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://www.alexgeorgiou.gr/?p=55</guid>

					<description><![CDATA[<p>Use Docker to test your WordPress themes and plugins on a range of PHP and WordPress versions.</p>
<p>The post <a href="https://www.alexgeorgiou.gr/test-wordpress-with-docker/">🐳 Test your WordPress themes and plugins with Docker</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this article I&#8217;ll explain how Docker can simplify testing, when engineering WordPress components. But before I even dive into WordPress testing with Docker, let&#8217;s just go through some basic stuff, to make sure we&#8217;re on the same page:</p>
<p>TL;DR? <a href="#howto">Click here to skip to the how-to part</a>.</p>
<h2>A list of lists of things to worry about as a WordPress developer</h2>
<p>So you&#8217;ve built your theme or plugin and <em>&#8220;It Works<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2122.png" alt="™" class="wp-smiley" style="height: 1em; max-height: 1em;" />&#8221;</em>.</p>
<p>Being a pro ninja developer, you&#8217;ve thought of, analyzed, designed, developed, and tested, everything. Here are some issues you&#8217;ve had to address, in no particular order:</p>
<ul>
<li><strong>Your UI has been optimized with respect to the user experience.</strong> Any <a href="https://codex.wordpress.org/Settings_API">Settings</a> pages or <a href="https://developer.wordpress.org/themes/advanced-topics/customizer-api/">Customizer</a> tabs are in place and easy to navigate. Perhaps you&#8217;ve even made use of the Admin Pointers API. Good job! Your efforts will mean less effort in support later on.</li>
<li><strong>You&#8217;ve interfaced with the right WordPress APIs for the job</strong>, using recommended practices. Perhaps you&#8217;ve extended the <a href="https://codex.wordpress.org/TinyMCE">TinyMCE</a> editor to add a button or two for your special functionality. Perhaps you&#8217;ve even catered for people who <a href="https://wordpress.org/plugins/ckeditor-for-wordpress/">use WordPress with the CKEditor</a>. Or perhaps you&#8217;ve decided that they&#8217;re not worth the extra effort. In any case, you&#8217;ve made your educated guesses in your analysis, and you have followed through in your implementation.</li>
<li><strong>You&#8217;ve made sure that your product plays nicely with prominent plugins and themes in the wild jungle that is the WordPress ecosystem.</strong> You&#8217;ve tested your deliverable side-by-side with titans such as <a href="https://vc.wpbakery.com/">Visual Composer</a>, <a href="https://www.woothemes.com/woocommerce/">WooCommerce</a> and the likes, and you&#8217;re satisfied with the synergy.</li>
<li><strong>You&#8217;ve made correct use of HTML5, and CSS3</strong>, while silently serving polyfills to the Internet Explorer <a href="https://en.wikipedia.org/wiki/Luser">lusers</a> and anyone still browsing the web on an <a href="https://en.wikipedia.org/wiki/Android_Gingerbread">Android </a><a href="https://en.wikipedia.org/wiki/Android_Gingerbread">Gingerbread</a>.</li>
<li><strong>You&#8217;ve used the built-in <a href="https://codex.wordpress.org/WordPress_Optimization/Caching">caching mechanisms</a> appropriately.</strong> Remember, cache invalidation is <a href="http://skeptics.stackexchange.com/questions/19836/has-phil-karlton-ever-said-there-are-only-two-hard-things-in-computer-science">one of the only two hard problems in Computer Science</a>. You&#8217;ve made sure that power users can tweak your cache setting, while novices don&#8217;t need to know or care.</li>
<li>If you are creating your own tables on the database, <strong>you have a plan in place for upgrading the schema in future versions</strong>. Perhaps you&#8217;re saving the installed version number on the <code>wp_options</code> table? Good job! Very pro move!</li>
<li><strong>You&#8217;ve hooked into <a href="https://codex.wordpress.org/Function_Reference/register_activation_hook"><code>plugin_activation_hook()</code></a> <a href="https://codex.wordpress.org/Function_Reference/register_deactivation_hook">and</a> <a href="https://codex.wordpress.org/Function_Reference/register_uninstall_hook">friends</a>.</strong> You&#8217;ve come to terms with the humbling fact that users might, at some point, and for whatever reason, choose to uninstall your product! You&#8217;re kind enough to clean up after yourself. The community thanks you.</li>
<li><strong>Your slugs don&#8217;t clash with any other well-known slugs.</strong> Also, your post types don&#8217;t clash with other known post types. Remember, naming things is the <em>other</em> hard problem in Computer Science! Congratulate yourself for having tackled it, within the context of your project.</li>
<li><strong>Your design reflects the WordPress mantras.</strong> Study them <a href="https://wordpress.org/about/philosophy/">here</a>.</li>
<li><strong>You&#8217;ve thought about code quality.</strong> Your code conforms to the <a href="https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/">PHP</a> and JavaScript WordPress coding standards. You <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Strict_mode">only use the good parts of JavaScript</a>. Also, your code is readable and idiot-proof enough that even a non-programmer can hack it without breaking too much stuff. Perhaps you&#8217;ve incorporated <a href="http://pear.php.net/package/PHP_CodeSniffer/">PHP_CodeSniffer</a>, <a href="http://jshint.com/">JSHint</a> and friends into your IDE or build workflow. Bonus points for using <a href="https://www.phpdoc.org/">PHPdoc</a> comments on any APIs you expose.</li>
<li><strong>You&#8217;ve invested time in tooling/automation.</strong> Perhaps you use <a href="http://gruntjs.com/">grunt</a> or <a href="http://gulpjs.com/">gulp</a> or some other modern task management tool to automate testing, packaging (code, assets, translations, documentation, etc), deployment; you know, all the boring stuff. Perhaps you&#8217;re managing your dependencies with composer. The bottom line here is: you understand that shorter and easier iterations make you a more productive and insightful developer. If you can test and build a new release with one click, pat yourself on the back! You&#8217;re amazing and your effort will save you heaps of trouble later on.</li>
<li><strong>You&#8217;ve done at least <em>something</em> about testing.</strong> Perhaps you&#8217;ve decided your plugin&#8217;s functionality is simple enough that some smoke testing is enough. Or you have a rigorous &#8220;script&#8221; for the human that tests functionality via the browser. Bonus points if you&#8217;ve used a solution such as <a href="https://phpunit.de/">PHPUnit</a>, perhaps with some <a href="https://github.com/10up/wp_mock">WP_Mock</a> on the side. Or perhaps you&#8217;ve even gone as far as doing automated browser testing on real devices and browsers, using <a href="https://www.npmjs.com/package/karma">karma</a> or some other bleeding-edge solution. You&#8217;re insane! I love you!</li>
<li>To <strong>double-check you&#8217;ve thought of everything</strong>, you&#8217;ve scoured the web for other listicles of last-minute things to check. Here&#8217;s an <a href="https://www.smashingmagazine.com/2011/03/ten-things-every-wordpress-plugin-developer-should-know/">example</a>. Go find more.</li>
<li>If it&#8217;s a contract job, <strong>your client is happy</strong>. If it&#8217;s not, <strong>you&#8217;ve done your corridor testing</strong> and other WordPress developers or end users have given you their feedback.</li>
</ul>
<h2>(Version) hell is other programs</h2>
<p>If you&#8217;ve tackled all of the above and more, surely you&#8217;re &#8220;done&#8221;, right?</p>
<p>WRONG! Get ready to go through your test scenarios and checklists again on WordPress 4.0.10. And what about WordPress 3.7.13? Perhaps it&#8217;s too old to support, but you want to know if you can make the claim that your product works on it, right? And all of this has to work on PHP 5.4. Perhaps also on PHP 5.3? If you&#8217;ve done any custom DB stuff, do your SQL queries work as expected on MySQL 5.5? How about MySQL 4.x? And for every one of these version combinations, your product must work on the multitude of browsers and devices that you&#8217;re testing on. Oh, my!</p>
<blockquote><p>Every line of code you&#8217;ve written is a potential point of failure on some combination of platform, user data, and other pieces of software that share the same platform and data. Programming is easy, software engineering is hard!</p></blockquote>
<p>Remember, on day 1 when you go live, you will have a number of bugs coming in from users who use your product in combination with themes and plugins you haven&#8217;t even thought of. Your code will run on server environments so stupidly configured, you&#8217;ll want to find out where their administrator lives just so you can go punch them in the face. No matter how large or small your dev team is, <strong>you&#8217;ll have to tackle bugs very quickly in the first few days</strong>. On the internet, first impressions matter. So:</p>
<blockquote><p>The more bugs you can identify <span style="text-decoration: underline;">before</span> your users do, the better for the reputation of your product and team.</p></blockquote>
<p>The good news is that if you&#8217;ve been proactive with your tooling and test automation (and, being a professional developer, why wouldn&#8217;t you?), the easier and quicker it will be to repeat your tests on different platforms. But you still need to setup these platforms and deploy and test your product on them. Doing this using Virtual Machines is ultra-tedious, error-prone, and slow.</p>
<h2>Enter docker</h2>
<p>The age-old problem of software development can be summarized in the phrase &#8220;But it worked on <em>my</em> computer&#8221;. Today this problem has a solution, and it is called <em>Docker</em>. If you&#8217;ve been living under a rock for the past few years, go read <a href="https://www.docker.com/what-docker">what Docker is</a> and how it works before we move on. Go on, I&#8217;ll wait here!</p>
<p>Traditionally, lightweight containers are used like so: the developer &#8220;dockerizes&#8221; their product and delivers the resulting container to the system administrator, thus simplifying the deployment phase as well as any software migrations. Because deployment instructions are written by the developer in an unambiguous, repeatable, machine-executable form, this also eliminates human error and the aforementioned need for anyone to punch anyone in the face.</p>
<p>This is not what we&#8217;ll do here. In the context of WordPress component development,<strong> we will use a lightweight container to <span style="text-decoration: underline;">test</span>, not <span style="text-decoration: underline;">deploy</span>.</strong></p>
<h2 id="howto">What I did (and how you can do it too)</h2>
<p>Let me just come out and say it: I do not know, or want to know, how to install several versions of PHP side-by-side on one system. There is a multitude of how-to articles on the topic. I hate all of them because <del>I do not understand Linux package management</del> their authors are all stupid and lame. Instead, I started from <a href="http://blog.eriksen.com.br/en/docker-image-multi-version-php-development">this article</a> about a multi-PHP Docker image. You can optionally go read it, if you promise to come back here once you&#8217;re finished :-p</p>
<p>The bottom line is that there is a Docker image that already does half of what we want: It provides an environment where we can easily switch between a reasonable selection of popular PHP versions.</p>
<p>I then extended the image so that it installs WordPress, and my plugin, and runs it.</p>
<p>My plugin does not directly access the MySQL database. I only store data using the <a href="https://codex.wordpress.org/Options_API">Options API</a>. Therefore I ran a single version of a MySQL docker container like so:</p>
<pre>$ docker run --name wordpress-mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:5.5</pre>
<p>Now that our MySQL 5.5 database is up and running, let&#8217;s write a Dockerfile that describes an image based on that base image:</p>
<pre>FROM eriksencosta/php-dev:latest</pre>
<p>It is traditional to include some metadata:</p>
<pre>MAINTAINER Alexandros Georgiou &lt;alex.georgiou@gmail.com&gt;
LABEL Description="Container for testing themes and plugins on all the PHPs and WordPresses" Version="1.0"</pre>
<p>Then I declare two environment variables where I set the desired PHP and WP versions for my test. Note the use of the <code>ENV</code> instructions. (We can later override these values from the command line without the need to rebuild the image).</p>
<pre>ENV WP_VER=4.0
ENV PHP_VER=5.3</pre>
<p>We will want to be able to use <code>php</code>, <code>phpenv</code>, <code>composer</code>, etc. so let&#8217;s set the environment&#8217;s <code>PATH</code> to something sensible:</p>
<pre>ENV PATH /opt/phpenv/shims:/opt/phpenv/bin:/opt/php-build/bin/:/opt/composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin</pre>
<p>Now let&#8217;s use the <code>WORKDIR</code> instruction to say to docker that we want to do some stuff in the root user&#8217;s home directory. This is analogous to the <code>cd</code> command.</p>
<pre>WORKDIR /root</pre>
<p>Here we will download and install the WP-CLI.</p>
<p><strong>Having a command line interface to our WordPress instance is essential because we need an automated way (i.e. script) to deploy and test our software.</strong> (If you are a WordPress developer and you do not know or are not already using WP-CLI, please stop reading this blog, step away from the computer, sell your computer, and change your career to something other than engineering.) Part of being an engineer is about doing things in an automated, repeatable and testable way.</p>
<p>Because my project is based on grunt, I use <code>apt-get</code> to install <code>node</code> and <code>npm</code>. This will allow me to run stuff like <code>npm install</code> and <code>grunt test</code> later on. I also install some basic utilities, so when a bug shows up, I can drop into the container&#8217;s shell and investigate. Here I installed the MySQL client, Midnight Commander (my favorite orthogonal file manager), and two text editors, <code>vim</code> and <code>nano</code>. Depending on your project&#8217;s needs and personal preferences you will probably want to have other things installed via <code>apt-get</code>, but I bet you will need <code>wp-cli</code>.</p>
<pre>RUN \
    curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar &amp;&amp; \
    chmod +x wp-cli.phar &amp;&amp; \
    mv wp-cli.phar /usr/local/bin/wp &amp;&amp; \
    curl -sL https://deb.nodesource.com/setup | bash - &amp;&amp; \
    apt-get install -y nodejs npm mysql-client vim nano mc</pre>
<p>Note the backslashes. We&#8217;ve stringed together the shell commands into one as a performance optimization. Every command creates a new intermediate image in docker. We could just as well have issued commands in separate RUN instructions, but we know better than that!</p>
<p>The following <code>ADD</code> instruction says that any files in the same directory as this Dockerfile are to be copied into the image&#8217;s root user home directory.</p>
<pre>ADD . /root</pre>
<p>The way I&#8217;ve set things up, this will bring in a script that I call <code>install.sh</code>. This will carry out the deliverable&#8217;s deployment and possibly add content and run any automated testing we have available. Remember that the <code>CMD</code> instuction defines the <em>utility command</em> of the container. You can later override this from the command line. This is very useful for debugging.</p>
<pre>CMD /bin/bash -c /root/install.sh
</pre>
<p>In that same home directory I have a <code>plugins</code> directory. This will contain the zip files for any plugins I want to install. My <code>themes</code> directory will contain the themes that I will want to install. I have a <code>wp</code> directory where I have downloaded tarballs of every WordPress version since 3.7. Adding all of the versions into the image, we make sure that we can use our environment variable to switch between images without rebuilding the docker image. Arguably one could use the <code>wp-cli core install</code> command but that would involve re-downloading stuff every time.</p>
<p>Of course we need to make sure that we can access the image&#8217;s nginx web server:</p>
<pre>EXPOSE 80</pre>
<p>Let&#8217;s look at the entire <code>Dockerfile</code> again before we dive into <code>install.sh</code>:</p>
<pre>FROM eriksencosta/php-dev:latest

MAINTAINER Alexandros Georgiou &lt;alex.georgiou@gmail.com&gt;
LABEL Description="Container for testing themes and plugins on all the PHPs and WordPresses" Version="1.0"

ENV WP_VER=4.0
ENV PHP_VER=5.3

ENV PATH /opt/phpenv/shims:/opt/phpenv/bin:/opt/php-build/bin/:/opt/composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

WORKDIR /root

RUN \
    curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar &amp;&amp; \
    chmod +x wp-cli.phar &amp;&amp; \
    mv wp-cli.phar /usr/local/bin/wp &amp;&amp; \
    curl -sL https://deb.nodesource.com/setup | bash - &amp;&amp; \
    apt-get install -y nodejs npm mysql-client vim nano mc

ADD . /root

CMD /bin/bash -c /root/install.sh

EXPOSE 80</pre>
<p>Neat!</p>
<p>We then start writing our install script. Now, dockerfiles are a relatively new thing and warrant some explanation, but fortunately we all speak fluent <a href="https://www.gnu.org/software/bash/">bash</a>, so I hopefully don&#8217;t need to say much about the following code.</p>
<p>Remember how we&#8217;ve set environment variables for WordPress and PHP? Now is the time to use them.</p>
<pre>#!/bin/bash -x

echo "Installing on WordPress ${WP_VER} on PHP ${PHP_VER}"

phpenv global $PHP_VER</pre>
<p>We&#8217;ve switched to the right PHP, now on to install the right version of WordPress from our directory of WordPress tarballs:</p>
<pre>cd /var/www

tar xfv /root/wp/wordpress-${WP_VER}.tar.gz
touch /var/www/wordpress/wp-content/debug.log
chown -R root:root /var/www/wordpress</pre>
<p>Next, we create the <code>wp-config.php</code> file, with the help of <code>WP-CLI</code>. We will want to enable logging, to spot any errors resulting from running our code on different versions. We will also expose the image&#8217;s 80 port to port 81 of our local machine. This is necessary if you are already running a web server on your dev machine. Change 81 to any port that you have free.</p>
<pre>cd /var/www/wordpress

wp core config --allow-root --dbhost=db --dbname=wpdb --dbuser=root --dbpass=password --extra-php &lt;&lt;PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define('WP_HOME','http://localhost:81/wordpress');
define('WP_SITEURL','http://localhost:81/wordpress');
PHP</pre>
<p>Throughout my script you&#8217;ll notice the liberal use of <code>--allow-root</code> in <code>wp-cli</code> commands. In our container we do stuff as the user root, hence the need for the switch.</p>
<p>Let&#8217;s make sure that we start with a clean database:</p>
<pre>wp db drop --allow-root --yes

wp db create --allow-root

wp core install \
    --allow-root \
    --url=http://localhost:81 \
    --title="Testing on WordPress ${WP_VER} on PHP ${PHP_VER}" \
    --admin_user=admin \
    --admin_password=password \
    --admin_email=alex.georgiou@gmail.com</pre>
<p>Now let&#8217;s install the plugins we want in our test:</p>
<pre>wp plugin install /root/plugins/*.zip --allow-root
wp plugin activate --all --allow-root</pre>
<p>Perhaps you&#8217;ll need to import some content into your WordPress instance. Here we import a post that could contain new shortcodes or whatever:</p>
<pre>wp post create --allow-root --post_status=publish --post_type=page --post_title='Test post' /root/post.txt</pre>
<p>Depending on your project&#8217;s needs, you can go wild here. Use <code>wp-cli</code> to tweak your test site as needed. This is also where you can run your automated tests.</p>
<pre>mkdir /root/src
cd /root/src
git clone http://dev.lan/git/myproject.git
cd myproject
npm install
grunt phpunit</pre>
<p>Now let&#8217;s make sure that <a href="http://nginx.org/">nginx</a> is running, and that we are viewing the debug logs and web server logs:</p>
<pre>webserver restart
tail -f /var/log/nginx/*.log /var/www/wordpress/wp-content/debug.log</pre>
<p>That&#8217;s it!</p>
<p>Let&#8217;s review the install script in its entirety:</p>
<pre>#!/bin/bash -x

echo "Installing on WordPress ${WP_VER} on PHP ${PHP_VER}"

phpenv global $PHP_VER

cd /var/www

tar xfv /root/wp/wordpress-${WP_VER}.tar.gz
cp /root/.htaccess /var/www/wordpress
touch /var/www/wordpress/wp-content/debug.log
chown -R root:root /var/www/wordpress

cd /var/www/wordpress

wp core config --allow-root --dbhost=db --dbname=wpdb --dbuser=root --dbpass=password --extra-php &lt;&lt;PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define('WP_HOME','http://localhost:81/wordpress');
define('WP_SITEURL','http://localhost:81/wordpress');
PHP

wp db drop --allow-root --yes

wp db create --allow-root

wp core install \
    --allow-root \
    --url=http://localhost:81 \
    --title="Testing on WordPress ${WP_VER} on PHP ${PHP_VER}" \
    --admin_user=admin \
    --admin_password=password \
    --admin_email=alex.georgiou@gmail.com

wp plugin install /root/plugins/*.zip --allow-root

wp plugin activate --all --allow-root

wp post create --allow-root --post_status=publish --post_type=page --post_title='Test post' /root/post.txt

mkdir /root/src
cd /root/src
git clone http://dev.lan/git/myproject.git
cd myproject
npm install
grunt phpunit

webserver restart

tail -f /var/log/nginx/*.log /var/www/wordpress/wp-content/debug.log</pre>
<p>Beautiful!</p>
<p>We&#8217;re already running one docker image with a standard MySQL database. Now we need to build our webserver image and run it.</p>
<p>First, build the image:</p>
<pre>docker build -t alexg/wp-test:latest .</pre>
<p>Then, run it, making sure to link the database image, map the 80 port to local 81, and set the version variables:</p>
<pre>docker run --link wordpress-mysql:db -p 81:80 -e PHP_VER=5.6 -e WP_VER=4.4.2 -i -t alexg/wp-test:latest</pre>
<p>This command will now fire up a WordPress version of your choosing, and run the install script. The script will install your theme or plugin, add content, run your automated tests, and finally expose the entire thing at <a href="http://localhost:81/wordpress">http://localhost:81/wordpress</a>. Visit <a href="http://localhost:81/wordpress/wp-login.php">http://localhost:81/wordpress/wp-login.php</a> and login with <code>admin/password</code> or whatever credentials you&#8217;ve set in the <code>wp-cli core install</code> command.</p>
<p>Your shell should now be tailing the nginx and WP logs. Do any automated or manual testing via your browser(s), then hit Ctrl-C to stop the container and run the command with different versions, like so:</p>
<pre>docker run --link wordpress-mysql:db -p 81:80 -e PHP_VER=5.3 -e WP_VER=4.0 -i -t alexg/wp-test:latest</pre>
<p>Hit <a href="http://localhost:81/wordpress">http://localhost:81/wordpress</a> again and voila! Your product is now running on a completely different environment.</p>
<p>Now say you&#8217;ve spotted an error that did not show in your development environment. It is probably a version-related issue. How do you investigate?</p>
<p>You could override the install script by setting the utility command to be the Linux shell (note the part <strong>in bold</strong>):</p>
<pre>docker run --link wordpress-mysql:db -p 81:80 -e PHP_VER=5.6 -e WP_VER=4.4.2 -i -t alexg/slate-test:latest <strong>/bin/bash</strong></pre>
<p>Now docker loads your image and drops you in a shell. Here you can do fun stuff like connect to your database via the command line (remember, we&#8217;ve installed the mysql-client package):</p>
<pre>mysql -h db -u root -p
use wpdb;</pre>
<p>Once you&#8217;re ready you can run the install script yourself, or even run the deployment manually if you need to tweak stuff.</p>
<pre>/root/install.sh</pre>
<p>That&#8217;s it!</p>
<h2>Conclusion</h2>
<p><strong>Thanks to Docker, we&#8217;ve managed to parametrise our test system to the point where we know for sure that a particular deliverable will behave in a combination of PHP and WordPress versions.</strong> Want to test on different MySQL versions? Easy. Just tweak the command where we fire up the database.</p>
<p>You might think that all of this is too much. You might think that testing on a couple of versions is OK. But you would be wrong. You&#8217;ll be amazed at what issues crop up when you do rigorous testing. For instance, I was using shortcodes where their attributes had dashes (-) in their names. This is fine in WordPress 4.3.0 and greater, but not in earlier versions. Thanks to docker testing, I was able to find this out and fix it by changing any dashes to underscores. And I was able to do all this, without having to read <a href="https://codex.wordpress.org/Shortcode_API#Attributes">this part of the documentation that expressly warns about the issue</a>:</p>
<blockquote><p>Attribute names are optional and should contain only the following characters for compatibility across all platforms:</p>
<ul>
<li>Upper-case and lower-case letters: A-Z a-z</li>
<li>Digits: 0-9</li>
<li>Underscore: _</li>
<li>Hyphen: &#8211; (Not allowed before version 4.3.0)</li>
</ul>
</blockquote>
<p>In conclusion, does my solution suck? Yes, it sucks bad, because this was my first real use of Docker. Being a lightweight container &#8220;rookie&#8221;, I&#8217;ve probably done more than a few things &#8220;The Wrong Way&#8221;. Feel free to flame away in the comment section below, preferably but not necessarily in a constructive manner.</p>
<p>But, did my solution work? You bet your computer&#8217;s rear USB port it did!</p>
<p>Or, at least, it worked on my machine :-p</p>
<p>The post <a href="https://www.alexgeorgiou.gr/test-wordpress-with-docker/">🐳 Test your WordPress themes and plugins with Docker</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.alexgeorgiou.gr/test-wordpress-with-docker/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
