Tuesday, April 5, 2016

Codeception - How to read YAML file entries


    It has been almost 2 years since my last post. Lately, I've been occupied with PHP unit, REST and acceptance tests.

   Codeception is a testing framework, built with PHP, that allows developers and QA professionals to create all sorts of tests for their projects. The syntax is almost human readable and clean. The framework itself is configurable and uses YAML syntax files to store parameters. This can be very handy at times, the question is, how to access those values.

   There are 1 core class that can help us with this \Codeception\Configuration and the process is very easy.

    Lets have a look at the following config file called api.suite.yml

class_name: ApiTester
modules:
    enabled:
        - \Helper\Api
        - REST:
                depends: PhpBrowser
                url:  'http://my.local.dev'
                part: Json
testdata:
    api_secret: '87897ddhgg'
    api_key: '7367hcfkdjhfkhfdkhf83787'

This file describes the configuration for the API test suite. Most RESTfull APIs use a key and/or  secret. To be able to use the values, the following lines of code will do the job:


$thisConfig = \Codeception\Configuration::config();
$apiSettings = \Codeception\Configuration::suiteSettings('api', $thisConfig);

$apiSecret = $apiSettings['testdata']['api_secret'];
$apiKey = $apiSettings['testdata']['api_key'];


Now we can use the variables in our tests.