Source: adithya.dev

Data Providers in PHPUnit
When I built a https://www.arbeitnow.com/tools/salary-calculator/germany using Laravel, I started covering the code with Unit Tests using PHPUnit. This particularly came in handy when I had to cover more edge cases as I was able to trust the calculation.

When you want to add multiple assertions, you would be tempted to add another assertion like this: public function test_add() { $actual = (new Operations)->add(1, 2); $this->assertEquals(3, $actual); $actual = (new Operations)->add(3, 4); $this->assertEquals(7, $actual); } // Or a test per method public function test_add_first() { $actual = (new Operations)->add(1, 2); $this->assertEquals(3, $actual); } public function test_add_second() { $actual = (new Operations)->add(3, 4); $this->assertEquals(7, $actual); }

/** * @dataProvider addProvider */ public function test_add($a, $b, $expected) { $actual = (new Operations)->add($a, $b); $this->assertEquals($expected, $actual); } public function addProvider() { return array( array(1, 2, 3), array(4, 5, 7), ); }

In the test cases above with a Data Provider, we can simplify it even further by inlining the data provider.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners