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
410
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
490
XP本読書会 - AgileJapan 2018 高知サテライト
kazweda
0
370
Local environments of WordPress for macOS
kazweda
0
610
CI Study with Vagrant & Chef
kazweda
0
540
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
CQRS/ESのクラスとシステムフロー ~ RailsでフルスクラッチでCQRSESを組んで みたことから得た学び~
suzukimar
0
190
テスト分析入門/Test Analysis Tutorial
goyoki
11
2.6k
バランスを見極めよう!実装の意味を明示するための型定義 TSKaigi 2025 Day2 (5/24)
whatasoda
2
760
AIコーディングの本質は“コード“ではなく“構造“だった / The essence of AI coding is not “code” but "structure
seike460
PRO
2
710
知識0からカンファレンスやってみたらこうなった!
syossan27
5
320
型付け力を強化するための Hoogle のすゝめ / Boosting Your Type Mastery with Hoogle
guvalif
1
220
『Python → TypeScript』オンボーディング奮闘記
takumi_tatsuno
1
120
MLOps Japan 勉強会 #52 - 特徴量を言語を越えて一貫して管理する, 『特徴量ドリブン』な MLOps の実現への試み
taniiicom
2
510
バリデーションライブラリ徹底比較
nayuta999999
1
380
Parallel::Pipesの紹介
skaji
2
840
rbs-traceを使ってWEARで型生成を試してみた After RubyKaigi 2025〜ZOZO、ファインディ、ピクシブ〜 / tried rbs-trace on WEAR
oyamakei
0
920
ワンバイナリWebサービスのススメ
mackee
10
7.3k
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Designing for Performance
lara
608
69k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
A Tale of Four Properties
chriscoyier
159
23k
VelocityConf: Rendering Performance Case Studies
addyosmani
329
24k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
Being A Developer After 40
akosma
91
590k
Practical Orchestrator
shlominoach
187
11k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.6k
KATA
mclloyd
29
14k
Balancing Empowerment & Direction
lara
1
81
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
42
2.3k
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