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
380
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
44
Retrospective 2019
kazweda
0
340
Selenium Grid on Azure and LambdaTest
kazweda
1
410
XP本読書会 - AgileJapan 2018 高知サテライト
kazweda
0
350
Local environments of WordPress for macOS
kazweda
0
540
CI Study with Vagrant & Chef
kazweda
0
520
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
Clean Architecture by TypeScript & NestJS
ryounasso
0
150
유연한 Composable 설계
l2hyunwoo
0
380
feature環境をGitHub ActionsとCloudFormationでいい感じに管理する
nealle
2
310
大規模マルチテナントを解決するYugabyteDBという選択肢
nnaka2992
1
250
CSC307 Lecture 10
javiergs
PRO
0
310
最古の関数型言語「Lisp」ことはじめ / lisp_in_kamiyama
uhooi
1
190
ピグパーティにおけるMongoDB CommunityバージョンからAtlasへの移行事例
10969hotaka
0
130
Rubyのパフォーマンスプロファイリングの改善 / Enhancing performance profiling for Ruby
osyoyu
1
410
スクラムマスターって孤独じゃないですか?
yoshitaroyoyo
1
140
DynamoDB コスト最適化っぽいことの基本 with Terraform
kuro_kurorrr
2
250
TiDB Serverless ~理想のServerless DBを考える~
soso_15315
1
160
ドメイン駆動設計の実践
masuda220
PRO
17
5.1k
Featured
See All Featured
In The Pink: A Labor of Love
frogandcode
139
22k
Making Projects Easy
brettharned
111
5.7k
The Cost Of JavaScript in 2023
addyosmani
31
4.7k
Being A Developer After 40
akosma
72
580k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
16
1.6k
How to name files
jennybc
67
96k
Build your cross-platform service in a week with App Engine
jlugia
227
17k
Building Applications with DynamoDB
mza
89
5.8k
A Modern Web Designer's Workflow
chriscoyier
689
190k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
277
13k
Designing for Performance
lara
604
67k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
105
6.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