Getting Started with Selenium and PHP

Note that I am using OS X 10.8.3 so YMMV.

Install PHPUnit

pear channel-discover pear.symfony.com
sudo pear install pear.symfony.com/Yaml
sudo pear install --alldeps pear.phpunit.de/PHPUnit

Install Selenium extension for PHPUnit

sudo pear install phpunit/PHPUnit_Selenium

Here’s a test to show you how to use Selenium to go to google with a firefox driver and enter in a search query, just as an example

 

<?php
class TestGoogle extends PHPUnit_Extensions_Selenium2TestCase {
	public function setUp(){
		$this->setHost('localhost');
		$this->setPort(4444);
		$this->setBrowser('firefox');
		$this->setBrowserUrl('http://google.com');
	}

	public function testEnterText(){
		$this->url("/");
		//if the next line ever causes the test to fail, check that the class name of the input on google.com is still gbqfif
		$text = $this->byClassName('gbqfif')->value('Selenium Made Me Do It!!'); 
		//on the next line replace @USERNAME with your username or it probably will fail, you can always remove this line if you do not want a screenshot
		$this->screenshot('/Users/@USERNAME/screenshot.'.time().'.png'); 
	}

	public function screenshot($filepath) {
	    $filedata = $this->currentScreenshot();
	    file_put_contents($filepath, $filedata);
	}
}

It should return something like this:

selenium-screenshot

 

 

 

 

 

 

 

 

Have fun testing!


htaccess for angular.js projects so that you don’t get a 404 on some refreshes!

Just make this the htaccess :)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)       /index.php/#/$1 
</IfModule>

Installing git on CentOS 6.4 with cPanel

So, I ran into an issue installing git on a box that had CentOS 6.4 on it… I kept pulling out my hair… Turns out…

Per http://serverfault.com/questions/337399/cant-install-git-on-a-centos-6-0-x64 — to install git on a machine with CentOS 6.4 and cPanel you can install git using:

yum install git --disableexcludes=main --skip-broken

Have fun out there!


Getting the Taxonomy and Term from within archive.php

This one just takes advantage of get_query_var, see below:

$taxonomy_name = get_taxonomy( get_query_var( 'taxonomy' ) )->labels->name;
$term_name = get_term_by( 'slug',$term, get_query_var('taxonomy') )->name;


Use archive.php as the search results page

This one is easy too…
add_action( 'template_redirect', function(){
if (is_search()) {
include("archive.php");
die();
}
});

It’s important to note this uses an anonymous function which requires PHP 5.3+.


Getting a Unix Timestamp in JavaScript (same as time() in PHP)

It’s easy…

var timenow = Math.round((new Date()).getTime() / 1000);


Automatically Include All Class Files in Subdirectory (e.g. inc/ or class/)

Easy as pie!

Here’s an example to include all PHP files in a subdirectory named class.

N.B. Make sure you are one level below the class folder.

 

foreach (glob(plugin_dir_path(__FILE__) . "class/*.php") as $filename) include $filename;

Ignoring Files Locally in Git

I wanted to keep some WordPress plugins on my local installation of WordPress that did not end up on my live server: mostly debugging tools (for example: 1, 2 and 3). I could have essentially added them to git ignore, but that would get pushed upstream, so I essentially needed to locally exclude these files/directories. Luckily there’s a fairly painless way to do so on a local machine (this is written from the vantage of a Linux terminal):

  1. cd into your .git directory typically resides your base directory (note dotfiles may be hidden).
  2. From your .git directory cd into the info directory.
  3. Use the text editor of your choosing to edit the file “exclude” in the info directory.
  4. Add the specific paths that you wish to exclude.
  5. Here’s what my exclude file looks like for the aforelinked plugins to be excluded:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
plugins/debug-bar
plugins/debug-objects
plugins/debug-queries

 

 

 


Permenant Alias in OS X Terminal

I use MAMP on my local as part of my development environment. When dropping to terminal to perform an action (e.g. git commit/checkout/etc.) I was getting tired of manually changing my directory to the default MAMP root (cd /Applications/MAMP/htdocs/wp-content) so I figured that I’d add an alias using the following:

echo 'alias mamp="cd /Applications/MAMP/htdocs/wp-content"' >> ~/.bashrc && source ~/.bashrc

This seemed to work but much to my chagrin the next time I rebooted or opened a new terminal the alias appeared to be gone, bringing me back to step one. I finally figured out how to get this to work permanently …

  1. issue nano ~/.bash_profile to edit your bash profile
  2. add in alias mamp="cd /Applications/MAMP/htdocs/wp-content" and close and save

voila!


Simple Add a Class to the Body Tag in WordPress

This is something that is extremely easy once you realize that the hook body_class exists. It is important to pass an variable argument (it can be anything, just has to be there), the hook will pass the current array of classes to that argument. Simple usage of this hook is as follows:

function add_body_class ($classes) {
    $classes[] = 'myclass';
    return $classes;
}

add_filter('body_class', 'add_body_class');

What this does is add the class “myclass” to the <body> class.

Here’s an example of adding a specific class to the homepage, add this to your functions.php (for a theme) or your plugin functions page (for a plugin):

function add_home_class ($classes) {
   if (is_home()) $classes[] = 'myclass';
    return $classes;
}

add_filter('body_class', 'add_home_class');

This can be adapted in any way shape or form, just change the logic in the if statement that wraps around the filter.

Have fun!