39
Vagrant HOW TO SET UP A VAGRANT DEVELOPMENT SYSTEM

Vagrant WordCamp Hamilton

Embed Size (px)

Citation preview

Page 1: Vagrant  WordCamp Hamilton

VagrantHOW TO SET UP A VAGRANT DEVELOPMENT SYSTEM

Page 2: Vagrant  WordCamp Hamilton

Paul Bearne @pbearneFreelance Sr. Full Stack WordPress Developer Plugin author of Author Avatars List ( http://wordpress.org/plugins/author-avatars/ )WP Site Verification tool ( http://wordpress.org/plugins/wp-site-verification-tool/ )Core contribs In WordPress versions 3.9, 4.0 and 4.2

Page 3: Vagrant  WordCamp Hamilton

Real-time Publishing for WordPress

Livepress.com

Page 4: Vagrant  WordCamp Hamilton

eLearning Solutions Powered by WordPress

UncannyOwl.com

Page 5: Vagrant  WordCamp Hamilton

Where do you develop your sites?

Page 6: Vagrant  WordCamp Hamilton

Test PHP versions add_action( 'init', function () {

remove_post_type_support( 'post', 'editor' );

}, 99 );◦ Needs PHP 5.2 + : Parse error: syntax error, unexpected T_FUNCTION

$result = $this->multidimensional( &$root, $keys, true );◦ Breaks in PHP 5.4 + : Fatal error: Call-time pass-by-reference has been removed

Page 7: Vagrant  WordCamp Hamilton

Why use Vagrant?

No need to have a web server installed.You can match the configuration of production server.Project isolation - one Vagrant setup per project.Version isolation - more than one version of WordPress.Works the same on PC/Mac or Linux.

Page 8: Vagrant  WordCamp Hamilton

Vagrant

“Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the 'works on my machine' excuse a relic of the past.”

http://www.vagrantup.com/about.html

Page 9: Vagrant  WordCamp Hamilton

Host Computer

Virtualbox

Shared Folder/usr/www/site C:/user/document/code

Point Source control Here

Point editor / IDE

Here

Run Unit Tests Here

Point Web

browser Here

Telnet client / vagrant ssh

Page 10: Vagrant  WordCamp Hamilton

Install

Vagrant: http://downloads.vagrantup.com/ VirtualBox: https://www.virtualbox.org/ Plus a configuration file

Note: Sometimes problems with latest version VirtualBox on windows

Page 11: Vagrant  WordCamp Hamilton

THE BASIC COMMAND LINE

$ vagrant init precise32 http://files.vagrantup.com/precise32.box $ vagrant up

$ vagrant destroy

Page 12: Vagrant  WordCamp Hamilton

ScriptingAUTOMATE THE CONFIG

Page 13: Vagrant  WordCamp Hamilton

Options• varying-vagrant-vagrants (VVV) : https://github.com/Varying-Vagrant-Vagrants/VVV

• vip-quickstart : https://github.com/Automattic/vip-quickstart

• Salty-WordPress : https://github.com/humanmade/Salty-WordPress

• Mercury Vagrant (HGV) : https://github.com/wpengine/hgv

• roots/bedrock-ansible : https://github.com/roots/bedrock-ansible

• Roll your own https://puphpet.com/

• more ….

Page 14: Vagrant  WordCamp Hamilton
Page 15: Vagrant  WordCamp Hamilton
Page 16: Vagrant  WordCamp Hamilton

DEMOJUST RUN IT

Page 17: Vagrant  WordCamp Hamilton

VVV bits : internal commands Default WP Login/password: admin/password

Db account: wp/wp

xdebug_on/ xdebug_off (via ssh shell)

Makepot

Page 18: Vagrant  WordCamp Hamilton

VVV bits : Tools Hosts updaterhttps://github.com/cogitatio/vagrant-hostsupdatervagrant plugin install vagrant-hostsupdater

Guest Editionshttps://github.com/dotless-de/vagrant-vbguestvagrant plugin install vagrant-vbguest

Dashboardhttps://github.com/topdown/VVV-Dashboard

Page 19: Vagrant  WordCamp Hamilton

VVV bits : vagrant add-ons VVV Site Wizardhttps://github.com/aliso/vvv-site-wizardvvv -a create -n mysite -d mysite.dev -v 3.9.1 –x

VV Site Wizard https://github.com/bradp/vv New fork of VVV site wizard

Vagrant Manager for OS X. http://vagrantmanager.com

Page 20: Vagrant  WordCamp Hamilton

Config demoTHE VAGRANTFILE FROM VARYING-VAGRANT-VAGRANTS

Page 21: Vagrant  WordCamp Hamilton

VVV on windows problemsDos line endssudo dos2unix /home/vagrant/bin/*

SVN fetch fails with DB errorssvn cleanup need to be run

Windows 8 – hosts file need to be unprotect in window defender

SVN version can be different

Use “git bash” not “cmd”

Page 22: Vagrant  WordCamp Hamilton

Vagrant Commands Vagrant up

◦ Start

Vagrant Suspend / resume◦ pause/play

Vagrant halt◦ turn off

Vagrant destroy◦ wipeout

Vagrant status◦ is it up

Vagrant int◦ create empty config file

Vagrant box◦ manage

Page 23: Vagrant  WordCamp Hamilton

Lets be tidy : use a “Customfile”

config.vm.synced_folder "../hello", "/srv/www/wordpress-default/wp-content/plugins/hello", :owner => "www-data", :mount_options => [ "dmode=775", "fmode=774" ]

config.vm.synced_folder "../hello", "/srv/www/wordpress-trunk/wp-content/plugins/hello", :owner => "www-data", :mount_options => [ "dmode=775", "fmode=774" ]

config.vm.synced_folder "../hello", "/srv/www/wordpress-develop/src/wp-content/plugins/hello", :owner => "www-data", :mount_options => [ "dmode=775", "fmode=774" ]

Page 24: Vagrant  WordCamp Hamilton

PHPUnit

Page 25: Vagrant  WordCamp Hamilton

https://make.wordpress.org/core/handbook/automated-testing/

<?php

class SampleTest extends WP_UnitTestCase {function testSample() {

// replace this with some actual testing code$this->assertTrue( true );

}}

Page 26: Vagrant  WordCamp Hamilton

PHPUnit Commands phpunit --list-groups

phpunit –groups ******

phpunit –verbose

phpunit –filter text

Page 27: Vagrant  WordCamp Hamilton

Core Test demo

Page 28: Vagrant  WordCamp Hamilton

Vagrant ssh

$ cd /srv/www/wordpress-devolop/phpunit --group functions.phpphpunit --group 28666

Run WordPress core unit tests

Page 29: Vagrant  WordCamp Hamilton

PHPUnit plugin demo

Page 30: Vagrant  WordCamp Hamilton

Vagrant ssh

$ cd /srv/www/wordpress-trunk/$ wp scaffold plugin-tests hello$ cd wp-content/plugins/hello/$ phpunit

Add Unit tests to a plugin

Page 31: Vagrant  WordCamp Hamilton

class MyTestClass extends PHPUnit_Framework_TestCase { public function setUp() { \WP_Mock::setUp(); }

public function tearDown() { \WP_Mock::tearDown(); }}

\WP_Mock::onFilter( 'the_content' )->with( 'Windows Rocks!' )->reply( 'Apple Rocks!' );

wp_mock

https://github.com/10up/wp_mock

Page 32: Vagrant  WordCamp Hamilton

public function test_content_filter() { \WP_Mock::onFilter( 'the_content' )->with( 'Windows Rocks!' )->reply( 'Apple Rocks!' );

$post = new stdClass; $post->post_content = 'Windows Rocks!'; setup_postdata( $post );

$content = get_the_content();

$this->assertEquals( 'Apple Rocks!', $content ); }

wp_mock test example

https://github.com/10up/wp_mock

Page 33: Vagrant  WordCamp Hamilton

Vagrant ssh

$ makepot wp-plugin /srv/www/wordpress-trunk/wp-content/plugin/hello/

$ makepot wp-theme /srv/www/wordpress-trunk/wp-content/themes/twentyfifteen/

Makepot on VVV

Page 34: Vagrant  WordCamp Hamilton

Makepot demo

Page 35: Vagrant  WordCamp Hamilton

1. Use an IDE

2. Configure your IDE

3. Enable xdebug

4. Set breakpoint

5. Walk the code and variables

6. Fix the code

Debuging

https://wordpress.tv/2014/08/03/aaron-holbrook-introduction-to-ides-and-debugging/

Page 36: Vagrant  WordCamp Hamilton

Questions?

Page 37: Vagrant  WordCamp Hamilton

What is coming next? Docker : www.docker.com Slides: http://flightless.us/wcmia2015/

Not ready for windows yet

Page 38: Vagrant  WordCamp Hamilton

Hire [email protected]

Page 39: Vagrant  WordCamp Hamilton

Slides@ http://www.slideshare.net/pbearneEmail: [email protected]