symfony1.4でhello, world

symfony1.4のインストールはPractial Symfonyの1日目の通りにすれば出来ます。ここでは"Hello, World"とだけ出すモジュールを作って、URLルーチンとそのテスト(機能テスト)をすることを目指します。つくるものはCodeZineにあるやつのアップデート版と考えても良いと思います。

1日目のように、プロジェクトを作って、それからアプリケーション、モジュールを作ります。


php symfony generate:project practice
php symfony generate:app frontend
php symfony generate:module frontend helloworld

モジュールなどのひな形ができたので、機能テストを実行してみます。

php symfony test:functional frontend helloworldActions

テストに失敗します。このテストの内容は"/sfproject/practice/test/functional/frontend/helloworldActionsTest.php"にあります。これを次のように書き換えます。

<?php
include(dirname(__FILE__).'/../../bootstrap/functional.php');
$browser = new sfTestFunctional(new sfBrowser());
$browser->
  get('/')->

  with('request')->begin()->
    isParameter('module', 'helloworld')->
    isParameter('action', 'index')->
  end()->

  with('response')->begin()->
    isStatusCode(200)->
    checkElement('h1', 'Hello, World')->
  end()
;

"http://example.com/"にアクセスしたら、h1タグに"Hello, World"が挟まれているというような内容です。テンプレート"sfprojects/practice/apps/frontend/modules/helloworld/templates/indexSuccess.php"が空なのでテストにパスするように書き加えます。

<html>
<title>hello, symfony</title>
<body>
<h1>Hello, World</h1>
</html>

次に"sfprojects/practice/apps/frontend/modules/helloworld/actions/actions.class.php
"がデフォルトページに移動させているので、これをコメントアウトします。

class helloworldActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    //$this->forward('default', 'module');
  }
}

あとは"/sfprojects/practice/apps/frontend/config/routing.yml"を編集すれば、テストにパスします。

default_index:
  url:   /:module
  param: { action: index }


php symfony test:functional frontend helloworldActions
# get /
ok 1 - request parameter module is helloworld
ok 2 - request parameter action is index
ok 3 - status code is 200
ok 4 - response selector h1 matches Hello, World
1..4
# Looks like everything went fine.