Jump to content

class test issue


purencool

Recommended Posts

I have an issue that with some testing on an Access  class with a method getControlAccess it should only return true or false but as soon as I place a string of any sort

into the return my tests always returns true. Any help to point out if it is the way I am testing or the way I have written the method would be great. In the example

below it would return true.

 

 

In one class I have this method

class Access{
public function getControlAccess(){

	return   'I am stuffed';
}
}

 

In the second class I have this

class AccessTests extends Access{
public function controlAccessTest(){
	$return = "<ul>";
	  
	if ($this->getControlAccess() == TRUE){

		$return .= "<li>getControlAcess should be true and is returning:<b> ". $this->getControlAccess() ."</b></li>";

	} elseif ($this->getControlAccess() == FALSE){

		$return .= "<li>getControlAcess should be false and is returning:<b> ". $this->getControlAccess() ."<b></li>";
	} else {

		$return .= "<li>getControlAcess is really stuffed:<b>". $this->getControlAccess() ."<b></li>";
	}

	$return .= "</ul>";
	return $return;
}

public function setAccessTest($set){
	return $set;
}

}

Link to comment
https://forums.phpfreaks.com/topic/248295-class-test-issue/
Share on other sites

Any text value you feed as the return will be considered true (there are special cases). You should explicitly define the value to be false, or return nothing at all.

 

See: http://php.net/manual/en/language.types.boolean.php

 

If you want the literal (string) value "TRUE," use quotes, otherwise it's boolean.

Link to comment
https://forums.phpfreaks.com/topic/248295-class-test-issue/#findComment-1275025
Share on other sites

No, use this

 

<?php 

$varA = TRUE;
$varB = 'somestring';

if( $varA == TRUE )
echo '$varA == TRUE<br>';
if( $varB == TRUE )
echo '$varB == TRUE<br>';
if( $varA === TRUE )
echo '$varA === TRUE<br>';
if( $varB === TRUE )
echo '$varB === TRUE';

?>

 

Output

$varA == TRUE
$varB == TRUE
$varA === TRUE

Link to comment
https://forums.phpfreaks.com/topic/248295-class-test-issue/#findComment-1275031
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.