subject = new AdjustmentMap(); } /** * @dataProvider increaseParameterValidationDataProvider * @param $sourceId * @param $action * @param $seconds */ public function testIncrease_WhenParametersAreNotValid_ThenMapIsNotUpdated($sourceId, $seconds): void { $this->expectException(InvalidArgumentException::class); $this->subject->increase($sourceId, self::DUMMY_ADJUSTMENT_TYPE, $seconds); } public function testIncrease_WhenValueIsRegisteredForNewSource_ThenEntryIsCorrectlyInitialized(): void { $this->subject->increase('testSourceId', self::DUMMY_ADJUSTMENT_TYPE, 10); $this->assertEquals( 10, $this->subject->toArray()['testSourceId'][self::DUMMY_ADJUSTMENT_TYPE][AdjustmentMap::ACTION_INCREASE] ); $this->assertEquals( 0, $this->subject->toArray()['testSourceId'][self::DUMMY_ADJUSTMENT_TYPE][AdjustmentMap::ACTION_DECREASE] ); } public function testIncrease_WhenValueExistsInTheMap_ThenSubsequentValuesAreAdded(): void { $this->subject->increase('testSourceId', self::DUMMY_ADJUSTMENT_TYPE, 10); $this->subject->increase('testSourceId', self::DUMMY_ADJUSTMENT_TYPE, 15); $this->assertEquals( 25, $this->subject->toArray()['testSourceId'][self::DUMMY_ADJUSTMENT_TYPE][AdjustmentMap::ACTION_INCREASE] ); } public function testGet_WhenThereIsNoValuePresent_ThenZeroIsReturned(): void { $this->assertEquals(0, $this->subject->get('newSourceId')); } public function testGet_WhenRequested_ThenReturnValueIsCalculatedFromIncreasesAndDecreasesOfAllTypes(): void { $this->subject->increase('testSourceId', self::DUMMY_ADJUSTMENT_TYPE, 10); $this->assertEquals(10, $this->subject->get('testSourceId')); $this->subject->decrease('testSourceId', self::DUMMY_ADJUSTMENT_TYPE, 5); $this->assertEquals(5, $this->subject->get('testSourceId')); $this->subject->decrease('testSourceId', self::DUMMY_ADJUSTMENT_TYPE, 10); $this->assertEquals(-5, $this->subject->get('testSourceId')); } public function testGetByType_WhenAdjustmentForTypeExist_ThenReturnsValuesCalculated(): void { $newAdjustmentType = 'NEW_TYPE'; $this->subject->increase('testSourceId', self::DUMMY_ADJUSTMENT_TYPE, 10); $this->assertEquals( 10, $this->subject->getByType('testSourceId', self::DUMMY_ADJUSTMENT_TYPE) ); $this->subject->increase('testSourceId', $newAdjustmentType, 10); $this->assertEquals( 10, $this->subject->getByType('testSourceId', self::DUMMY_ADJUSTMENT_TYPE) ); $this->subject->decrease('testSourceId', self::DUMMY_ADJUSTMENT_TYPE, 5); $this->assertEquals( 5, $this->subject->getByType('testSourceId', self::DUMMY_ADJUSTMENT_TYPE) ); $this->subject->decrease('testSourceId', $newAdjustmentType, 5); $this->assertEquals( 5, $this->subject->getByType('testSourceId', self::DUMMY_ADJUSTMENT_TYPE) ); } public function testGetByType_WhenAdjustmentForTypeDoesntExist_ThenReturnsZero(): void { $this->assertEquals( 0, $this->subject->getByType('newSourceId', self::DUMMY_ADJUSTMENT_TYPE) ); } public function testToArray_WhenSerializedToAndFromArray_ThenValuesAreStillTheSame(): void { $this->subject->increase('testSourceId1', self::DUMMY_ADJUSTMENT_TYPE, 10); $this->subject->increase('testSourceId2', self::DUMMY_ADJUSTMENT_TYPE, 20); $data = $this->subject->toArray(); $secondMap = new AdjustmentMap(); $secondMap->fromArray($data); $this->assertEquals($this->subject->toArray(), $secondMap->toArray()); } public function testFromArray_WhenSerializedToAndFromArray_ThenValuesAreStillTheSame(): void { $this->subject->increase('testSourceId1', self::DUMMY_ADJUSTMENT_TYPE, 10); $this->subject->increase('testSourceId2', self::DUMMY_ADJUSTMENT_TYPE, 20); $data = $this->subject->toArray(); $secondMap = new AdjustmentMap(); $secondMap->fromArray($data); $this->assertEquals($secondMap->toArray(), $this->subject->toArray()); } public function testJsonSerialize_WhenEntriesAreEncodedToAndDecodedFromJson_ThenValuesAreTheSame(): void { $this->subject->increase('testSourceId1', self::DUMMY_ADJUSTMENT_TYPE, 10); $this->subject->increase('testSourceId2', self::DUMMY_ADJUSTMENT_TYPE, 20); $encoded = json_encode($this->subject); $data = $this->subject->toArray(); $this->assertEquals($data, json_decode($encoded, true)); } public function increaseParameterValidationDataProvider(): array { return [ ['', 0], ['testSourceId', 0], ]; } }