9951 explained code solutions for 126 technologies


phpunitHow to use named arguments in PHPUnit?


Named arguments are used to pass parameters to a function in PHPUnit. They are used to make the code more readable and easier to maintain.

Example

<?php

class MyTest extends \PHPUnit\Framework\TestCase
{
    public function testNamedArguments()
    {
        $this->assertEquals(
            'Hello World',
            myFunction('Hello', 'World')
        );
    }
}

function myFunction($first, $second)
{
    return $first . ' ' . $second;
}

Output example

Hello World

Code explanation

  1. $this->assertEquals('Hello World', myFunction('Hello', 'World')); - This line is used to call the myFunction function with two named arguments, 'Hello' and 'World'.

  2. function myFunction($first, $second) - This line is used to define the myFunction function with two parameters, $first and $second.

Helpful links

Edit this code on GitHub