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
350
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
41
Retrospective 2019
kazweda
0
310
Selenium Grid on Azure and LambdaTest
kazweda
1
380
XP本読書会 - AgileJapan 2018 高知サテライト
kazweda
0
340
Local environments of WordPress for macOS
kazweda
0
520
CI Study with Vagrant & Chef
kazweda
0
500
About python community in Ehime - PyCon mini Hiroshima 2016
kazweda
0
1k
Agile開発徐々に進行中
kazweda
0
160
Other Decks in Programming
See All in Programming
フロントエンドパフォーマンス 入門
shouta2
7
1.5k
Some Quick Ideas To Improve Your Tests ( #jassttokyo )
teyamagu
PRO
2
2.3k
脱・初心者!脱・マネコン!AWS CDKを使ってみませんか!?
har1101
0
180
Deno に Web 標準 API を実装する / Implementing Web Standard API to Deno
petamoriken
0
350
両面どころかインフラもTSでできるよ ~ 全方位TypeScriptによるプロダクト開発 ~
myfinder
9
2.5k
PHPerKaigi 2024〜10年以上動いているレガシーなバッチシステムを Kubernetes(Amazon EKS) に移行する取り組み〜
tshinowpub
1
220
htmx is fun!
codehex
2
190
15分間でふんわり理解するDocker @ Matsuriba MAX
ukwhatn
PRO
1
340
【KMC春合宿2024】実装視点で見るNeural Radiance Fields
runningoutrate
0
150
導入から5年が経って見えた Datadog APM 運用の課題
bgpat
2
540
イベントストーミングによるオブジェクトモデリング・オブジェクト指向プログラミングの適用・開発プロセスの変遷・アーキテクチャの変革 / Object modeling with Event Storming.
nrslib
12
3.2k
incrementalモデルの理解を深める
ikkimiyazaki
2
640
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
8
8.2k
Rebuilding a faster, lazier Slack
samanthasiow
72
8.2k
Making Projects Easy
brettharned
106
5.4k
KATA
mclloyd
14
11k
For a Future-Friendly Web
brad_frost
170
8.9k
4 Signs Your Business is Dying
shpigford
174
21k
YesSQL, Process and Tooling at Scale
rocio
160
13k
The World Runs on Bad Software
bkeepers
PRO
60
6.6k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
19
1.9k
Building Applications with DynamoDB
mza
88
5.6k
Pencils Down: Stop Designing & Start Developing
hursman
115
11k
Thoughts on Productivity
jonyablonski
57
3.8k
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