node-mocha-chai-header

Installing Mocha + Chai

When installing Mocha, most readmes that I have found were telling me to install it globally. I tried to find another way to do this as I dislike installing anything globally. What it I want one project to have one version of Mocha and another project to have another version?!

Therefore, with this in mind, here is how I go about implementing it.

First we need to update our package.json file

{
  ...
  "scripts": {
    ...
    "test": "./node_modules/.bin/mocha"
  },
  ...
  "devDependencies": {
    "mocha": "*",
    "chai": "*"
  }
  ...
}

Once this is done, you will need to run an “npm update”

The “test” value in “scripts” will allow us to later run our tests by typing “npm test” in our root directory.

Now we need to create a test folder. In the root of project create /test/ and then create a file called test.js

Assuming that you have a file in your project called ./lib/helloWorld.js create test/lib/helloWorld.js and then update your test.js to look like so:

/**
 * test.js
 */

require("./lib/helloWorld");

This will now mean that when npm test is run, it will run your helloWorld.js tests.

My helloWorld.js in my project (i.e. not the HelloWorld.js within test/lib/). Is very simple, it looks like so:

/**
 * lib/helloWorld.js
 */

exports.say = function() {
   return "hello world!";
};

In my test, I’m going to assert that when I use the above ‘say’ method, I get a string. Therefore my test/lib/helloWorld.js should look like this:

/**
 * test/lib/helloWorld.js
 */

describe("helloWorld Library", function(){
    var assert = require("chai").assert,
        helloWorld = require("./../../lib/helloWorld");


    describe("HelloWorld say method", function() {
        it("should return a string", function(){
            assert.typeOf(helloWorld.say(), "string", "HelloWorld.say returns a string");
        });

    });
});

Its now all setup. Run ‘npm test’ to run your test and your output should be similar to this:

Jamess-MacBook-Pro:test-project james$ npm test

> test-project@0.0.1 test /Users/james/PhpstormProjects/test-project
> mocha

hello world!


  helloWorld Library
    HelloWorld say method
      ✓ should return a string 


  1 passing (3ms)

Jamess-MacBook-Pro:test-project james$

Happy Unit Testing!

Fork me on GitHub

WordPress 4 initial thoughts

Today is supposedly the day that WordPress 4 is out. In the last 2 years I haven’t really used WordPress but I still like to read what is going on with it. I have heard interesting rumours about the direction that WordPress plan on taking the framework, primarily the rumour in which it will run on node.js rather than PHP. If this rumour where true, I personally think that it would be the start of the end of PHP’s popularity.

When I worked as a WordPress developer, the 3 big CMSes were WordPress, Drupal and Joomla. Joomla was one that we completely ignored and I haven’t met a Joomla developer in quite some years! Were I worked we favoured WordPress over Drupal, mainly as we found it easier to develop and we found that our customers preferred the admin interface.

I think WordPress is great, it does what it says on the tin, you can have a blog/website in minutes and it is very user friendly. I think I spent about 5 minutes setting up this site, hence you may noticed that I’m using the default theme! However there are good examples of how it can be implemented such as the Playstation blog and Eurogamer.

With this new iteration of WordPress I have only read about the new features that have been implemented and they all seem to be improvements to the user interface. I saw one review in which the reviewer stated that this iteration felt more like version 3.1 than version 4. I think I agree with that.

What I expected, were more technical features such as PHPUnit integration as standard or dependency management using composer.

I am sure that WordPress know much better than I do about what their customers want. And after all half the appeal of WordPress is it’s simplicity and thus lower development costs. However, I must admit that I prefer the route that Drupal has opted for. In the forthcoming Drupal 8 they have opted for composer to manage their dependencies, PHPUnit integration and they are using Symfony2 components.

If WordPress continues in this direction, I think it will remain the CMS of choice by designers, however, Drupal will be the choice for developers.

Penguins

IceMarkt

I thought I would say a couple of words about the open source project that I have been working on recently, IceMarkt. I’m not too happy with the name, I just wanted a unique name! A friend of mine recently mentioned that for marketing there was not a open source platform available, thus I decided why not try and start one. First things first, it would have to be able to upload and manage contacts. Secondly it must be able to email those contacts. And thirdly, the email content must be manageable. With this is mind, I created a CRUD for mail recipients and would send them all an email sent with Swift Mailer. The emails that were sent were a twig template. Because it was a standard twig file, this didn’t seem very usable for an end user. It was for this reason I decided to store the templates in a new table and then quickly add ckeditor to the page to make it more user friendly. At this point I realised that the end user would have to specify what the  email profile would be. i.e. who should the email say it is from and what are the reply to details. A quick another CRUD was created and Doctrine managed the one-to-many relation for me. (An email profile can be used on multiple templates, further to this, every email template must have a profile). Finally, to make sure that this wouldn’t be used for malicious spamming, I added a public facing bundle just so that I could provide and url for a user to unsubscribe from the mailing list. After all this worked end-to-end, I did my initial commit along with the Vagrant and puppet script I was using for my environment. (Yes, I cheated, I used puphpet.com !) With everything working, I started on the unit tests. Nothing worse than low code coverage from your tests! Once the unit tests were done, this allowed me to optimise IceMarkt an check that I wasen’t breaking it by accident! The biggest optimisation was to send the emails in batches so that if you had a very large contact list, the page wouldn’t time out. Now that this is done, I am refactoring the controllers so that doctrine is injected so that I can write unit tests instead of functional tests for my controllers. There are many things still left to do and realistically I just did this project because I want to play with vagrant, puppet and doctrine2 outside of industry. And it never hurts to add more to your portfolio. I am happy for anyone to volunteer and add to the project however I won’t merge anything in that breaks a unit test, doesn’t pass with PHPCodeSniffer set to PSR-2 or jslint errors when using the jslint config in the root directory! Features that still need implementing are:

  • Tracking – at the moment I’m relying on the user including google tracking codes in the markup of their email templates.
  • Custom fields for recipients – As anyone that uses IceMarkt will most likely want to store different types of information about a recipient custom fields seems appropriate.
  • Filter recipients for sending – Once custom fields are done it would be nice to query the recipients the receive the marketing so that it is no longer blind marketing! i.e. Only send to recipients where custom field ‘gender’ is ‘male’.
  • Authentication – Currently there is no authentication so anyone could add/edit/delete recipients, templates and profiles. I need to create a table of users and have a way to manage their roles.
  • Turn of mapping with NFS – as the VagrantFile is using NFS for the drive mapping, the VM will not currently work in windows.

