<?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>Claudio&#039;s Hideout &#187; zend framework</title>
	<atom:link href="http://claudio.cicali.name/post/tag/zend-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://claudio.cicali.name</link>
	<description>Claudio Cicali web hub</description>
	<lastBuildDate>Sun, 25 Sep 2011 11:29:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>The new providers discovery for Zend_Tool 1.10</title>
		<link>http://claudio.cicali.name/post/2010/03/the-new-providers-discovery-for-zend-tool-1-10/</link>
		<comments>http://claudio.cicali.name/post/2010/03/the-new-providers-discovery-for-zend-tool-1-10/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 13:06:30 +0000</pubDate>
		<dc:creator>claudio</dc:creator>
				<category><![CDATA[Articoli]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://claudio.cicali.name/?p=732</guid>
		<description><![CDATA[With the recent release of the 1.10 version of the  Zend Framework, they made a subtle change on how Zend_Tool searches its providers. Before 1.10 the loader (aka the provider discover) was set to be the IncludePathLoader class; what that meens is that if you wrote a new provider, all that you needed to do to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://framework.zend.com/"><img class="alignleft size-full wp-image-735" title="zf-logo" src="http://claudio.cicali.name/wp-content/uploads/2010/03/zf-logo.jpg" alt="" width="150" height="84" /></a></p>
<p>With the recent release of the 1.10 version of the  Zend Framework, they made a subtle change on how <a href="http://framework.zend.com/manual/1.10/en/zend.tool.html">Zend_Tool</a> searches its providers. Before 1.10 the loader (aka the provider discover) was set to be the <strong>IncludePathLoader</strong> class; what that meens is that if you wrote a new provider, all that you needed to do to have Zend_Tool automatically find it was to edit your PHP&#8217;s include_path or add your provider&#8217;s directory to the ZEND_TOOL_INCLUDE_PATH_PREPEND environment variable.</p>
<p>Now that is history, because (<a href="http://n4.nabble.com/Several-issues-for-Zend-Tool-1-10-td1555536.html">source</a>):</p>
<blockquote><p>There were many issues for people when Zend_Tool used a scanning approach to finding providers. This caused many issues on all different platforms.  Now we&#8217;ve opted to go the specify your providers approach.</p></blockquote>
<p>(beware: the suggested solution in that post is wrong)</p>
<p>So if you ever write a new tool provider remember that there&#8217;s no more &#8220;auto discovery&#8221; by scanning the path. The loader now is the <strong>BasicLoader</strong> and you have to explicitely tell Zend_Tool where <strong>your</strong> providers are and how their classes are named. For this to happen, you can use the <strong>zf enable config.provider</strong> command or use the <strong>zf.ini</strong> file.</p>
<p>My solution is:</p>
<ul>
<li>create a <strong>zf.ini</strong> file for your project. This is slighlty different from what the documentation implies; it considers zf.ini to be an hidden file in your $HOME. But this is only a default you can change via the  ﻿ZF_CONFIG_FILE env variable</li>
<li>put something like <strong>basicloader.classes.0 = &#8220;Migrations_MigrationProvider&#8221;</strong> as the first line (that example is the actual class name of my migration tool provider)</li>
<li>assure that your class can be loaded, setting ZEND_TOOL_INCLUDE_PATH_PREPEND accordingly</li>
</ul>
<p>On the same topic you can also read <a href="http://framework.zend.com/issues/browse/ZF-8899?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel">this issue</a>.</p>
<p>As a bonus track, below is a little bash script I use to run my migration tool.</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash

APPPATH=$(readlink -f ..)/app

export ZF_CONFIG_FILE=${APPPATH}/../zf.ini

if [ ! -f ${ZF_CONFIG_FILE} ]; then
  echo &quot;Non trovo zf.ini&quot;
  echo &quot;Forse non stai eseguendo questo programma dalla directory DB?&quot;
  exit -1
fi

if [[ &quot;${ZF_BIN_DIR}&quot; == &quot;&quot; ]]; then
  ZF_BIN_DIR=$(readlink -f ../vendor/Zend)/../../bin
  ZF_BIN_DIR=$(readlink -f ${ZF_BIN_DIR})
fi

if [ ! -f ${ZF_BIN_DIR}/zf.sh ]; then
  echo &quot;Non trovo zf.sh in &quot; ${ZF_BIN_DIR}
  echo &quot;Forse non stai eseguendo questo programma dalla directory DB?&quot;
  exit -1
fi

MIGCLASSDIR=$(readlink -f ../vendor/Renomo/library)

if [ ! -d ${MIGCLASSDIR} ]; then
  echo &quot;Impossibile trovare la directory della classe Migration&quot;
  exit -1
fi

MIGDIR=$(readlink -f migrations)

if [ ! -d ${MIGDIR} ]; then
  echo &quot;Impossibile trovare la directory delle migration&quot;
  exit -1
fi

export ZEND_TOOL_INCLUDE_PATH_PREPEND=${MIGCLASSDIR}

${ZF_BIN_DIR}/zf.sh run migration ${1}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://claudio.cicali.name/post/2010/03/the-new-providers-discovery-for-zend-tool-1-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Come fare i test sui controller con Zend Framework</title>
		<link>http://claudio.cicali.name/post/2009/06/come-fare-i-test-sui-controller-con-zend-framework/</link>
		<comments>http://claudio.cicali.name/post/2009/06/come-fare-i-test-sui-controller-con-zend-framework/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 12:54:33 +0000</pubDate>
		<dc:creator>claudio</dc:creator>
				<category><![CDATA[Articoli]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://claudio.cicali.name/?p=454</guid>
		<description><![CDATA[Lo ammetto, pietà di me: non sono un grandissimo esperto di test. Non ne ho mai fatti troppi e probabilmente neppure troppo belli. Nel mio ultimo progetto, iniziato da poco, ho rimesso mano alla questione e unilateralmente deciso di applicarmi meglio a farli funzionare. Il progetto è scritto in PHP ed usa Zend Framework (da [...]]]></description>
			<content:encoded><![CDATA[<p>Lo ammetto, pietà di me: non sono un grandissimo esperto di test. Non ne ho mai fatti <em>troppi</em> e probabilmente neppure troppo belli.</p>
<p>Nel mio ultimo progetto, iniziato da poco, ho rimesso mano alla questione e unilateralmente deciso di applicarmi meglio a farli funzionare. Il progetto è scritto in PHP ed usa <a href="http://framework.zend.com">Zend Framework </a>(da qui in avanti solo ZF). Premetto che quello che segue è probabilmente inutile a chi non ha già una certa esperienza con quel framework.</p>
<p>ZF è sempre stato <em>testabile</em> predilegendo tra i vari framework <a href="http://www.phpunit.de">PHPUnit</a>. Questa scelta si è spinta recentemente fino ad offrire un componente (<a href="http://framework.zend.com/manual/en/zend.test.html">Zend_Test</a>) che estende le classi base dei test di PHPUnit in modo da esporre ai test dei controller tutti gli oggetti di alto livello necessari per lavorare in modo estremamente <strong>pratico</strong>. Questi oggetti sono la $request e la $response, ma anche una pletora di nuovi assert() tra cui alcuni fantastici che usano le estensioni XPath e DOM per verificare il contenuto di una pagina durante un test utilizzando appunto XPath o selettori CSS.</p>
<p>Quando scriveremo un test per un controller di una nostra applicazione, faremo dunque derivare il nostro test dalla classe Zend_Test_PHPUnit_ControllerTestCase.</p>
<p>Quelli che seguono sono consigli derivati dalla mia recente esperienza e non vogliono assolutamente essere una trattazione esaustiva sui test dei quali, come innanzi confessavo, non sono un grande esperto. Ma chi non è un grande esperto di test fa fatica a trovare documentazione di base sui test con Zend Framework.. per cui eccomi qua :)</p>
<p>Prima di tutto risolviamo il problema più grave: PHPUnit va installato (non viene distribuito insieme a ZF).</p>
<p>Si può scegliere di farlo in tre modi:</p>
<ol>
<li>via PEAR</li>
<li>installare il pacchetto della propria distribuzione GNU/Linux</li>
<li>andarsi a prendere direttamente il pacchetto di sorgenti dal sito.</li>
</ol>
<p>Su Ubuntu 9.04, la mia attuale distribuzione, l&#8217;unica possibilità è la 3. Infatti la versione del pacchetto che arriva con la distribuzione è troppo vecchia e Zend_Test fa uso di feature piuttosto recenti (non ci puoi fare niente: ti beccherai subito un errore riguardante il metodo sconosciuto incrementAssertionCounter(). Show stopper.). Anche la 1 non mi è parsa percorribile a causa del mio &#8220;PEAR&#8221; troppo vecchio per la versione di PhpUnit (non ho indagato moltissimo, a dire il vero).</p>
<p>Ho preso dunque il tgz dal sito di PhpUnit, l&#8217;ho scompattato e ho messo tutta la sua directory PHPUnit nella stessa directory dove risiede lo ZF (directory <em>./library</em>, per la cronaca). In questo modo le classi di PHPUnit sono già visibili senza che sia necessario toccare gli include path, grazie agli autoloader di ZF stesso.</p>
<p>Ho creato una directory &#8220;tests&#8221; allo stesso livello della directory &#8220;application&#8221; della mia applicazione, dunque fuori dalla DocumentRoot.</p>
<p>All&#8217;interno di questa directory ho messo, per ora: un file AllTests.php che serve come entry point per eseguire tutti i test e un file TestConfiguration.php che in pratica sostituisce le operazioni da fare nell&#8217;index.php del sito.</p>
<p>Ecco per esempio come potrebbe essere un primo AllTests.php:</p>
<pre class="code">&lt;?php
require 'TestConfiguration.php';

$suite = new PHPUnit_Framework_TestSuite();
$suite-&gt;setName('MyApp');
$suite-&gt;addTestSuite('controllers_IndexControllerTest');
$suite-&gt;addTestSuite('controllers_UsersControllerTest');

PHPUnit_TextUI_TestRunner::run($suite, array());</pre>
<p>Ed ecco invece parte del mio TestConfiguration.php</p>
<pre class="code">
&lt;?php
TestConfiguration::setUp();  

class TestConfiguration
{
  static function setUp()
  {
    define('APPLICATION_ENVIRONMENT', 'test');
    define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/'));

    set_include_path(implode(PATH_SEPARATOR, array(
      realpath(APPLICATION_PATH . '/../library'),
      realpath(APPLICATION_PATH . '/library'),
      realpath(APPLICATION_PATH . '/models'),
      realpath(APPLICATION_PATH . '/modules/default/forms'),
      get_include_path(),
    )));

    require_once 'Zend/Loader/Autoloader.php';
    $loader = Zend_Loader_Autoloader::getInstance();
    $loader-&gt;setFallbackAutoloader(true);
  }

  static function setUpDatabase()
  {
    // TODO
  }
}  
</pre>
<p>Nella directory tests/controllers metterò i test dei controller. Per esempio, un inizio di un test per una home page potrebbe essere:</p>
<pre class="code">class controllers_IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

  public function testHomePageIsASuccessfulRequest()
  {
    $this-&gt;dispatch('/');

    // Tests there are no exceptions on the home page
    $this-&gt;assertFalse($this-&gt;response-&gt;isException());

    $this-&gt;assertController('session');
    $this-&gt;assertAction('new');

    $this-&gt;assertQueryCount('#LoginForm', 1);
  }

}</pre>
<p>A questo punto non resta che eseguire i test. Da linea comando sarà sufficiente lanciare un <strong>php AllTests.php</strong> direttamente dalla directory tests.</p>
<p>Un altro punto di attenzione è dovuto al fatto che PHPUnit inizia molto presto a  scrivere in console e questo potrebbe dare fastidio a particolari operazioni di ZF. Nella fattispecie mi sto riferendo al malefico errore &#8220;Headers already sent&#8221;.</p>
<p>Quello che succedeva a me era che in una preDispatch() di un action plugin, a fronte di una particolare situazione, effettuavo un header(&#8220;Location &#8230;&#8221;). Questa operazione non andava a buon fine proprio per il motivo di cui sopra (e tutti i test fallivano miseramente); per cui, se la vostra applicazione ha un redirect HTTP in un plugin prima dell&#8217;effettivo dispatch, potreste incorrere in un errore del genere. La soluzione (almeno per me) è stata quella di non fare la redirect ma di modificare il controller e la action della corrente request. Stesso risultato, ed anche pienamente testabile.</p>
]]></content:encoded>
			<wfw:commentRss>http://claudio.cicali.name/post/2009/06/come-fare-i-test-sui-controller-con-zend-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

