<?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>Code for Concinnity</title>
	<atom:link href="http://cfc.kizzx2.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://cfc.kizzx2.com</link>
	<description>beautiful and elegant solutions</description>
	<lastBuildDate>Fri, 16 Jul 2010 18:57:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Archlinux &#8212; pure awesomeness (+ how to change Linux console font)</title>
		<link>http://cfc.kizzx2.com/index.php/archlinux-pure-awesomeness-how-to-change-linux-console-font/</link>
		<comments>http://cfc.kizzx2.com/index.php/archlinux-pure-awesomeness-how-to-change-linux-console-font/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 18:57:01 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arch-linux]]></category>
		<category><![CDATA[change-console-font]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=161</guid>
		<description><![CDATA[Today I checked out Arch Linux. It&#8217;s just that cool. It&#8217;s a minimalistic distro that focuses on, well, minimalism, simplicity and elegance. What&#8217;s more of a pleasant surprise is that I find the Arch Linux Wiki to be probably the most comprehensive and high quality resources of Linux in general available on the net. Among [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Farchlinux-pure-awesomeness-how-to-change-linux-console-font%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Farchlinux-pure-awesomeness-how-to-change-linux-console-font%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>Today I checked out <a href="http://archlinux.org">Arch Linux</a>. It&#8217;s just that cool. It&#8217;s a minimalistic distro that focuses on, well, minimalism, simplicity and <em>elegance</em>.</p>

<p>What&#8217;s more of a pleasant surprise is that I find the <a href="http://wiki.archlinux.org">Arch Linux Wiki</a> to be probably the most comprehensive and high quality resources of Linux in general available on the net. Among those gems, I&#8217;ve found the way to <a href="http://wiki.archlinux.org/index.php/Fonts">change the font at the console</a>. I&#8217;ve been using nix environments for a couple of years and this is the question that has always bugged me but nobody seemed to have talked about. There you go, several years of research <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

<p>Setting up Arch Linux was a pleasant learning experience from itself. The package manager <code>pacman</code> offers the same level of convenience as the famous <code>apt</code>, if not more. The nice thing about it is that Arch&#8217;s packages seem to be much more recent than those ancient rugs on Debian and Ubuntu.</p>

<p>At the same time, Arch doesn&#8217;t bog you down with lots of unnecessary stuffs. Right out of the box, Arch almost doesn&#8217;t have any packages, and you have to edit a couple of config files just to be able to download things from the repos. But it was a pleasant learning experience, largely attributable to the excellent wiki.</p>

<p>If you want to improve your nix-fu or just want plain, beautiful distro to play with, give Arch a try!</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/archlinux-pure-awesomeness-how-to-change-linux-console-font/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Restoring elegance to CakePHP &#8212; doing multiple joins The Right Way&#8482;</title>
		<link>http://cfc.kizzx2.com/index.php/restoring-elegance-to-cakephp-doing-multiple-joins-the-right-way/</link>
		<comments>http://cfc.kizzx2.com/index.php/restoring-elegance-to-cakephp-doing-multiple-joins-the-right-way/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 12:30:40 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=157</guid>
		<description><![CDATA[In my last article about unit testing, I mentioned one way to do ad-hoc multiple joins in CakePHP rather succinctly. Here&#8217;s a recap: 1234567891011121314151617function tagged&#40;$tag&#41; &#123; &#160; &#160; $this-&#62;bindModel&#40;array&#40;'hasOne'=&#62;array&#40; &#160; &#160; &#160; &#160; 'PostsTag'=&#62;array&#40; &#160; &#160; &#160; &#160; &#160; &#160; 'foreignKey'=&#62;false, &#160; &#160; &#160; &#160; &#160; &#160; 'conditions'=&#62;&#34;PostsTag.post_id = Post.id&#34; &#160; &#160; &#160; &#160; &#41;, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Frestoring-elegance-to-cakephp-doing-multiple-joins-the-right-way%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Frestoring-elegance-to-cakephp-doing-multiple-joins-the-right-way%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>In my <a href="/index.php/unit-testing-in-cakephp-the-missing-manual-and-a-step-by-step-tutorial/">last article about unit testing</a>, I mentioned one way to do ad-hoc multiple joins in CakePHP rather succinctly. Here&#8217;s a recap:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> tagged<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindModel</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hasOne'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'PostsTag'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'foreignKey'</span><span style="color: #339933;">=&gt;</span>false<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;PostsTag.post_id = Post.id&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'Tag'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'foreignKey'</span><span style="color: #339933;">=&gt;</span>false<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;PostsTag.tag_id = Tag.id&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'Tag.name'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>This is, of course, rather unintuitive. A <code>hasOne</code> relationship when in fact I&#8217;m trying to look for someone <code>hasAndBelongsToMany</code>? I thought more about it.</p>

<p>There is a <a href="http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find">Bakery article that talked about doing ad-hoc joins</a>. It looks more technically correct but just too freaking much verbose for my liking:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$markers</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'joins'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'markers_tags'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'MarkersTag'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'foreignKey'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MarkersTag.marker_id = Marker.id'</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'table'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'tags'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'alias'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Tag'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'inner'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'foreignKey'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'Tag.id = MarkersTag.tag_id'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'Tag.tag'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'q'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>

<p>Actually, a simple refactoring can make it (almost) syntactically sweet and technically more correct:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// /app/vendors/joins.php</span><br />
<br />
<span style="color: #009933; font-style: italic;">/**<br />
&nbsp;* A new helper class to produce those join arrays just to make <br />
&nbsp;* life less miserable<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">class</span> Joins<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> left<span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span>_makeJoin<span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'left'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> inner<span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span>_makeJoin<span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'inner'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> _makeJoin<span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conditions</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'table'</span><span style="color: #339933;">=&gt;</span>Inflector<span style="color: #339933;">::</span><span style="color: #004000;">tableize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'alias'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'type'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'foreignKey'</span><span style="color: #339933;">=&gt;</span>false<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$conditions</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// /app/app_model.php</span><br />
<br />
App<span style="color: #339933;">::</span><span style="color: #004000;">import</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Vendor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'joins'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> AppModel <span style="color: #000000; font-weight: bold;">extends</span> Model<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// /app/models/post.php</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> tagged<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/*<br />
&nbsp; &nbsp; &nbsp;* Let's make use of our new class, this has become<br />
&nbsp; &nbsp; &nbsp;* a &quot;one-liner.&quot;<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tag.name'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'joins'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Joins<span style="color: #339933;">::</span><span style="color: #004000;">inner</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PostsTag'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'PostsTag.post_id = Post.id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Joins<span style="color: #339933;">::</span><span style="color: #004000;">inner</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tag'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;PostsTag.tag_id = Tag.id&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/restoring-elegance-to-cakephp-doing-multiple-joins-the-right-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit testing in CakePHP &#8212; the missing manual and a step-by-step tutorial</title>
		<link>http://cfc.kizzx2.com/index.php/unit-testing-in-cakephp-the-missing-manual-and-a-step-by-step-tutorial/</link>
		<comments>http://cfc.kizzx2.com/index.php/unit-testing-in-cakephp-the-missing-manual-and-a-step-by-step-tutorial/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 16:53:40 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[manual]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unit-testing]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=150</guid>
		<description><![CDATA[Today I&#8217;ve finally formalized a streamlined procedure to do unit testing properly in CakePHP &#8212; without all the pain. Needless to say, The Cookbook&#8217;s chapter on this issue cover the basic grounds but is inconsistent and lack a real life feeling to it. The Cookbook&#8217;s coverage is really basic and doesn&#8217;t hold up to more [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Funit-testing-in-cakephp-the-missing-manual-and-a-step-by-step-tutorial%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Funit-testing-in-cakephp-the-missing-manual-and-a-step-by-step-tutorial%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>Today I&#8217;ve finally formalized a streamlined procedure to do unit testing properly in CakePHP &#8212; without all the pain. Needless to say, <a href="http://book.cakephp.org/view/160/Testing">The Cookbook&#8217;s chapter</a> on this issue cover the basic grounds but is inconsistent and lack a real life feeling to it.</p>

<p>The Cookbook&#8217;s coverage is really basic and doesn&#8217;t hold up to more real life complicated cases. Cake seems to be particularly picky about its <em>automagical</em> (too clever for me to figure out for a long time) configurations and bark errors at me very often. That led to reluctance to write tests and sometimes giving it up all together :p</p>

<p>The steps I&#8217;ve written here should lay down a very solid framework to make you bullet proof for all your future testing needs.</p>

<p>(Of course, this might be coming a bit late since most people would be better off looking at Cake&#8217;s successor <a href="http://lithify.me/">Lithium</a>, anyway here we goes.)</p>

<p>Can&#8217;t gather up much time to make a polished article but I believe this point-form brain dump is much more effective than most documentation out there.</p>

<h2>Testing models</h2>

<p>First, some notes</p>

<ul>
<li><p>Testing models is the most important thing. Of course, a proper MVC application should have fat models and thin controllers.</p></li>
<li><p>For the most part, <em>you really should use fixtures</em> even if you do not use fixtures to load data. If you just use the database live, chances are Cake might mess up your data and errors might pop up, just go ahead and use fixtures. <em>Yes, you don&#8217;t need to load data with your fixtures if you don&#8217;t want, but you need to turn this feature on</em>. Yes, this was the thing that confused me a lot.</p></li>
<li><p>CakePHP uses SimpleTest, you would think that you&#8217;d put your setup and teardown code in <code>setUp()</code> and <code>tearDown()</code> just like everybody else? Wrong. Cake has already invaded those spaces. You&#8217;ll need to use <code>startTest()</code> and <code>endTest()</code>. This was documented in the Cookbook but this took me quite some time to figure out since I took it for granted and didn&#8217;t RTFM.</p></li>
<li><p>I may cover controller testing later but IMO controller testing in Cake is mostly broken and is quite straight forward to figure out.</p></li>
</ul>

<h2>Testing models &#8212; the tutorial</h2>

<p>First we&#8217;ll set up the database, install SimpleTest etc. If you don&#8217;t know how to do this step you should read the <a href="http://book.cakephp.org/">Real Manual</a> first.</p>

<p>Set up our testing application database in (My)SQL.</p>

<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- We have the title field called &quot;name&quot; instead of &quot;title&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- IMO this is idiomatic Cake because your post's title </span><br />
<span style="color: #808080; font-style: italic;">-- will now automatically show up in Post::find('list')</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`posts`</span><br />
<span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">`id`</span> INT <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">`name`</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">`body`</span> TEXT<br />
<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- You may think that the &quot;name&quot; field should be primary key.</span><br />
<span style="color: #808080; font-style: italic;">-- Wrong! Doing so will make Cake unhappy if you ever use</span><br />
<span style="color: #808080; font-style: italic;">-- Cake's console schema or the all other Cake migration</span><br />
<span style="color: #808080; font-style: italic;">-- plugins out there. The id field is the Creed.</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`tags`</span><br />
<span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">`id`</span> INT <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">`name`</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span><br />
<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- The id field is the Creed, even for join tables</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`posts_tags`</span><br />
<span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">`id`</span> INT <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">`post_id`</span> INT <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">`tag_id`</span> INT <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>post_id<span style="color: #66cc66;">,</span> tag_id<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>

<p>Bake our stuffs. Bake has come a long way since the earlier versions and the bake in Cake 1.3 is genuinely useful:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cake bake all Post<br />
$ cake bake all Tag</div></td></tr></tbody></table></div>

<p>Now go create some test data:</p>

<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`posts`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Lorem&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Ipsum&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Dolor&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Sit&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`tags`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;apple&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;orange&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`posts_tags`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`post_id`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`tag_id`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>

<p>Now open <code>/app/models/post.php</code> and <code>/app/models/tag.php</code>. Both look OK! And Cake has gone ahead and created the test cases for us at <code>/app/tests/cases/model</code>. Let&#8217;s try to run it</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cake testsuite app all<br />
Error: Missing database table <span style="color: #ff0000;">'test_suite_posts_tags'</span> <span style="color: #000000; font-weight: bold;">for</span> model <span style="color: #ff0000;">'PostsTag'</span></div></td></tr></tbody></table></div>

<p>This is how broken it is! Now let&#8217;s fix it. The thing is that we haven&#8217;t baked the fixture for the join table class <code>PostsTag</code>. Of course, we don&#8217;t want to have to create controller, models, views just for a simple join class. Luckily we can amend it by creating all missing fixtures:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cake bake fixture all <span style="color: #660033;">-records</span> <span style="color: #660033;">-count</span> <span style="color: #000000;">999</span></div></td></tr></tbody></table></div>

<p>This will create all the missing fixtures. The <code>-records -count 999</code> part tells Cake to pull real data from our database as fixture. The <code>-count</code> part is needed because it defaults to 10. Might as well enter some very large number but YMMV. This will also <em>update</em> our existing fixtures for Post and Tag so that the new fixture data we&#8217;ve added to the database will be reflected in the fixtures. Let&#8217;s try to run the tests again:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cake testsuite app all<br />
...<br />
<span style="color: #000000;">4</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">4</span> &nbsp;<span style="color: #7a0874; font-weight: bold;">test</span> cases complete.</div></td></tr></tbody></table></div>

<p>Finally, we&#8217;ve got the testing architecture down. To honor TDD, let&#8217;s create (edit) our test file before we do some development. Now go to <code>/app/tests/cases/models/post.test.php</code>, you&#8217;ll see this line:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$fixtures</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'app.tag'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'app.post'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'app.posts_tag'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>This is unfortunate, because it means every time you add a new association to your model, you&#8217;ll have to manually edit this <code>$fixtures</code> array. Too bad there isn&#8217;t an automated way to do this. (Running <code>bake test</code> will overwrite your whole file. You&#8217;ve been warned!)</p>

<p>Anyway, let&#8217;s just write some tests for kicks in <code>/app/tests/cases/model/post.test.php</code>:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> testSanity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Our fixtures should be loaded with data</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Run it, all is good:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cake testsuite app all<br />
...<br />
<span style="color: #000000;">4</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">4</span> <span style="color: #7a0874; font-weight: bold;">test</span> cases <span style="color: #7a0874; font-weight: bold;">complete</span>: <span style="color: #000000;">2</span> passes.</div></td></tr></tbody></table></div>

<p>To honor TDD, we&#8217;ll write our test first before we do any implementation. Let&#8217;s add our test function. We&#8217;re going to write a model function that will find all posts for a given tag:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> testTagged<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// These are based on our testing fixture data above</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tagged</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apple'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span>Set<span style="color: #339933;">::</span><span style="color: #004000;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Post[name=Lorem]'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span>Set<span style="color: #339933;">::</span><span style="color: #004000;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Post[name=Ipsum]'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertFalse</span><span style="color: #009900;">&#40;</span>Set<span style="color: #339933;">::</span><span style="color: #004000;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Post[name=Dolor]'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tagged</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'orange'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span>Set<span style="color: #339933;">::</span><span style="color: #004000;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Post[name=Lorem]'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span>Set<span style="color: #339933;">::</span><span style="color: #004000;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Post[name=Dolor]'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertFalse</span><span style="color: #009900;">&#40;</span>Set<span style="color: #339933;">::</span><span style="color: #004000;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/Post[name=Ipsum]'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$posts</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Run the test and watch it fail:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cake testsuite app all<br />
...</div></td></tr></tbody></table></div>

<p>Do our implementation in <code>/app/models/post.php</code></p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*<br />
&nbsp;* If this line seems alien to you, don't worry.<br />
&nbsp;* Doing HABTM in CakePHP is a train wreck and whole another<br />
&nbsp;* topic. This is probably the most elegant way to do it <br />
&nbsp;* (not necessarily the shortest and cleanest) as far as <br />
&nbsp;* I know.<br />
&nbsp;*<br />
&nbsp;* The good thing about unit test -- we can be sure <br />
&nbsp;* it works even though we don't understand it lol<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">function</span> tagged<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindModel</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hasOne'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'PostsTag'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'foreignKey'</span><span style="color: #339933;">=&gt;</span>false<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;PostsTag.post_id = Post.id&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'Tag'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'foreignKey'</span><span style="color: #339933;">=&gt;</span>false<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;PostsTag.tag_id = Tag.id&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'conditions'</span><span style="color: #339933;">=&gt;</span>array<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">'Tag.name'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>The above trick was used to force CakePHP to do left joins for us. There is an <a href="http://teknoid.wordpress.com/2008/07/17/forcing-an-sql-join-in-cakephp/">article that talks about this technique</a> on the <a href="http://teknoid.wordpress.com/">nuts and bolts of cakephp</a> blog.</p>

<p>Well, let&#8217;s add one more test. We want to make a function to get us the content of a post:</p>

<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- First, update our test database</span><br />
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`posts`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`body`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Hello World!!&quot;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`name`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Lorem&quot;</span>;</div></td></tr></tbody></table></div>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// /app/tests/cases/models/post.test.php</span><br />
<span style="color: #666666; font-style: italic;">// Then write our new test</span><br />
<span style="color: #000000; font-weight: bold;">function</span> testGetContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findByName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lorem&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContent</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertPattern</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/hello world/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Run it, watch it fail. And then we add our implementation:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// /app/models/post.php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> getContent<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'body'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$post</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Post'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'body'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<p>Great, now run it and expect it to pass&#8230;.</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cake testsuite app all<br />
...<br />
<span style="color: #000000;">4</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">4</span> <span style="color: #7a0874; font-weight: bold;">test</span> cases <span style="color: #7a0874; font-weight: bold;">complete</span>: <span style="color: #000000;">11</span> passes, <span style="color: #000000;">1</span> fails.</div></td></tr></tbody></table></div>

<p>Wtf!? It failed? Yeah, we forgot to update our fixtures. That&#8217;s it. Every time we update our database, we need to update the fixture. Fortunately, this is one area from Cake that is really painless:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cake bake fixture all <span style="color: #660033;">-records</span> <span style="color: #660033;">-count</span> <span style="color: #000000;">999</span><br />
...<br />
$ cake testsuite app all<br />
...<br />
<span style="color: #000000;">4</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">4</span> <span style="color: #7a0874; font-weight: bold;">test</span> cases <span style="color: #7a0874; font-weight: bold;">complete</span>: <span style="color: #000000;">12</span> passes.</div></td></tr></tbody></table></div>

<p>The fixture updating part was a little redundant, but it&#8217;s better than manually updating the fixture from SQL and also from <code>*_fixture.php</code>. I suggest you can store all your test fixtures data into an SQL file and make some bash script or ruby script to deconstruct the database and load the test SQL file for each test run. You can play around with different connection settings if you don&#8217;t want to sabotage your main table for every test run. You can package this loading of SQL file and the fixture baking into one script file so that it can be done in one click. (left as an exercise to reader)</p>

<p>That&#8217;s it! In retrospect, it isn&#8217;t rocket science, it&#8217;s just that I haven&#8217;t found any good piece of comprehensive tutorial that hand holds me from start to finish. I&#8217;ve been putting off a lot of unit test writing because I&#8217;ve always had Cake barking errors at me left and right. Now there&#8217;s no excuse <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/unit-testing-in-cakephp-the-missing-manual-and-a-step-by-step-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Composition vs. Inheritance</title>
		<link>http://cfc.kizzx2.com/index.php/composition-vs-inheritance/</link>
		<comments>http://cfc.kizzx2.com/index.php/composition-vs-inheritance/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 07:21:22 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Interesting things]]></category>
		<category><![CDATA[composition]]></category>
		<category><![CDATA[dependency-injection]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[liskov-substitution-principle]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=144</guid>
		<description><![CDATA[I was thinking about composition vs. inheritance today and I wanted to make everything crystal clear. Here&#8217;s some quick brain-dump of my research and thoughts: Use composition when possible. It delegates the dependency process to the callsite. This is a prefer composition over inheritance. Since modern programming idioms prefer composition to inheritance, most of the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fcomposition-vs-inheritance%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fcomposition-vs-inheritance%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>I was thinking about composition vs. inheritance today and I wanted to make everything crystal clear. Here&#8217;s some quick brain-dump of my research and thoughts:</p>

<ul>
<li><p>Use composition when possible. It delegates the dependency process to the callsite. This is a prefer composition over inheritance. Since modern programming idioms prefer composition to inheritance, most of the time using delegates and <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a> to achieve composition would be a Good Thing.</p></li>
<li><p>Why prefer composition to inheritance? Simply because inheritance is almost the strongest coupling you can introduce between two classes (second to <code>friend</code> classes in C++). We all know that coupling is bad and that separation of concern is good.</p></li>
<li><p>Use Inheritance whenever necessary. An example of a necessary case is when the new functionality (injection point) needs to access the protected members of the base class.</p></li>
<li><p>When using Inheritance, prefer nonpublic inheritance to public inheritance. Use private inheritance when you need to access the protected members of the base class; use protected inheritance when you need to access the protected members of the base class, while at the same time exposing them to your derivatives.</p></li>
<li><p>Only use public inheritance when your derived class is a true IS-A relationship that satisfies the <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov Substitution Principle</a>.</p></li>
<li><p>There is a good quote from somewhere:</p></li>
</ul>

<blockquote>
  <p>Inherit (publicly) not to reuse, but to be reused.</p>
</blockquote>

<!-- TODO: citation needed -->

<h2>Is there anything that inheritance can do but composition can&#8217;t do?</h2>

<ul>
<li><p>Yes, when you want to access protected members of the base class.</p></li>
<li><p>Theoretically, this could still be done using composition by having the base class pass along the required attributes, but that often quickly degenerates into passing a chain of message.</p></li>
<li><p>Even if you pass an instance of the Base class to the Derived class in hope of that the Derived class can access the Base class, it&#8217;s still limited to the public interface only.</p></li>
</ul>

<!-- TODO: Insert example here -->

<!-- TODO: finish this example
For example, consider a `Monster` class. Below is an initial design:

    class Monster
    {
    protected:
        int health_;

        void WalkQuickly(){ cout << "Walk quickly" << endl;}
        void WalkSlowly(){ cout << "Walk slowly" << endl;}
    public:
        void Walk();
        // ...
    }

Suppose that our `Monster` have other methods listed at the `// ...` part that would increase/decrease its health. `Walk()` is a public method that will pick `WalkQuickly()` or `WalkSlowly()` depending on `health_`.

Suppose we want to have 3 types of Monsters right now:

- FragileMonster walks quickly on full health. Otherwise it walks slowly.
- NormalMonster walks quickly if it has more than half health. Otherwise it walks slowly.
- StrongMonsters walks quickly no matter what (probably until it dies).

Now we want to design our `Monster` class so that 

-->
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/composition-vs-inheritance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Start doing Requirement Specifications &#8212; start programming in English!</title>
		<link>http://cfc.kizzx2.com/index.php/start-doing-requirement-specifications-start-programming-in-english/</link>
		<comments>http://cfc.kizzx2.com/index.php/start-doing-requirement-specifications-start-programming-in-english/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 12:41:32 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[estimation]]></category>
		<category><![CDATA[project-management]]></category>
		<category><![CDATA[requirements]]></category>
		<category><![CDATA[specification]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=139</guid>
		<description><![CDATA[As a freelance developer, I&#8217;ve always thought that formal requirements specifications are just too much overhead. Sure, big projects in big corporations will have to ensure the budget and time-frame very accurately using rigorous methods like IBM&#8217;s RUP or the ISO approved Function Point Analysis, etc. But for a one-man show, I mean, I could [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fstart-doing-requirement-specifications-start-programming-in-english%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fstart-doing-requirement-specifications-start-programming-in-english%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>As a freelance developer, I&#8217;ve always thought that formal requirements specifications are just too much overhead. Sure, big projects in big corporations will have to ensure the budget and time-frame very accurately using rigorous methods like IBM&#8217;s <a href="http://www-01.ibm.com/software/awdtools/rup/">RUP</a> or the ISO approved <a href="http://en.wikipedia.org/wiki/Function_point">Function Point Analysis</a>, etc. But for a one-man show, I mean, I could have got the whole project done in the time I had to prepare the specifications, right?</p>

<h2>Wrong</h2>

<p>Programmers have always notoriously over-estimated how fast they can write code. Of course, we enjoy writing code and time passes by just so quickly, but it doesn&#8217;t mean writing code is as really as efficient as writing English. Many small teams shy away from formal specifications because they think they could have written the damn code by the time they&#8217;ve got the specification down.</p>

<p>I&#8217;ve always believed that, as much as I believed I&#8217;m a programmer with adequate skill, until a recent revelation hit me:</p>

<h2>Writing the program is, in itself, a DAMN SPECIFICATION PROCESS</h2>

<p>That&#8217;s right. Instead of confirming with the customer what the requirements are about in clear concise English, we somehow feel better describing the problem to a computer using programming languages. We have advanced through several decades and have lived past the dark eras of Assembly programming to higher level programming languages like Ruby, Python and idioms like functional programming. Why did we choose to move away from low level languages like Assembly and C? Because higher level languages are inherently more expressive &#8212; we can say much more with fewer words in a higher level language. In other words, high level languages allow us to program faster.</p>

<p>And what&#8217;s the highest level language of all?</p>

<p><strong>English!!</strong></p>

<p>That&#8217;s right. Surprise.</p>

<h2>But computers can&#8217;t read English, doesn&#8217;t that mean duplication? We now need to define it in two sets of languages.</h2>

<p>Yeah, and that upsets my <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> spirit a lot.</p>

<p>In an ideal world, we could understand the requirements 100% accurately, code it once in the programming language of our choice (Ruby anyone?), deliver the product and bring home the bacon. <em>Everybody is happy</em></p>

<p>But how often does that ideal scenario happen?</p>

<h2>The ideal never happens. Let&#8217;s face it, we need to rewrite the damn code anyway.</h2>

<p>That&#8217;s right. Unless you are an uber skillz haxx0r incarnate, I believe you don&#8217;t expect your program to work totally correctly the first time you run it, right? For any programs that more than 10 lines long, I&#8217;m it takes at least a couple of retries at least to get it compile without errors. Even if you get it to work correctly, how many times have you written a program and say to yourself &#8220;Yeah, this is the perfect solution to the problem&#8221;?</p>

<p>That is, even after you get it to work technically correctly, you&#8217;ll realize there are some ways to improve it, some quirks that really shouldn&#8217;t be there. It&#8217;s called &#8220;I know it when I see it.&#8221;</p>

<h2>See? So much for DRY, we need to rewrite it over and over. It&#8217;s our damn fate.</h2>

<p>If you have to throw away &#8220;code,&#8221; would you rather throw away a couple of English descriptions plus some wireframes, or a full set of engineered objects?</p>

<p>Many <a href="http://en.wikipedia.org/wiki/Agile_software_development">Agile</a> proponents advocate writing code as soon as possible. Ironically, it&#8217;s pretty stupid once you think about the pseudo-math behind it:</p>

<pre><code>Let the total number of iterations needed be R
Let the amount of time needed for each English iteration be E
Let the amount of time needed for each Coding iteration be C

Obviously,
    R ≥ 1
    C ≥ 1

For total amount of time needed T:
    T = (R - 1)C + RN
        where N is either E or C

For example, in an ideal case, we do no specification,
just code it once and everything goes fine:
    T = C

Scenario 2: the first version gets rejected by client since we
misunderstood the requirements (or they think we
misunderstood, whatever). It took 3 times of coding:
    T = 3C

Scenario 3: we made a detailed specification, client realized
they mispresented the requirements (notice how the client seem
to understand their own faults when you just tell them in English
instead of code). It still took 3 iterations:
    T = 2E + C

Obviously, E &amp;lt; C. Which means we've saved time!
</code></pre>

<p>You see, <strong>the only case</strong> where writing the detailed specification would &#8220;waste&#8221; time is when you get everything right the first time. Honestly, how often does that happen?</p>

<h2>So what should I do?</h2>

<p>Should I go out and read on all rigorous processes out there? Probably, I went down that route and it was quite fun to learn each of them. But ultimately, I must also acknowledge the huge overheads those processes incur. And enforcing the client to go through rigid processes don&#8217;t always work without a full team of experienced lawyers and contract writers.</p>

<p>After a lot of trial and errors, the approach I&#8217;m adopting now is to have as detailed specification as possible before writing any code. It doesn&#8217;t necessarily have to be 100% accurate UML diagrams with spell-checked use cases. I find that just a set of PowerPoint slides, annotated wireframes along with some rough use case scenarios seem to work fine. I try to avoid having charts because people tend to misinterpret quite often. It&#8217;s the right mix of flexibility and details that maximizes my productivity.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/start-doing-requirement-specifications-start-programming-in-english/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some great bash command line tricks I learned lately</title>
		<link>http://cfc.kizzx2.com/index.php/some-great-bash-command-line-tricks-i-learned-lately/</link>
		<comments>http://cfc.kizzx2.com/index.php/some-great-bash-command-line-tricks-i-learned-lately/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 18:34:07 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Interesting things]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[commandlinefu]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=134</guid>
		<description><![CDATA[Many adopted from Peteris Krumins&#8217; blog post Display mounted file systems nicely The main point here is really about the column command: 1234567891011$ mount /dev/root on / type ext3 &#40;rw&#41; /proc on /proc type proc &#40;rw&#41; /dev/mapper/lvmraid-home on /home type ext3 &#40;rw,noatime&#41; $ mount &#124; column -t /dev/root &#160; &#160; &#160; &#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fsome-great-bash-command-line-tricks-i-learned-lately%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fsome-great-bash-command-line-tricks-i-learned-lately%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>Many adopted from <a href="http://www.catonmat.net/blog/another-ten-one-liners-from-commandlinefu-explained">Peteris Krumins&#8217; blog post</a></p>

<h3>Display mounted file systems nicely</h3>

<p>The main point here is really about the <code>column</code> command:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">mount</span><br />
<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>root on <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">type</span> ext3 <span style="color: #7a0874; font-weight: bold;">&#40;</span>rw<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">/</span>proc on <span style="color: #000000; font-weight: bold;">/</span>proc <span style="color: #7a0874; font-weight: bold;">type</span> proc <span style="color: #7a0874; font-weight: bold;">&#40;</span>rw<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>mapper<span style="color: #000000; font-weight: bold;">/</span>lvmraid-home on <span style="color: #000000; font-weight: bold;">/</span>home <span style="color: #7a0874; font-weight: bold;">type</span> ext3 <span style="color: #7a0874; font-weight: bold;">&#40;</span>rw,noatime<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<br />
$ <span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #000000; font-weight: bold;">|</span> column <span style="color: #660033;">-t</span><br />
<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>root &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on &nbsp;<span style="color: #000000; font-weight: bold;">/</span> &nbsp; &nbsp; &nbsp;<span style="color: #7a0874; font-weight: bold;">type</span> &nbsp;ext3 &nbsp; <span style="color: #7a0874; font-weight: bold;">&#40;</span>rw<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">/</span>proc &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on &nbsp;<span style="color: #000000; font-weight: bold;">/</span>proc &nbsp;<span style="color: #7a0874; font-weight: bold;">type</span> &nbsp;proc &nbsp; <span style="color: #7a0874; font-weight: bold;">&#40;</span>rw<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>mapper<span style="color: #000000; font-weight: bold;">/</span>lvmraid-home &nbsp;on &nbsp;<span style="color: #000000; font-weight: bold;">/</span>home &nbsp;<span style="color: #7a0874; font-weight: bold;">type</span> &nbsp;ext3 &nbsp; <span style="color: #7a0874; font-weight: bold;">&#40;</span>rw,noatime<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># woot, now it's printing out nicely!</span></div></td></tr></tbody></table></div>

<h3>Repeat arguments of the most recent command</h3>

<p><code>Alt + .</code></p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #7a0874; font-weight: bold;">echo</span> hello world<br />
hello world<br />
<br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>Alt + .<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #666666; font-style: italic;"># this becomes...</span><br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> world<br />
<br />
<span style="color: #666666; font-style: italic;"># Cool, but what if I wanted &quot;hello&quot;?</span><br />
<br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> hello world<br />
hello world<br />
<br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>Alt + <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>Alt + .<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> hello <span style="color: #666666; font-style: italic;"># here you go!</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Doing the same thing with history expansion</span><br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> hello world<br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">!!</span>:<span style="color: #000000;">1</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> hello<br />
hello<br />
<br />
<span style="color: #666666; font-style: italic;"># There's a shorthand for the last argument in history expansion</span><br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> hello world<br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">!</span>$<br />
<span style="color: #7a0874; font-weight: bold;">echo</span> world<br />
world</div></td></tr></tbody></table></div>

<h3>Edit the whole command line in $EDITOR</h3>

<p>This one is <em>extremely huge</em>. Ever got tired of doing those multiple lines long <code>ffmpeg</code> command lines? Here&#8217;s your salvation (and his name is <code>vi</code>)</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> my-input-file.avi <span style="color: #000000; font-weight: bold;">&lt;</span>Ctrl+x<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>Ctrl+e<span style="color: #000000; font-weight: bold;">&gt;</span><br />
<span style="color: #666666; font-style: italic;"># You can specify multiple command lines in your $EDITOR and they'll be executed one by one, cool!</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/some-great-bash-command-line-tricks-i-learned-lately/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Firefox vs. Chrome &#8212; is Mozilla going down?</title>
		<link>http://cfc.kizzx2.com/index.php/firefox-vs-chrome-is-mozilla-going-down/</link>
		<comments>http://cfc.kizzx2.com/index.php/firefox-vs-chrome-is-mozilla-going-down/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 17:13:18 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Interesting things]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=132</guid>
		<description><![CDATA[Just read an article on Tom&#8217;s Hardware: Why Mozilla Needs to go into Survival Mode. Woflgang Gruener said that &#8220;Google&#8217;s royalties account for 80-90% of Mozilla&#8217;s entire revenues.&#8221; If that&#8217;s true, then when Google&#8217;s got their own browser, Mozilla would surely go down. Since it&#8217;s launch last year, Chrome&#8217;s been iterating extremely rapidly. Chrome v5 [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Ffirefox-vs-chrome-is-mozilla-going-down%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Ffirefox-vs-chrome-is-mozilla-going-down%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>Just read an article on <a href="http://www.tomshardware.com">Tom&#8217;s Hardware</a>: <a href="http://www.tomshardware.com/news/firefox-internet-explorer-chrome-safari-opera,10146.html">Why Mozilla Needs to go into Survival Mode</a>. Woflgang Gruener said that &#8220;Google&#8217;s royalties account for 80-90% of Mozilla&#8217;s entire revenues.&#8221; If that&#8217;s true, then when Google&#8217;s got their own browser, Mozilla would surely go down.</p>

<p>Since it&#8217;s launch last year, Chrome&#8217;s been iterating extremely rapidly. Chrome v5 is about to launch, that&#8217;s five freaking versions in a year! Firefox on the other hard, pretty much (seemingly) got stuck since 3.6. This is probably the weakness of open source software &#8212; changes are so damn slow!</p>

<p>I would be very sad to see Firefox go down, because the market really needs more open source sofware instead of giving more personal information to Google.</p>

<p>Strangely enough, my personally experience with Chrome isn&#8217;t particularly spectacular. Yes, it launches (way) faster than Firefox and I use it in my eee PC 1000H, but it&#8217;s not that much more stable. Ironically with its tab-process separation thing, I&#8217;ve had Chrome crashed on me several times <em>bringing down the whole system</em>. Another thing is that I&#8217;ve never managed to install any extensions to Chrome, every time I bothered to try, it crashed and would possibly bring my system together with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/firefox-vs-chrome-is-mozilla-going-down/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iterating Bash arrays with spaces</title>
		<link>http://cfc.kizzx2.com/index.php/iterating-bash-arrays-with-spaces/</link>
		<comments>http://cfc.kizzx2.com/index.php/iterating-bash-arrays-with-spaces/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 17:27:35 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[iteration]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=123</guid>
		<description><![CDATA[The problem 1234567891011121314151617181920212223242526272829#!/bin/sh $ a=(hello world foo bar) $ for i in #{a[*]}; do echo $i; done # Expected output: # &#160; hello # &#160; world # &#160; foo # &#160; bar # so far so good: # &#160; hello # &#160; world # &#160; foo # &#160; bar $ a=(&#34;hello world&#34; &#34;foo bar&#34;) $ [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fiterating-bash-arrays-with-spaces%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fiterating-bash-arrays-with-spaces%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><h2>The problem</h2>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#!/bin/sh<br />
<br />
$ a=(hello world foo bar)<br />
$ for i in #{a[*]}; do echo $i; done<br />
<br />
# Expected output:<br />
# &nbsp; hello<br />
# &nbsp; world<br />
# &nbsp; foo<br />
# &nbsp; bar<br />
<br />
# so far so good:<br />
# &nbsp; hello<br />
# &nbsp; world<br />
# &nbsp; foo<br />
# &nbsp; bar<br />
<br />
$ a=(&quot;hello world&quot; &quot;foo bar&quot;)<br />
$ for i in #{a[*]}; do echo $i; done<br />
<br />
# Expected output:<br />
# &nbsp; hello world<br />
# &nbsp; foo bar<br />
<br />
# omg:<br />
# &nbsp; hello<br />
# &nbsp; world<br />
# &nbsp; foo<br />
# &nbsp; bar</div></td></tr></tbody></table></div>

<p>The problem is caused by the affect that Bash uses space as array element separator internally.</p>

<h2>Failed attempt</h2>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#!/bin/sh<br />
<br />
$ a=(&quot;hello world&quot; &quot;foo bar&quot;)<br />
$ for i in &quot;#{a[*]}&quot;; do echo $i; done<br />
<br />
# Expected output:<br />
# &nbsp; hello world<br />
# &nbsp; foo bar<br />
<br />
# zomg!<br />
# &nbsp; hello world foo bar</div></td></tr></tbody></table></div>

<h2>The solution</h2>

<p>The solution lies in the magical <code>$@</code> expansion. When the <code>$@</code> expansion is put in a quote, the shell automatically expands each element properly quoted:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">#!/bin/sh<br />
<br />
a=(&quot;hello world&quot; &quot;foo bar&quot;)<br />
for i in &quot;#{a<br />
<br />
# Expected output:<br />
# &nbsp; hello world<br />
# &nbsp; foo bar<br />
<br />
# yay!<br />
# &nbsp; hello world<br />
# &nbsp; foo bar</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/iterating-bash-arrays-with-spaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Muxing audio and video with ffmpeg</title>
		<link>http://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/</link>
		<comments>http://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 10:10:20 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[multiplexing]]></category>
		<category><![CDATA[mux]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=112</guid>
		<description><![CDATA[ffmpeg is a great codec converter, but it&#8217;s wide array of options is really daunting. I just figured out it can also be used to mux video and audio together into a file, here&#8217;s how to do it 123456# Simple example: mux an audio with a video file without audio track $ ffmpeg -i audio.mp3 [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fmuxing-audio-and-video-with-ffmpeg%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fmuxing-audio-and-video-with-ffmpeg%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>ffmpeg is a great codec converter, but it&#8217;s wide array of options is really daunting. I just figured out it can also be used to mux video and audio together into a file, here&#8217;s how to do it</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># Simple example: mux an audio with a video file without audio track</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> audio.mp3 <span style="color: #660033;">-i</span> video.avi <span style="color: #660033;">-acodec</span> copy <span style="color: #660033;">-vcodec</span> copy output.avi<br />
<br />
<span style="color: #666666; font-style: italic;"># Daily usage example: mux an audio with a video file _with_ an existing audio track.</span><br />
<span style="color: #666666; font-style: italic;"># This will replace the AVI file's audio track with the MP3</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> audio.mp3 <span style="color: #660033;">-i</span> video-with-audio.avi <span style="color: #660033;">-acodec</span> copy <span style="color: #660033;">-vcodec</span> copy output.avi <span style="color: #660033;">-map</span> <span style="color: #000000;">0.0</span> <span style="color: #660033;">-map</span> <span style="color: #000000;">1.0</span></div></td></tr></tbody></table></div>

<p>The key to the second command is the <code>-map</code> parameter. Typically, the output file would contain two streams: one audio and one video. The numbers <code>0.0</code> and <code>1.0</code> refers to the first input file and the second input file respectively.</p>

<p>The <code>-map</code> parameter is used to <em>spell out</em> the streams. What the above said was &#8220;use input stream 0.0 for your first output stream (which is an audio stream) and input stream 1.0 for your second output stream (which is the video stream)&#8221;.</p>

<p>You can see a list of stream numbers by</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> audio.mp3 <span style="color: #660033;">-i</span> video-with-audio.avi</div></td></tr></tbody></table></div>

<p>You can add <em>additional</em> streams to the output file with the <code>-newaudio</code> and <code>-newvideo</code> parameters. Two audio streams make sense when you&#8217;re making a DVD rip with two sound tracks. I didn&#8217;t try it myself but it&#8217;s nice to know <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>

<p>Cheers <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vrapper: open source vim plugin for eclipse</title>
		<link>http://cfc.kizzx2.com/index.php/vrapper-open-source-vim-plugin-for-eclipse/</link>
		<comments>http://cfc.kizzx2.com/index.php/vrapper-open-source-vim-plugin-for-eclipse/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 09:49:12 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[jvi]]></category>
		<category><![CDATA[vi]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[viplugin]]></category>
		<category><![CDATA[vrapper]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=110</guid>
		<description><![CDATA[Found out Vrapper recently. The fact that it is open source deserves a lot of merits. For years eclipse users only had the commercial option ViPlugin which didn&#8217;t work quite well for something that charges money, considering the NetBeans folks have the excellent jVi which is also open source. Considering eclipse is free and open [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fvrapper-open-source-vim-plugin-for-eclipse%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fvrapper-open-source-vim-plugin-for-eclipse%2F&amp;source=kizzx2&amp;style=normal" height="61" width="50" />
			</a>
		</div><p>Found out <a href="http://vrapper.sourceforge.net/">Vrapper</a> recently. The fact that it is open source deserves a lot of merits. For years eclipse users only had the commercial option <a href="http://www.viplugin.com/">ViPlugin</a> which didn&#8217;t work quite well for something that charges money, considering the NetBeans folks have the excellent <a href="http://jvi.sourceforge.net/">jVi</a> which is also open source.</p>

<p>Considering eclipse is free and open source, Vrapper only seems more fitting than ViPlugin. I&#8217;ve tried it for a week and it seems most essential functions are there, and I&#8217;ve been using vi for a couple of years so I think my set of &#8220;essential functions&#8221; shouldn&#8217;t be too narrow :p</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/vrapper-open-source-vim-plugin-for-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
