Visithor, Testing Your Routes Without Pain

| Comments

Do you like testing? I like testing. Well, in fact I like testing once I have understood how the engine I am using works. Until this moment, everything is a completely disaster.

And because this learning curve, many of my personal projects have reached an MVP without any type of testing environment. And sincerely, I don’t really like this scenario.

Many years ago I was thinking about a simple and fast tool to test specific routes, expecting specific HTTP codes and providing an easy environment of ensuring properly your HTTP layer.

So… I present you Visithor, a PHP based library that provides you this functionality, with a simple configuration definition and a very easy way of installation.

Let’s see an example.

Execute Visithor

With this lines of code you will download Visithor, ready to be used.

1
2
3
4
$ git clone git@github.com:visithor/visithor.git
$ cd visithor
$
$ build/visithor.phar

You can download it as a global executable

1
2
3
4
5
$ git clone git@github.com:visithor/visithor.git
$ cd visithor
$ cp build/visithor.phar /usr/local/bin/visithor
$
$ visithor

Creating your configuration

As easy as it seems. This is a random configuration file with a random set of urls. If your visithor.yml file is placed in the same folder than you are, then you don’t have to specify its location. Otherwise, please use the [—config|-c] option.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
defaults:
    #
    # This value can be a simple HTTP Code or an array of acceptable HTTP Codes
    # - 200
    # - [200, 301]
    #
    http_codes: [200, 302]

urls:
    #
    # By default, is there is no specified HTTP Code, then default one is used
    # as the valid one
    #
    - http://google.es
    - http://elcodi.io

    #
    # There are some other formats available as well
    #
    - [http://shopery.com, 200]
    - [http://mmoreram.com, [200, 302]]
    - [http://nonexistingurl.com, [200]]

In this case, the execution result is 0

Executing Visithor

Once you have it installed and you have a configuration file, let’s see what happens!

1
2
3
4
5
6
7
8
9
10
11
12
13
$ visithor visithor:go

Visithor by Marc Morera and contributors.

Configuration read from /var/www/my/project

OK [200] http://google.es
OK [200] http://elcodi.io
OK [200] http://shopery.com
OK [200] http://mmoreram.com
KO [404] http://nonexistingurl.com

Time: 1002 ms, Memory: 15.5Mb

As you can see, we have an error. We expect a 200 code when we call http://nonexistingurl.com but the real response is 404, so it fails. Then, the execution result is 1.

Integrations

You can integrate Visithor with your Symfony projects right now. I expect some other integrations will be available soon.

If you use this bundle, then you will be able to define your routes not with the final path but with the route name and a bunch of parameters, as it is shown here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
defaults:
    #
    # This value can be a simple HTTP Code or an array of acceptable HTTP Codes
    # - 200
    # - [200, 301]
    #
    http_codes: [200, 302]

urls:
    #
    # This Bundle adds some extra formats
    #
    - [store_homepage, 200]
    - [[store_category_products_list, {'slug': 'women-shirts', 'id': 1}], 200]
    - [[store_category_products_list, {'slug': 'another-name', 'id': 1}], 302]
    - [[store_homepage, {_locale: es}]]

This project is being so useful for me, I expect it will be for you as well :)

Travis

Of course, you can add Visithor in your travis tests suite. You can use the [—format|-f] option to change the format and use dots

1
2
3
4
script:
    - bin/behat -fprogress --tags="~javascript"
    - bin/phpunit -c app
    - bin/visithor --format=dots visithor:go

Comments