Donoven Rally
Donoven Rally

Reputation: 1700

using data from config.php in phpunit class

i am trying to write some tests in which i am using data from config.php file. the files are in the same folder. i know i can't include files from inside a class but i also don't know what is the correct way to use this delicate data in my phpunit test class.

the class looks like this:

<?php

require dirname(__FILE__).'/../../src/vendor/autoload.php';
$config = require "config.php";

use GuzzleHttp\Client;

echo $config['base_url']; //HERE I GET THE DATA

class ApiAdTest extends PHPUnit_Framework_TestCase
{--

    public function testApiAd_postAd()
    {
        $client = new Client(['base_uri' => --DATA FROM CONFIG.PHP--]);
        $response = $client->post(--DATA FROM CONFIG.PHP--, ['form_params' => [
          'name' => 'bellow content - guzzle testing',
          'description' => 'guzzle testing ad - demo'
          ]]);
        $code = $response->getStatusCode();
        $data = json_decode($response->getBody());
        $adId = $data->id;

        $this->assertEquals($code, 200);


        return $adId;
    }

what will be te correct way to use this data from config.php in my testing class??

UPDATE: config.php content:

<?php

return array(
    'base_url' => 'http://10.0.0.0/',
    'path' => 'api/ad/',
);

Upvotes: 2

Views: 1159

Answers (3)

Matteo
Matteo

Reputation: 39410

You can load the config.php file in the setup method of the testcase and assign the value to a class attribute, then you can use in the test method as follow:

<?php

require dirname(__FILE__).'/../../src/vendor/autoload.php';

use GuzzleHttp\Client;


class ApiAdTest extends PHPUnit_Framework_TestCase
{

    protected $config;

    protected function setUp()
    {
        $this->config = require("config.php");

    }


    public function testApiAd_postAd()
    {
        $client = new Client(['base_uri' =>$this->config['base_url'] ] );
        $response = $client->post($this->config['path'], ['form_params' => [
          'name' => 'bellow content - guzzle testing',
          'description' => 'guzzle testing ad - demo'
          ]]);
        $code = $response->getStatusCode();
        $data = json_decode($response->getBody());
        $adId = $data->id;

        $this->assertEquals($code, 200);


        return $adId;
    }

}

Hope this help

Upvotes: 1

Jamesking56
Jamesking56

Reputation: 3901

As a native PHP solution, I suggest using composer's autoloader to load an extra configuration file. For this you can define a files array in your composer.json file like so:

{
    "autoload": {
        "files": ["config.php"]
    }
}

Then in your configuration file, I would create a global function to access configuration variables:

<?php

function config($item) {
    $config = [
        // Config variables here
    ];

    return $config[$item];
}

After running composer dumpautoload, you can then access config() globally in your application to grab configuration items.

(You may want to adapt this solution for your requirements, I prefer using my previous answer)

Note: If you're using source control, do not commit your config.php file as this is a security risk

Upvotes: 0

Jamesking56
Jamesking56

Reputation: 3901

If you're using composer, a package I like to use for setting configuration is called PHP Dotenv.

It uses a .env file in the root of your project and allows you to add your own settings to PHP's $_ENV array. You can then pull these out by using getenv() wherever you wish to use them.

You can then add .env to your .gitignore file so that you don't accidentally commit it to source control.

Upvotes: 0

Related Questions