Testing

Clicking and visually checking stuff after code changes is very inefficient. For every change made, something else could break and there’s no way to know other than testing the whole application every single time.
This is where unit testing comes in. The idea is to create a piece of code that tests some functionality. As more tests are created, the developer can launch them all at the same time to check if everything is OK.
Historically this concept evolved into Test-driven Development (TDD). The idea is to write a test before building a new feature. This forces a kind of specification to be created, which sets the rules of how the feature should operate. So the goal of the developer becomes “building the feature to make the test pass”.
Recently this concept further evolved into Behavior-driven Development (BDD). This is the bleeding edge concept/technology at the moment and it’s the stuff I’m using for our tests. BDD places focus on the user behavior, and tests are written as a kind of story. I’ll give you an example of a test I wrote just a moment ago:
Given I login as "student"
  And I go to "/calendar/index"
 When I mouseover "#credits_count"
 Then I should see "Remaining rescheduling credits = 3"
  And I should see "47" in the "#credits_count" element
So as you can see this test is very readable, when I run this, a browser pops up, logs in as a student, goes to the calendar page, mouses over the credits number next to the time and checks that the remaining scheduling credits is 3, and the actual credit count is 47.
These stories/scenarios are very easy to read and write, and revolve around 3 keywords: Given, When, Then.
 
Given is a precondition.
When is an action.
Then is the expected outcome.

Simulate a click on fullcalendar event

For my current project I am using the excellent jQuery calendar plugin fullcalendar by Adam Shaw.

Whilst creating a functional test with phpunit and Selenium, I found that triggering a click on the calendar event was not reaching the procedure I defined for eventClick in fullcalendar. In fact, in order to get the click event to trigger, a mouseover event needs to be triggered on the same element. The correct way is:

$this->waitForElementPresent("css=span.fc-event-time");
$this->mouseOver("css=span.fc-event-time");
$this->click("css=span.fc-event-time");

1. Wait for the element to be present (important if using ajax events fetch).
2. Simulate mouseover.
3. Simulate click.

The “css=span.fc-event-time” locator will always point to the first event in the calendar, that’s ok for this example but if you need more specificity then I believe xpath is better.

Also, the above example also holds true for a js script. You always need to trigger a mouseover before a click!

mysqld dead but subsys locked

Had a problem in my VPS of the mysqld service spontaneously dying.

When I used the command:

sudo service mysqld status

I got a message saying, “mysqld dead but subsys locked”. I didn’t realise this machine didn’t have a swapfile, so in order to fix the problem I ran the following commands:

dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
swapon /swapfile
Open /etc/fstab and add this line /swapfile swap swap defaults 0 0