PredictionTest.php 3.46 KB
<?php

namespace FootyRoom\Tests\Unit\Predictor;

use FootyRoom\Tests\TestCase;
use FootyRoom\Core\CoreException;
use FootyRoom\Core\Predictor\Prediction;
use FootyRoom\Core\Predictor\PredictionCreated;
use FootyRoom\Core\Predictor\PredictionStakeRaised;

class PredictionTest extends TestCase
{
    protected const USER_ID = 1000;
    protected const MATCH_ID = 2000;
    protected const HOME_SCORE = 1;
    protected const AWAY_SCORE = 3;
    protected const STAKE = 0;

    public function setUp()
    {
        $this->prediction = new Prediction(
            self::USER_ID,
            self::MATCH_ID,
            self::HOME_SCORE,
            self::AWAY_SCORE,
            self::STAKE
        );

        parent::setUp();
    }

    public function test_it_raises_prediction_created_event()
    {
        $this->assertEquals($this->prediction->releaseEvents(), [new PredictionCreated($this->prediction)]);
    }

    public function test_it_doesnt_let_you_make_negative_bet()
    {
        $this->expectException(CoreException::class);
        $this->prediction->bet(-1);
    }

    public function test_it_doesnt_let_you_lower_your_bet()
    {
        $this->prediction->bet(100);
        $this->expectException(CoreException::class);
        $this->prediction->bet(50);
    }

    public function test_it_raises_your_bet_to_new_value()
    {
        $this->prediction->bet(100);
        $this->assertEquals($this->prediction->getStake(), 100);
        
        $this->prediction->bet(200);
        $this->assertEquals($this->prediction->getStake(), 200);
    }

    public function test_it_raises_prediction_stake_raised_event()
    {
        $this->prediction->releaseEvents();
        
        $this->prediction->bet(100);
        $this->assertEquals($this->prediction->releaseEvents(), [new PredictionStakeRaised($this->prediction, 100)]);
        
        $this->prediction->bet(150);
        $this->assertEquals($this->prediction->releaseEvents(), [new PredictionStakeRaised($this->prediction, 50)]);
    }

    public function test_it_lets_you_change_score()
    {
        $this->prediction->change(5, 6);
        $this->assertEquals($this->prediction->getHomeScore(), 5);
        $this->assertEquals($this->prediction->getAwayScore(), 6);
    }

    public function test_it_doesnt_let_you_change_score_if_bet_is_set()
    {
        $this->prediction->bet(100);
        $this->expectException(CoreException::class);
        $this->prediction->change(0, 1);
    }

    public function test_it_doesnt_let_you_set_crazy_scores()
    {
        $this->expectException(CoreException::class);
        $this->prediction->change(20, 30);
    }

    public function test_it_should_initialize()
    {
        $this->assertEquals($this->prediction->getUserId(), self::USER_ID);
        $this->assertEquals($this->prediction->getMatchId(), self::MATCH_ID);
        $this->assertEquals($this->prediction->getHomeScore(), self::HOME_SCORE);
        $this->assertEquals($this->prediction->getAwayScore(), self::AWAY_SCORE);
        $this->assertEquals($this->prediction->getStake(), self::STAKE);
    }

    public function test_it_should_place_stake_during_initialization()
    {
        $prediction = new Prediction(
            self::USER_ID,
            self::MATCH_ID,
            self::HOME_SCORE,
            self::AWAY_SCORE,
            100
        );

        $this->assertEquals($prediction->getStake(), 100);
        $this->assertTrue(in_array(new PredictionStakeRaised($prediction, 100), $prediction->releaseEvents()));
    }
}