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
49
Retrospective 2019
kazweda
0
380
Selenium Grid on Azure and LambdaTest
kazweda
1
470
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
ベクトル検索システムの気持ち
monochromegane
31
9.9k
RuboCop: Modularity and AST Insights
koic
1
150
趣味全開のAITuber開発
kokushin
0
200
技術選定を未来に繋いで活用していく
sakito
3
110
Kubernetesで実現できるPlatform Engineering の現在地
nwiizo
3
1.9k
Strategic Design (DDD)for the Frontend @DDD Meetup Stuttgart
manfredsteyer
PRO
0
110
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
1.3k
メモリウォールを超えて:キャッシュメモリ技術の進歩
kawayu
0
1.9k
Preact、HooksとSignalsの両立 / Preact: Harmonizing Hooks and Signals
ssssota
1
1.4k
[NG India] Event-Based State Management with NgRx SignalStore
markostanimirovic
1
120
DomainException と Result 型で作る型安全なエラーハンドリング
karszawa
0
890
プロダクト横断分析に役立つ、事前集計しないサマリーテーブル設計
hanon52_
2
410
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Practical Orchestrator
shlominoach
186
10k
Writing Fast Ruby
sferik
628
61k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
RailsConf 2023
tenderlove
30
1.1k
How to Ace a Technical Interview
jacobian
276
23k
Building Flexible Design Systems
yeseniaperezcruz
329
38k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
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