Categories
PHP

Behat and Mink: Finding and clicking with XPATH and jQuery like CSS Selector

While doing acceptance tests, we often run into scenarios where we need to click on elements which can’t be found or clicked on using simple expressions like – I follow “Login”. Let me give an example, on a page there is an anchor tag with specific id. The anchor tag has no text but an image inside it. In such cases, we are left no other choices than querying the dom. XPath could be a decent way to find the anchor tag here. Let’s assume that the anchor tag has the id – 14. So the xpath query should be – “//a[@id=’14’]”.

PS: We can use Firebug to test out XPath queries. There is a built in function – $x – on a firebug console, just pass xpath queries as string to this function and it will throw matching elements.

Here’s a step definition I wrote inside my FeaturedContext class. This method takes in a xpath query and clicks on the element:

And how do we use it inside a “feature” file?

UPDATE:

Using a jQuery like CSS Selector is pretty easy. Looks like Mink also allows CSS Selectors by default. Here’s the same method with CSS Selector:

And we use it in the feature file like:

The test should fail in case a matching element was not found. Let me know if you run into any issues or find even better or elegant ways to do this.

Categories
Mac PHP

Mac OSX, nginx and php-cgi: doing it the quick and dirty way!

I usually use Apache and it’s PHP5 module for my day to day php development. However, here came a time, when I needed to work on a project that involved using nginx. Installing nginx was easy with homebrew:

However, the recommended way to use php with nginx is using php-fpm. Since I use the apache module most of the time and building php with fpm builds it “–without-apache”, I didn’t want to go for anything complex for just one project at hand. I knew I could use the native “php-cgi” module, so I just googled, found few tips and set it up.

This is my nginx configuration:

Basically, all I did was change the root element to point to my current “~/Sites” directory which I use as the htdocs for apache. And I’m using unix sockets for better performance. Please read the nginx docs if you don’t understand any of the directives but I’m pretty confident that all of them are more or less self explanatory.

Now, we bind php-cgi to the socket mentioned in our nginx conf file. That’s quite straightforward:

Once we have updated the nginx configuration and started the php-cgi process, we should restart nginx:

This should restart nginx and all files with the “.php” extension will be parsed by php.

If you get “no input file specified”, check your nginx configuration and make sure that the paths are all correct. Specially this one:

Also check that if the root element is defined in appropriate scope for the block to have access to. If you still run along some issues, do a quick google or ask in stackoverflow. And feel free to experiment with different nginx features, specially url rewrites 🙂

Categories
PHP

Symfony2 Forms and Doctrine Entity: Unique field values validation

If you’re using Symfony2 forms paired with Doctrine entities, you might sometimes need to validate fields so that the values in that field is always unique. Say – “username” field or “email” field. Two users must not have the same username or email.

There is a form validation constraint – “Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity” which integrates nicely with your entity and allows unique values validation when the form is submitted. Let’s look at some code samples to see how it works:

Here, the email and username fields will be checked for duplicate values and the defined message will be displayed if duplicate values are found.