Summer Sale! All accounts are 50% off this week.

Hashim's avatar

Testing that outputs of an algorithm match precomputed outputs

I previously posted this on SO and it currently has 5 downvotes because that community seems to expect everyone asking a question to be an expert who already knows the ins and outs of the topic they're asking about. Here I am in a second attempt to actually get an answer.

I have some Laravel methods that take a string ID as input, perform some slightly complicated string and arithmetic manipulations on it, then return the output. I just finished updating the project to PHP 8, and since the migration page warns to watch out for both arithmetic and bitwise operations, both of which I'm making use of, I want to verify these algorithms still work in PHP 8 and that they always keep working.

The problem is Laravel ships with 4 or 5 different types of testing, and I'm unsure which one would be the most suitable for how I want to test the algorithms and how to implement it. My ideal process for testing would be to have 100 or so precomputed outputs (which I would generate separately from the tests and can hardcode into them), then check that for each ID, the output matches the precomputed output.

Which of Laravel's several testing frameworks and PHPUnit's hundreds of Assert* functions would I use for something like this?

0 likes
8 replies
tykus's avatar
tykus
Best Answer
Level 104

You seem to be describing Unit Tests, so you can use the PHPUnit TestCase parent class - you don't need framework features.

You can use PHPUnit's dataProvider method to provide the known inputs and expected outputs, and then use PHPUnit's assertEquals assertion method to assert that you get the expected output for a given, know input:

/**
 * @test
 * @dataProvider yourDataProviderMethod
 */
public function it_produces_the_expected_result($input, $output)
{
	$this->assertEquals($output, yourFunction($input);
}

public function yourDataProvider()
{
	return [
		// arrays of input/output
   ];
}
1 like
Hashim's avatar

@tykus This is perfect. By framework features I assume you mean the Laravel testing packages? So I should just install and use PHPUnit independently without relying on Laravel-specific stuff?

tykus's avatar

@Hashim PHPUnit is already installed; and you will maybe want the Laravel TestCase for feature tests where you are testing features/integrations. I meant that in your specific test class (since it is testing units of functionality) can use only PHPUnit\Framework\TestCase rather than Tests\TestCase - the latter bootstraps the application

Hashim's avatar

@tykus Thank you for your help, it's crazy to me that the people at SO refused to provide such a simple answer.

Hashim's avatar

@tykus Maybe I'm missing something but I can't seem to replace yourFunction() in your code with the method from my own model, and neither the documentation or a search has helped bring anything up. How would this be done?

tykus's avatar

@Hashim even if the method is in your model, you should have no issues testing by calling the method on an instance of your model. Just new up the model and use that instance.

1 like
Hashim's avatar

@tykus Perfect, I assumed there would be something more complicated to it.

sr57's avatar

Downvote on SO is the least bad method for the will to be a reference area ... you have to know which sort of question are relevant in it, or elsewhere, here for instance.

That said, snce you need to do this test only once, I should just store the result of of calculations in a db table table and compare versus the previous one.

Test framework are useful to ensure that your code is always Ok versus its life cycle (modifications).

Please or to participate in this conversation.