Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
How to build a WordPress plugin by TDD
Search
kazweda
November 07, 2015
Programming
0
400
How to build a WordPress plugin by TDD
WordBench愛媛 - 秋のお茶会のプレゼン資料です。
VCCW環境でTDDを試してみました。
kazweda
November 07, 2015
Tweet
Share
More Decks by kazweda
See All by kazweda
Ruby, Ruby on Rails をオンラインで学ぶ
kazweda
0
50
Retrospective 2019
kazweda
0
380
Selenium Grid on Azure and LambdaTest
kazweda
1
480
XP本読書会 - AgileJapan 2018 高知サテライト
kazweda
0
360
Local environments of WordPress for macOS
kazweda
0
580
CI Study with Vagrant & Chef
kazweda
0
530
About python community in Ehime - PyCon mini Hiroshima 2016
kazweda
0
1.1k
Agile開発徐々に進行中
kazweda
0
170
Other Decks in Programming
See All in Programming
個人開発の学生アプリが企業譲渡されるまで
akidon0000
0
950
Empowering Developers with HTML-Aware ERB Tooling @ RubyKaigi 2025, Matsuyama, Ehime
marcoroth
2
780
Optimizing JRuby 10
headius
0
470
スモールスタートで始めるためのLambda×モノリス(Lambdalith)
akihisaikeda
2
300
サービスレベルを管理してアジャイルを加速しよう!! / slm-accelerate-agility
tomoyakitaura
1
190
七輪ライブラリー: Claude AI で作る Next.js アプリ
suneo3476
1
120
これだけは知っておきたいクラス設計の基礎知識 version 2
masuda220
PRO
24
6.6k
サービスクラスのありがたみを発見したときの思い出 #phpcon_odawara
77web
4
690
メモリウォールを超えて:キャッシュメモリ技術の進歩
kawayu
0
1.9k
REALITY コマンド作成チュートリアル
nishiuriraku
0
110
Orleans + Sekiban + SignalR でリアルタイムWeb作ってみた
tomohisa
0
120
Make Parsers Compatible Using Automata Learning
makenowjust
2
5.4k
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.1k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.4k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
KATA
mclloyd
29
14k
Adopting Sorbet at Scale
ufuk
76
9.3k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.8k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
How GitHub (no longer) Works
holman
314
140k
Transcript
プラグインで開発 at 秋のお茶会 #wbehime
参考書
WordPress 1. コンテンツ管理 -> WordPress の基本機能 2. デザインのカスタマイズ -> テーマ制作で対応
3. 機能追加 -> 独自プラグインで対応
Plugin Handbook goo.gl/XK0sRI
Plugin 作成で最低限必要なもの plugins$ mkdir sfd-handbook sfd-handbook$ vi sfd-handbook.php <?php /*
Plugin Name: My Toolset */
テストコードを書こう 参考までに、昨年の Coderetreat "Global Day of Coderetreat 2014 in Matsuyama"
agile459.doorkeeper.jp/events/16066 ペア( 2 人一組)でプログラムの問 題を TDD で解くトレーニングのイ ベント。世界同時(同日)開催。
TDD のメリット ワンアクション (phpunit 実行 ) で、 一連の動作確認ができます。 ブラウザで所定のページを開いて動 作確認して、でも別のページが壊れ
ていて ... 、のようなデバッグ作業が 格段に効率化できます。
テスト駆動開発の参考記事 WP-CLI+PHPUnit を使った WordPress プラグインのユニットテスト(1) https://firegoby.jp/archives/5498 WP-CLI+PHPUnit を使った WordPress プラグインのユニットテスト(2)
https://firegoby.jp/archives/5511
環境が整っている前提で ... 仮想環境(べいぐらんと)を起動 $ vagrant up 仮想環境に ssh で入る $
vagrant ssh wordpress ディレクトリへ移動 $ cd /var/www/wordpress WP-CLI でプラグインのひな形を生成 $ wp scaffold plugin my-counter
自動生成されたフォルダ
PHPUnit 用の WordPress 環境準備 プラグインのディレクトリに移動 $ cd $(wp plugin path
--dir my-counter) テスト用 WordPress 環境を作成 $ bash bin/install-wp-tests.sh wordpress_test root 'wordpress' localhost latest mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'wordpress_test'; database exists' (テスト用DBはすでに作られている様子。)
PHPUnit を実行してみる
自動生成されたテストコード プラグインフォルダ内の test/test-sample.php <?php class SampleTest extends WP_UnitTestCase { function
test_sample() { // replace this with some actual testing code $this->assertTrue( true ); } }
テスト用のクラスを追加 $ touch tests/test-my-counter.php ショートコードを実行すると0が返る <?php class MyCounterTest extends WP_UnitTestCase
{ function test_my_counter_shortcode() { $count = do_shortcode( '[my_counter]' ); $this->assertEquals('0', $count); } }
テストを実行 - 失敗 $ phpunit
shortcode の処理を実装 class MyCounter { public function __construct() { add_shortcode(
'my_counter', array( $this, 'mycounter_shortcode' )); } public function mycounter_shortcode($p) { return '0'; } }
テストを実行 - 成功 $ phpunit
プラグインを有効にして、
Widget に設置してみる
プラグインのテスト駆動開発 表示されない ...
Widget でショートコードを使う フィルターが必要でした。 class MyCounterTest extends WP_UnitTestCase { function test_my_counter_shortcode()
{ $count = do_shortcode( '[my_counter]' ); $this->assertEquals('0', $count); $this->assertTrue( has_filter('widget_text', 'do_shortcode') ); } }
Widget でショートコードを使う テスト実行。
Widget でショートコードを使う フィルターを追加。 class MyCounter { public function __construct() {
add_shortcode( 'my_counter', array( $this, 'mycounter_shortcode' )); add_filter('widget_text', 'do_shortcode' ); } ...
Widget でショートコードを使う Widget に表示できた。
カウントアップ class MyCounterTest extends WP_UnitTestCase { ... function test_counter_increment() {
$cnt0 = do_shortcode( '[my_counter]' ); $cnt1 = do_shortcode( '[my_counter]' ); $this->assertEquals( $cnt1, $cnt0 + 1 ); } ... カウントアップのテストコード追加。
カウントアップのテスト実行 未実装なのでエラー( 0 のまま)
カウントアップを実装 class MyCounter { ... public function mycounter_shortcode($p) { $count
= get_option( 'my_counter' ) + 1; update_option( 'my_counter', $count ); return $count; } ... 今回は WordPress のオプション値を利用。
カウントアップのテスト実行 カウントアップできた。
続きはデモにて。
参考資料 Testing Added Hooks http://goo.gl/I2VySn 関数リファレンス /add_filter https://goo.gl/McBgYS