Home > Enterprise >  php unit testing dealing with input text
php unit testing dealing with input text

Time:08-21

I am trying to run some basic unit tests for the below code. The trouble i am facing is i am not sure how to deal with the input_text.

sample input_text:

module1,60newlinemodule2,50newlinemodule3,40

this is the class i would like to test:

<?php
function getMaxMin($input_text)
{
    $lines = explode("newline", $input_text);
    $module_marks=array();
    foreach ($lines as $line) {
       $line_array = explode(",", $line);
       $module_marks_array = array("module"=>$line_array[0], "marks"=>$line_array[1]);
       array_push($module_marks,$module_marks_array);
    }

    usort($module_marks, function($a, $b) {
          return $b['marks'] <=> $a['marks'];
    });

    $maxModule = $module_marks[0]['module'] . ', ' . $module_marks[0]['marks'];
    $minModule = $module_marks[count($module_marks)-1]['module'] . ', ' . $module_marks[count($module_marks)-1]['marks'];
    $maxminModule = $maxModule . 'newline' . $minModule;

    return $maxminModule;
}

this is what i have so far for test:

<?php
use PHPUnit\Framework\TestCase;
require('functions.inc.php');

class TotalTest extends TestCase {

public function testgetMaxMin(){
        $this->assertSame(60, 40,  getMaxMin('m1,60newlinem2,50newlinem3,40'));
}
}

the actual i believe is incorrect, just unsure how to deal with input like this. Any resources or help is much apricated.

CodePudding user response:

put the string on a variable, assertSame takes 3 parameters

assertSame(mixed $expected, mixed $actual[, string $message = ''])

It throws an error if the first two variables do not have the same type AND value.

The inverse is assertNotSame

All the assertions: PHPUnitAssertions

  •  Tags:  
  • php
  • Related