I have an array of tests for a function:
$testArray = array(
(object)array(
"value" => 12345678901,
"expected" => "123456789012"
),
(object)array(
"value" => "1234567890",
"expected" => false
),
(object)array(
"value" => (int)12345678901,
"expected" => "123456789012"
),
(object)array(
"value" => 1234567890,
"expected" => false
),
(object)array(
"value" => "12345678901",
"expected" => "123456789012"
),
(object)array(
"value" => "123456789012",
"expected" => "123456789012"
),
(object)array(
"value" => "123456789013",
"expected" => false
),
(object)array(
"value" => "1234567890128",
"expected" => "1234567890128"
),
(object)array(
"value" => "1234567890127",
"expected" => false
),
(object)array(
"value" => array(),
"expected" => false
),
(object)array(
"value" => (object)array(),
"expected" => false
),
(object)array(
"value" => array("1234567890127"),
"expected" => false
),
(object)array(
"value" => (object)array("1234567890127"),
"expected" => false
)
);
I run the array through a foreach loop to see which ones work to test a function, and most of them work. The third one should return 123456789012 but instead it returns "-539222987" when I get the value. Why is this happening when the rest work just fine?
Here is the foreach loop. The validUPC function just looks at the upc and makes sure it is a valid one.
foreach($testArray as $key => $test) { // Test each value
$result = App::make("Product")->validUPC($test->value);
var_dump($result);
echo "Result from array member $key was: $result\n";
if($result !== $test->expected) { // Check against expected value
echo "Test FAILED\n";
$success = false; // Returns false if any of them failed
}
else {
echo "Test passed\n";
}
}
The validUPC function begins like this:
public function validUPC($upc = "non") {
try {
// If there is no upc specified, get the product upc
if($upc == "non") {
$upc = $this->upc;
}
// No arrays allowed
if(is_array($upc)) {
return false;
}
// Make it a string
$upc = (string)$upc;
var_dump($upc);
Which returns something like this:
Product.php:99:float 12345678901
Product.php:101:string '12345678901' (length=11)
CatalogTest.php:70:string '123456789012' (length=12)
Result from array member 0 was: 123456789012 Test passed
Product.php:99:string '1234567890' (length=10)
Product.php:101:string '1234567890' (length=10)
CatalogTest.php:70:boolean false
Result from array member 1 was: Test passed
Product.php:99:int -539222987
Product.php:101:string '-539222987' (length=10)
CatalogTest.php:70:boolean false
Result from array member 2 was: Test FAILED