Untitled

 avatar
unknown
plain_text
a month ago
2.4 kB
3
Indexable
<?php

namespace App\Tests\pest\Unit\Content\Service\Entity\Controlpanel\ModelMetaData;
use App\Content\Service\Entity\Controlpanel\ModelMetaData;

class Calculator
{
    public function add(int $a, int $b): int
    {
        if (!is_int($a) || !is_int($b)) {
            throw new \InvalidArgumentException('Both inputs must be integers.');
        }
        return $a + $b;
    }
}

class CalculatorService
{
    private Calculator $calculator;

    public function __construct(Calculator $calculator)
    {
        $this->calculator = $calculator;
    }

    public function doubleSum(int $a, int $b): int
    {
        return $this->calculator->add($a, $b) * 2;
    }
}

beforeEach(function() {
    $this->calculator = mock(Calculator::class);
    $this->service = new CalculatorService($this->calculator);
});




describe('test that checks if math works', function() {

    dataset('numbers', [
        [1, 2, 3],
        [2, 2, 4],
        [3, 2, 5],
        [4, 2, 6],
        [5, 2, 7],
        [6, 2, 8],
        [7, 2, 9],
        [8, 2, 10],
        [9, 2, 11],
        [10, 2, 12],
        [10, 1, 11]
    ]);

    dataset('edge cases', [
        [1, 2, 3],
        [-1, -2, -3],
        [-7, 8, 1]
    ]);

    it('if true is true', function() {
        expect(true)->toBeTrue();
    });
    it('checks if the values in the array ammount to the right sum :key', function($a, $b, $sum) {
        expect($a+$b)->toBe($sum);
    })->with('numbers');

    it('checks for edge cases', function($a, $b, $sum) {
        expect($a+$b)->toBe($sum);
    })->with('edge cases');


    it(' checks the functioning of calculatorservice', function($a, $b, $sum) {
        $this->calculator->shouldReceive('add')
            ->with($a, $b)
            ->andReturn($sum);
        expect($this->service->doubleSum($a, $b))->toBe($sum*2);
    })->with('numbers');
});




describe(' test that checks for invalid types', function() {
    dataset('nullable inputs', [
        [null, null, 0],
        [1, null, 1],
        [null, 2, 2],
    ]);

    it('checks if the exception is thrown', function($a, $b, $sum) {
        $this->calculator->shouldReceive('add')
            ->with($a, $b)
            ->andThrow(new \InvalidArgumentException('Both inputs must be integers.'));
        expect(fn() => $this->service->doubleSum($a, $b))->toThrow(\TypeError::class);
    })->with('nullable inputs');
});





















Editor is loading...
Leave a Comment