Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Datetime tests with cakephp/chronos

Datetime tests with cakephp/chronos

#phperkaigi 2019でのLT内容です。
cakephp/chronosを利用して強いテスタビリティを手に入れる話

hideki kinjyo

March 31, 2019
Tweet

More Decks by hideki kinjyo

Other Decks in Programming

Transcript

  1. TL;DR • cakephp/chronos
 A standalone DateTime library originally based off

    of Carbon • Date(calendar) / DateTimeͷศརͳॲཧΛఏڙ ͢Δ • ೔࣌ॲཧΛҕৡ͢Δ͜ͱͰɺ
 γεςϜ೔࣌ʹґଘ͠ͳ͍ʮݱࡏ೔࣌ʯΛॲཧ͕ Մೳʹ
  2. ͍ͭͰʹɺ
 ૬खํ΋ॻ͖׵͑ͯ͠·͓͏ use Cake\Chronos\Chronos; $template = 'itsumono_hanbai_page'; if (Chronos::now() >=

    new Chronos(START_SALE_DATETIME_STR) ) { $template = 'special_taro_sale'; } render($template);
  3. testίʔυΛॻ͍ͯΈΔ /** * @test */ public function セール開始前() { Chronos::setTestNow('2019-03-10');

    $targetPageUrl = '/'; $actual = get($targetPageUrl); $this->assertTitleIs( '太郎君のお弁当箱', $actual ); }
  4. testίʔυΛॻ͍ͯΈΔ /** * @test */ public function セール開始前() { Chronos::setTestNow('2019-03-10');

    $targetPageUrl = '/'; $actual = get($targetPageUrl); $this->assertTitleIs( '太郎君のお弁当箱', $actual ); } ͜͜ʹೖΕͨ஋͕
 A$ISPOPTOPX AʹͳΔ
  5. testίʔυΛॻ͍ͯΈΔ /** * @test */ public function セール期間中() { Chronos::setTestNow('2019-04-01');

    $targetPageUrl = '/'; $actual = get($targetPageUrl); $this->assertTitleIs( '太郎君のお弁当箱〜7周年ありがとう!〜', $actual ); } ֘౰ظؒʹ͓͍ͯ͸
 ϖʔδ಺ίϯςϯπ͕
 ࠩ͠ସΘΔ͜ͱΛظ଴͢Δ
  6. testίʔυΛॻ͍ͯΈΔ /** * @test */ public function セール終了後() { Chronos::setTestNow('2019-05-01

    06:59:59’); $targetPageUrl = '/'; $actual = get($targetPageUrl); $this->assertTitleIs( ‘太郎君のお弁当箱', $actual ); } ࣌ࠁϨϕϧ·ͰݻఆՄೳ
  7. ΞάϨογϒͳ࢖͍ํ <?php use Cake\Chronos\Chronos; if (!function_exists('now')) { function now() {

    return Chronos::now(); } } // usage if (now()->isSunday()) { goto doNidone(); }
  8. ·ͱΊ(1/2) • ςετ = ҆৺ײ • ςελϒϧ = ઃܭͷ࿩ɺςεςΟϯάϑϨʔ ϜϫʔΫ(etc)ଆͷදݱྗͷ࿩

    • ࣌Λࢧ഑͢Δͱͱͬͯ΋ศར
 ಛʹςετɺ͜ΕͰTDD΋΍Γ΍͍ͧ͢ʂ