IceMarkt can be found on github: https://github.com/irvingswiftj/iceMarkt

Alcaidesa

Toodles England, Ola Spain!

Time for a new venture. I have moved from the UK and now reside in Spain. A big change when you can’t speak the local language, however also a great opportunity to learn a new non programming language!

I hope this new move will mean that I’m out and about more than thus will be able to blog about fun activities and nice images as well as my normal programming rants!

SpotifyLibImg

My Spotify Meta Api Library for PHP

About 6 weeks ago I completed the first version of my library to work with Spotify’s Meta Api. However I realised that I had forgotten to mention it on my blog. You can view to source on github ( https://github.com/irvingswiftj/spotifyLib )

Features:

  • Search for songs/artists/albums/tracks on Spotify
  • get info on a song/artist/album/track by uri
  • Unit tests built with PHPUnit
  • Using composer for phpunit and guzzle dependency

Still to do:

  • Implement PSR-0 autoloading
  • Go through my code with PHP-cs!

Any feedback is always welcome.  I posted this on reddit, and I was really impressed with feedback from the community ( http://www.reddit.com/r/PHP/comments/20ju0v/created_a_php_library_for_using_spotifys_api/ ).

Setting up Xdebug’s remote debug with PHPStorm

Introduction

As promised in a reddit discussion, I thought I would put up an article on setting up xdebug’s remote debugger with PHPStorm. I have had to assist multiple developers in setting it up and I suspectits because there is a lack of ‘how tos’ on this topic on the interwebs.

I have broken this ‘how to’ into two sections, the server setup and the PHPStorm setup. In my case my server happens to be Debian, so if you are using a different flavour of linux, please adjust as necessary. Likewise with PHPStorm version, my screenshots are from version 7, as far as I can remember, they should differ since version 5.

The server settings

Firstly, check that you have Xdebug installed. My method of checking this is to run the following from terminal:

$ php -i | grep "xdebug"

If you get a result, fanstastic, else you will need to install xdebug, this should do it for you:

$ apt-get install php5-xdebug

Now you will need to change you php settings, so open the file /etc/php/conf.d/xdebug.ini (hopefully using vim and we all know that nano is an inferior editor!!!).

It should contain one line which will be the setting for the zend_extension. After that line add the following settings:

debug.remote_host= 192.168.0.10
xdebug.remote_port=9000
xdebug.max_nesting_level=200
xdebug.remote_enable=1
xdebug.idekey="PHPSTORM"
xdebug.remote_autostart = 1

N.B. if you are unsure of what your remote host IP address is, create a php file in which you echo $_SERVER[‘REOMTE_ADDR’]; and run it from the machine you use PHPStorm with. It will output the correct IP address.

Finally, don’t forget to restart you web server (apache in this example):

$ service apache2 restart

The settings in PHPStorm

Go to your PHPStorm settings and make sure that you xdebug port is set to 9000 (if you can not use 9000, I sometimes 9124 else 9125; just make sure it is the same in you ini settings on your server).

PHPStorm Settings Panel
PHPStorm Settings Panel

Save those settings and now go to Run>Edit Configurations… where you will need to add a remote debug instance (see screenshot below)

add remote debug instance
add remote debug instance

You can name this remote debug instance what ever you want, just make sure the IDE key matches what is in xdebug.ini on your server (in this example we used ‘PHPSTORM’).

remote debug instance settings
remote debug instance settings

It likely you won’t have a server setup, if that is the case you can add one by clicking the ‘…’ button. In my project when adding a server, I always need to specify the mappings so you might find you have to do the same.

Finally, once have saved those settings, click on the listen button so that it goes from:

xdebug listening icon
xdebug listening icon

to:

xdebug not listening icon
xdebug not listening icon

If you add a breakpoint now, and load up the page that you want to debug, it should now stop at your breakpoint (I would probably test this with the first line of your index.php to make sure your breakpoint is getting hit!).

Hopefully you will now be a god at debugging the hardest of bugs!

Other hints

If you are not using xdebug, i highly recommend that you turn it off on your server, as if you computer is turned off and you access anything PHP via your webserver it will hang like make as it will be looking for the machine with the IP address specified.

I recommend that you check out xdebug’s profiler if you haven’t already and use it with a tool like kcachegrind else webgrind. Again, I would turn off profiling when not using it as the files it creates on each page load are vast and if you have a small server you may find you run out of storage very quickly!

My Lack of Blogging + Apology!

So, I have written anything to my blog for about 18months, one of my new year’s resolution is to blog more. I have notice that this site is painfully slow, hence new hosting is high on my list of priorities.

I was also tired of the old theme on this blog, so I have set it to WordPress’ new twenty fourteen theme until I rustle up the time to create something custom.

At least my resolution for this year was greater that 1024×768!

James Irving-Swift's portfolio and random blogging