忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/03/19 12:42 |
Symfony-8日目その2
今回はDoctrineモデルクラスのユニットテストを行います。
データベースに接続したりしないといけないので前回より少々面倒です。

テスト動作は今まで作成してきたdev環境でも本番用のprod環境でもなく、test環境で動作します。
現在データが入っているデータベースはdev環境なので、これではデータベースを用いたテストができません。

ということでMySQLにtest用のデータベースを作成します。
> mysqladmin -uroot -proot create jobeet_test

Symfonyにtest環境用の接続設定を作成します。
> php symfony configure:database --name=doctrine --class=sfDoctrineDatabase --env=test "mysql:host=localhost;dbname=jobeet_test" root root

config/databases.ymlを見てみると、test用のデータベース定義が追加されています。
フィクスチャに書いてあるデータをtest環境に追加します。。
> php symfony doctrine:insert-sql --env=test

データの投入が終わりました。


ここから先は一気にいきます。

まずdata/fixtures/ディレクトリにあるファイルを全部test/fixtures/ディレクトリにコピペ。
test/bootstrap/Doctrine.phpおよびtest/unit/model/JobeetJobTest.phpを作成。

test/bootstrap/Doctrine.php
1
2
3
4
5
include(dirname(__FILE__).'/unit.php');
$configuration = ProjectConfiguration::getApplicationConfiguration(
    'frontend', 'test', true);
new sfDatabaseManager($configuration);
Doctrine::loadData(sfConfig::get('sf_test_dir').'/fixtures');
test/unit/model/JobeetJobTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
include(dirname(__FILE__).'/../../bootstrap/Doctrine.php');
$t = new lime_test(3, new lime_output_color());
 
$t->comment('->getCompanySlug()');
$job = Doctrine::getTable('JobeetJob')->createQuery()->fetchOne();
$t->is($job->getCompanySlug(), Jobeet::slugify($job->getCompany()),
 '->getCompanySlug() return the slufor the company');
 
$t->comment('->save()');
 
$job = create_job();
$job->save();
$expiresAt = date('Y-m-d', time() + 86400 * sfConfig::get('app_active_days'));
$t->is(date('Y-m-d', strtotime($job->getExpiresAt())), $expiresAt,
 '->save() updates expires_at if not set');
 
$job = create_job(array('expires_at' => '2008-08-08'));
$job->save();
$t->is(date('Y-m-d', strtotime($job->getExpiresAt())), '2008-08-08',
 '->save() does not update expires_at if set');
 
function create_job($defaults = array()){
  static $category = null;
 
  if (is_null($category)){
    $category = Doctrine::getTable('JobeetCategory')
      ->createQuery()
      ->limit(1)
      ->fetchOne();
  }
 
  $job = new JobeetJob();
  $job->fromArray(array_merge(array(
    'category_id'  => $category->getId(),
    'company'      => 'Sensio Labs',
    'position'     => 'Senior Tester',
    'location'     => 'Paris, France',
    'description'  => 'Testing is fun',
    'how_to_apply' => 'Send e-Mail',
    'email'        => 'job@example.com',
    'token'        => rand(1111, 9999),
    'is_activated' => true,
  ), $defaults));
 
  return $job;
}

テストを実行
>php test/unit/model/JobeetJobTest.php
1..3
# ->getCompanySlug()
ok 1 - ->getCompanySlug() return the slufor the company
# ->save()
ok 2 - ->save() updates expires_at if not set
ok 3 - ->save() does not update expires_at if set
Looks like everything went fine.


テストに成功しました。


何をどうやって何のテストに成功したの?

さあ?

全く意味がわかりません。


他のDoctrineクラス用のテストとかとても書けそうにないのでパス。
PR


2010/01/25 22:45 | Comments(0) | TrackBack() | PHP

トラックバック

トラックバックURL:

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<アルファブロガー・アワード2009ゲーム部門のノミネートが酷い件 | HOME | 買ったものリスト 2010/01/24>>
忍者ブログ[PR]