Sobes.tech
Junior — Middle

What are the main operations to perform when writing unit tests?

sobes.tech AI

Answer from AI

The main operations when writing unit tests in PHP include:

  1. Preparing test data — creating necessary objects and values for the test.
  2. Calling the tested method or function with the prepared data.
  3. Checking the result — comparing the actual result with the expected one using assertions.
  4. Cleanup — if necessary, releasing resources or resetting state.

Example using PHPUnit:

use PHPUnit\Framework\TestCase;

class CalculatorTest extends TestCase {
    public function testAdd() {
        $calculator = new Calculator();
        $result = $calculator->add(2, 3);
        $this->assertEquals(5, $result);
    }
}