*/ class QtiTimeLineTest extends GenerisPhpUnitTestRunner { public function setUp(): void { \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiTest'); } /** * Test the TimeLine instantiation */ public function testConstructor() { $timeLine = new QtiTimeLine([ new TimePoint(['test-a', 'item-a'], 1459519500.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_CLIENT), ]); $this->assertInstanceOf(TimeLine::class, $timeLine); } /** * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testToArray($points) { $timeLine = new QtiTimeLine($points); $timeLineUnserialized = new QtiTimeLine(); $data = $timeLine->toArray(); $timeLineUnserialized->fromArray($data); $this->assertEquals($timeLine->getPoints(), $timeLineUnserialized->getPoints()); } /** * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testFromArray($points) { $timeLine = new QtiTimeLine($points); $timeLineUnserialized = new QtiTimeLine(); $data = $timeLine->toArray(); $timeLineUnserialized->fromArray($data); $this->assertEquals($timeLineUnserialized->getPoints(), $timeLine->getPoints()); } /** * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testJson($points) { $timeLine = new QtiTimeLine($points); $json = json_encode($timeLine); $data = $timeLine->toArray(); $this->assertEquals($data, json_decode($json, true)); } /** * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testSerialize($points) { $timeLine = new QtiTimeLine($points); $timeLineUnserialized = new QtiTimeLine(); $data = $timeLine->serialize(); $timeLineUnserialized->unserialize($data); $this->assertEquals($timeLine->getPoints(), $timeLineUnserialized->getPoints()); } /** * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testUnserialize($points) { $timeLine = new QtiTimeLine($points); $timeLineUnserialized = new QtiTimeLine(); $data = $timeLine->serialize(); $timeLineUnserialized->unserialize($data); $this->assertEquals($timeLineUnserialized->getPoints(), $timeLine->getPoints()); } public function testUnserializeInvalidDataException() { $this->expectException(InvalidDataException::class); $timeLine = new QtiTimeLine(); $data = serialize('string'); $timeLine->unserialize($data); } /** * Test the QtiTimeLine::getPoints() * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testGetPoints($points) { $timeLine = new QtiTimeLine(); foreach ($points as $point) { $timeLine->add($point); } $this->assertEquals($points, $timeLine->getPoints()); } /** * Test the QtiTimeLine::getPoints() */ public function testAdd() { $timeLine = new QtiTimeLine(); $this->assertEquals($timeLine->getPoints(), [], 'The timeline is empty added'); $point = new TimePoint(['test-A', 'item-A'], 1459519570.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER); $result = $timeLine->add($point); $this->assertEquals($timeLine, $result); $this->assertEquals($timeLine->getPoints(), [$point]); $point2 = new TimePoint(['test-A', 'item-B'], 1459519770.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER); $timeLine->add($point2); $this->assertEquals($timeLine->getPoints(), [$point, $point2]); } /** * Test the QtiTimeLine::remove() * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testRemove($points) { //remove all points $timeLine = new QtiTimeLine($points); $this->assertEquals($points, $timeLine->getPoints()); $removed = $timeLine->remove([], TimePoint::TARGET_ALL); $this->assertEquals([], $timeLine->getPoints()); $this->assertEquals(count($points), $removed); //remove all points $timeLine = new QtiTimeLine($points); $removed = $timeLine->remove([], TimePoint::TARGET_ALL, TimePoint::TYPE_ALL); $this->assertEquals([], $timeLine->getPoints()); $this->assertEquals(count($points), $removed); //remove all points $timeLine = new QtiTimeLine($points); $removed = $timeLine->remove([]); $this->assertEquals([], $timeLine->getPoints()); $this->assertEquals(count($points), $removed); //remove second points $pointToRemove = $points[1]; $timeLine = new QtiTimeLine($points); $removed = $timeLine->remove($pointToRemove->getTags(), $pointToRemove->getTarget(), $pointToRemove->getType()); $expected = $points; unset($expected[1]); $this->assertEquals(1, $removed); $this->assertEquals($expected, $timeLine->getPoints()); //remove first four points $timeLine = new QtiTimeLine($points); $removed = $timeLine->remove($points[0]->getTags(), TimePoint::TARGET_ALL, TimePoint::TYPE_ALL); $expected = $points; unset($expected[0], $expected[1], $expected[2], $expected[3]); $this->assertEquals(4, $removed); $this->assertEquals($expected, $timeLine->getPoints()); } /** * Test the QtiTimeLine::clear() * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testClear($points) { $timeLine = new QtiTimeLine($points); $this->assertEquals($points, $timeLine->getPoints()); $result = $timeLine->clear(); $this->assertEquals($timeLine, $result); $this->assertEquals([], $timeLine->getPoints()); } /** * Test the QtiTimeLine::filter() * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testFilter($points) { $timeLine = new QtiTimeLine($points); $filteredTimeLine = $timeLine->filter(null, TimePoint::TARGET_CLIENT); foreach ($filteredTimeLine->getPoints() as $point) { $this->assertEquals(TimePoint::TARGET_CLIENT, $point->getTarget()); } $timeLine = new QtiTimeLine($points); $filteredTimeLine = $timeLine->filter(null, TimePoint::TYPE_ALL, TimePoint::TYPE_START); foreach ($filteredTimeLine->getPoints() as $point) { $this->assertEquals(TimePoint::TYPE_START, $point->getType()); } $timeLine = new QtiTimeLine($points); $filteredTimeLine = $timeLine->filter(null, TimePoint::TARGET_CLIENT, TimePoint::TYPE_START); foreach ($filteredTimeLine->getPoints() as $point) { $this->assertEquals(TimePoint::TARGET_CLIENT, $point->getTarget()); $this->assertEquals(TimePoint::TYPE_START, $point->getType()); } $timeLine = new QtiTimeLine($points); $filteredTimeLine = $timeLine->filter(['test-a', 'item-a']); foreach ($filteredTimeLine->getPoints() as $point) { $this->assertEquals(['test-a', 'item-a'], $point->getTags()); } } /** * Test the QtiTimeLine::find() * @dataProvider linearTestPointsProvider * @param TimePoint[] $points */ public function testFind($points) { $timeLine = new QtiTimeLine($points); $foundPoints = $timeLine->find(null, TimePoint::TARGET_CLIENT); foreach ($foundPoints as $point) { $this->assertEquals(TimePoint::TARGET_CLIENT, $point->getTarget()); } $foundPoints = $timeLine->find(null, TimePoint::TARGET_ALL, TimePoint::TYPE_START); foreach ($foundPoints as $point) { $this->assertEquals(TimePoint::TYPE_START, $point->getType()); } $foundPoints = $timeLine->find(null, TimePoint::TARGET_SERVER, TimePoint::TYPE_START); foreach ($foundPoints as $point) { $this->assertEquals(TimePoint::TARGET_SERVER, $point->getTarget()); $this->assertEquals(TimePoint::TYPE_START, $point->getType()); } $foundPoints = $timeLine->find(['test-a', 'item-a']); foreach ($foundPoints as $point) { $this->assertEquals(['test-a', 'item-a'], $point->getTags()); } } /** * Test the QtiTimeLine::compute() */ public function testCompute() { $points = [ new TimePoint(['test-a', 'item-a'], 1459519500.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-a'], 1459519510.2422, TimePoint::TYPE_END, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-a'], 1459519512.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-b'], 1459519520.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-b'], 1459519522.2422, TimePoint::TYPE_START, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-b'], 1459519530.2422, TimePoint::TYPE_END, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-b'], 1459519532.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ]; $timeLine = new QtiTimeLine($points); $duration = $timeLine->compute(['test-a', 'item-a'], TimePoint::TARGET_SERVER); $expectedDuration = $points[3]->getTimestamp() - $points[0]->getTimestamp(); $this->assertEquals($duration, $expectedDuration); $duration = $timeLine->compute(['test-a', 'item-a'], TimePoint::TARGET_CLIENT); $expectedDuration = $points[2]->getTimestamp() - $points[1]->getTimestamp(); $this->assertEquals($duration, $expectedDuration); $timeLine = new QtiTimeLine([$points[1], $points[2]]); $duration = $timeLine->compute(null, TimePoint::TARGET_ALL); $expectedDuration = $points[2]->getTimestamp() - $points[1]->getTimestamp(); $this->assertEquals($duration, $expectedDuration); } /** * Test the QtiTimeLine::compute() */ public function testComputeOpenRange() { $points = [ new TimePoint(['test-a', 'item-a'], 1459519500.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519501.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-b'], 1459519520.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-b'], 1459519532.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ]; $timeLine = new QtiTimeLine($points); $duration = $timeLine->compute(null, TimePoint::TARGET_SERVER); $expectedDuration = $points[3]->getTimestamp() - $points[0]->getTimestamp(); $this->assertEquals($expectedDuration, $duration); } /** * Test the QtiTimeLine::compute() * @dataProvider malformedRangeExceptionProvider * @param TimePoint[] $points * @param int $expectedDuration */ public function testComputeMalformedRangeException($points, $expectedDuration) { $timeLine = new QtiTimeLine($points); $duration = $timeLine->compute(['test-a', 'item-a']); $this->assertEquals($duration, $expectedDuration); } /** * Test the QtiTimeLine::compute() */ public function testComputeIncompleteRangeException() { $timeLine = new QtiTimeLine([ new TimePoint(['test-a', 'item-a'], 1459519500.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-a'], 1459519510.2422, TimePoint::TYPE_END, TimePoint::TARGET_CLIENT), ]); $expectedDuration = 19; $duration = $timeLine->compute(['test-a', 'item-a'], TimePoint::TARGET_ALL, 1459519511.2422); $this->assertEquals($duration, $expectedDuration); $expectedDuration = 11; $duration = $timeLine->compute(['test-a', 'item-a'], TimePoint::TARGET_SERVER, 1459519511.2422); $this->assertEquals($duration, $expectedDuration); $expectedDuration = 8; $duration = $timeLine->compute(['test-a', 'item-a'], TimePoint::TARGET_CLIENT, 1459519511.2422); $this->assertEquals($duration, $expectedDuration); } /** * Test the QtiTimeLine::compute() * @dataProvider inconsistentRangeExceptionProvider * @param TimePoint[] $points */ public function testInconsistentRangeException($points) { $this->expectException(InconsistentRangeException::class); $timeLine = new QtiTimeLine($points); $timeLine->compute(['test-a', 'item-a']); } /** * @return array */ public function linearTestPointsProvider() { return [ [ 'linearTest' => [ //item-a new TimePoint(['test-a', 'item-a'], 1459519500.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-a'], 1459519510.2422, TimePoint::TYPE_END, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-a'], 1459519512.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), //item-b new TimePoint(['test-a', 'item-b'], 1459519600.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-b'], 1459519602.2422, TimePoint::TYPE_START, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-b'], 1459519610.2422, TimePoint::TYPE_END, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-b'], 1459519612.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), //item-b new TimePoint(['test-a', 'item-c'], 1459519700.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-c'], 1459519702.2422, TimePoint::TYPE_START, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-c'], 1459519710.2422, TimePoint::TYPE_END, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-c'], 1459519712.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ], ], ]; } /** * @return array */ public function malformedRangeExceptionProvider() { return [ [ [ new TimePoint(['test-a', 'item-a'], 1459519500.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ], 2 ], [ [ new TimePoint(['test-a', 'item-a'], 1459519500.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519500.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ], 2 ], [ [ new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519512.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519520.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519530.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ], 28 ], [ [ new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519520.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519512.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519530.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ], 20 ], [ [ new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519512.2422, TimePoint::TYPE_START, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519530.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), new TimePoint(['test-a', 'item-a'], 1459519520.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ], 28 ], ]; } /** * @return array */ public function inconsistentRangeExceptionProvider() { return [ [ [ new TimePoint(['test-a', 'item-a'], 1459519502.2422, TimePoint::TYPE_START, TimePoint::TARGET_CLIENT), new TimePoint(['test-a', 'item-a'], 1459519510.2422, TimePoint::TYPE_END, TimePoint::TARGET_SERVER), ] ], ]; } }