Wednesday, October 29, 2014

Packaging your Box in Vagrant

  • Set up your virtual machine
    • vagrant up
      
  • List your virtual machines
    • $ VBoxManage list vms
      "tesselact_default_1413389341047_169" {7ca66929-48d7-4e36-9237-011b5113c07b}
      "php-salt-box_default_1414574460667_45040" {a872c748-bf3f-46e1-9cd7-db7b7c1b302d}
      
  • Create the package
    • vagrant package --base php-salt-box_default_1414574460667_45040
      
  • You'll find your packaged box in a package.box file

Tuesday, September 9, 2014

Unit test / Test doubles

Test doubles
"Test Double is a generic term for any case where you replace a production object for testing purposes" - Martin Fowler
  • Dummy
    • What does it do?
      • It does almost nothing. You just need it (for example) to pass as a parameter to a constructor.
    • Example?
      • A dummy class has a method which returns null.
  • Stub
    • What does it do?
      • Kind of dummy. It does a thing. It returns a specific value.
    • Example?
      • A stub class has a method which returns true.
  • Spy
    • What does it do?
      • Kind of stub. Saves any kind of things what you might need to check at the end of your test.
    • Example? 
      • A spy class saves whether its "foobar" method has been called.
  • Mock
    • What does it do?
      • Kind of spy. Does behavioural testing. It knows what behaviour is expected. What function was called, with what arguments, when and how many times.
    • Example?
      • A mock expects the "validate" method will run 2 times and will return true.
  • Fake
    • What does it do?
      • Business behavioural testing. It can return different values based on different parameters.
    •  Example?
      • A fake class has a method which returns true if the logged in user's name (a parameter) is "root" else returns false.
 
It is beneficial to read uncle Bob's article.

References
http://blog.8thlight.com/uncle-bob/2014/05/14/TheLittleMocker.html

Thursday, August 21, 2014

Set up a Symfony2 dev environment in 10 minutes with Vagrant

Let's keep it simple. It is just a quick draft how can you set up a Symfony2 development environment in a few minutes.

I'll use Vagrant (you'll need version 1.5 at least) and my dev box to create the desired environment.

The box main components are: Ubuntu14.04, Nginx1.6, Php5.5Fpm, Xdebug, Composer, MySql5.5, NodeJs0.10.30, Redis and it has PhpMyAdmin, Webgrind and RedisCommander.

So the steps are:
  • Create the Vagrantfile
    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "adammbalogh/lemp"
    
      config.vm.network "private_network", ip: "192.168.33.10"
      config.vm.synced_folder "app/", "/var/www/app/"
    
      config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", "1512"]
        vb.customize ["modifyvm", :id, "--cpuexecutioncap", "85"]
      end
    end
    
    
  • mkdir app
  • vagrant up
  • vagrant ssh
  • cd /var/www/app
  • composer create-project symfony/framework-standard-edition . '~2.5'
  • nano app/AppKernel.php (and add these two methods)
    public function getCacheDir()
    {
        return '/tmp/symfony/cache/' . $this->environment;
    }
    
    public function getLogDir()
    {
        return '/tmp/symfony/log/' . $this->environment;
    }
    
  • nano web/app_dev.php (and delete these lines)
    if (isset($_SERVER['HTTP_CLIENT_IP'])
        || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
        || !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1',$
    ) {
        header('HTTP/1.0 403 Forbidden');
        exit('You are not allowed to access this file. Check '.basename(__FIL$
    }
    
  • app/console cache:clear --env=dev (to test it)

Thursday, August 7, 2014

Connect a Usb device through Vagrant

If you want to list your usb devices do this:
$ VBoxManage list usbhost
Host USB Devices:

<none>
On a pure VirtualBox setup it says nothing. Usb devices won't work in your virtual machine until you don't see something in the list. So, you'll need to do two things to fix this:

  • Install VirtualBox Extension Pack (from here)
  • You have to be (your currently logged in user) in the vboxusers group
    • sudo usermod -a -G vboxusers <username>
    • and you have to logout/login from your session!

When you have done with these steps, run the list usbhost again:
$ VBoxManage list usbhost
Host USB Devices:

UUID:               55d6661d-dd23-487c-8f97-23d4cd4f7378
VendorId:           0x064e (064E)
ProductId:          0x8125 (8125)
Revision:           40.16 (4016)
Port:               4
USB version/speed:  2/2
Manufacturer:       SuYin
Product:            Laptop_Integrated_Webcam_HD
Address:            sysfs:/sys/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.5//device:/dev/vboxusb/001/004
Current State:      Busy

UUID:               e96c6aef-f8a7-4f18-a67e-07776c7d839e
VendorId:           0x0bda (0BDA)
ProductId:          0x0129 (0129)
Revision:           57.96 (5796)
Port:               2
USB version/speed:  2/2
Manufacturer:       Generic
...

In Vagrantfile you have to enable the usb option and add a filter based on the desired Manufacturer and Product:
config.vm.provider "virtualbox" do |vb|
  ...
  vb.customize ["modifyvm", :id, "--usb", "on"]
  vb.customize ["modifyvm", :id, "--usbehci", "on"]
  vb.customize ["usbfilter", "add", "0", 
    "--target", :id, 
    "--name", "This is the identifier",
    "--manufacturer", "SuYin",
    "--product", "Laptop_Integrated_Webcam_HD"]
  ...
end  
You are all set.

Run vagrant up and ssh in your vm with vagrant ssh. In your vm run lsusb and you should see the connected usb device!