Complete the code to create a basic unit test class in Laravel.
<?php namespace Tests\Unit; use PHPUnit\Framework\TestCase; class ExampleTest extends TestCase { public function testBasicTest() { $this->assertTrue([1]); } }
The assertTrue method expects a boolean true value to pass the test.
Complete the code to run a test that checks if two values are equal.
public function testValuesAreEqual()
{
$expected = 5;
$actual = 2 + 3;
$this->assert[1]($expected, $actual);
}
assertEqual which does not exist.assertSame which is stricter than needed.The correct PHPUnit assertion method to check equality is assertEquals.
Fix the error in the test method to correctly check if a string contains a substring.
public function testStringContains()
{
$string = 'Laravel Framework';
$this->assertStringContainsString([1], $string);
}
The substring check is case-sensitive. The string contains 'Laravel' with uppercase L, so the exact case must be used.
Fill both blanks to create a test that expects an exception to be thrown.
public function testException()
{
$this->expectException([1]::class);
throw new [2]();
}
The test expects an InvalidArgumentException to be thrown, so both places must use the same exception class.
Fill all three blanks to write a test that mocks a class method and asserts it was called once.
public function testMockMethodCall()
{
$mock = $this->createMock([1]::class);
$mock->expects($this->once())
->method([2])
->willReturn([3]);
$result = $mock->process();
$this->assertEquals('done', $result);
}
The test mocks the Processor class, expects the process method to be called once, and returns 'done'.