/** * {@inheritdoc} */ public function fromString($string) : Workout { $simpleXML = new \SimpleXMLElement($string); $workout = new Workout(); if (isset($simpleXML->metadata->author->name)) { $workout->setAuthor(new Author($simpleXML->metadata->author->name)); } foreach ($simpleXML->trk as $simpleXMLTrack) { // Sport. $sport = SportMapperInterface::class; if (isset($simpleXMLTrack->type)) { $sport = SportGuesser::sportFromCode((string) $simpleXMLTrack->type); } $track = new Track(array(), $sport); // Track points. foreach ($simpleXMLTrack->trkseg->trkpt as $point) { $attributes = $point->attributes(); $dateTime = new \DateTime((string) $point->time); $trackPoint = new TrackPoint((double) $attributes['lat'], (double) $attributes['lon'], $dateTime); $trackPoint->setElevation((int) $point->ele); if (isset($point->extensions)) { $trackPoint->setExtensions($this->parseExtensions($point->extensions)); } $track->addTrackPoint($trackPoint); } $workout->addTrack($track); } return $workout; }
/** * Test setting/getting the author. */ public function testSetGetAuthor() { $workout = new Workout(); self::assertNull($workout->author()); $author = new Author('author'); $workout->setAuthor($author); self::assertSame($author, $workout->author()); }
/** * Test dumping a workout to a GPX string. */ public function testToStringMultiTrack() { $workout = new Workout(); $workout->addTrack(new Track(array($this->getTrackPoint(53.551075, 9.993672, '2014-05-30T17:12:58+00:00', 11, 78), $this->getTrackPoint(53.550085, 9.992682, '2014-05-30T17:12:59+00:00', 10, 88)), SportMapperInterface::RUNNING)); $workout->addTrack(new Track(array($this->getTrackPoint(53.549075, 9.991671999999999, '2014-05-30T17:13:00+00:00', 9, 98), $this->getTrackPoint(53.548085, 9.990682, '2014-05-30T17:13:01+00:00', 8, 108)), SportMapperInterface::SWIMMING)); $workout->setAuthor(new Author('John Doe')); $filesystemMock = $this->createMock(FilesystemInterface::class); $gpx = new GPX($filesystemMock); $actual = $gpx->toString($workout); self::assertXmlStringEqualsXmlFile(__DIR__ . '/Expected/' . $this->getName() . '.gpx', $actual); }
/** * Test loading a workout from a GPX string with multiple tracks. */ public function testFromStringMultiTrack() { $expected = new Workout(); $expected->addTrack(new Track(array($this->getTrackPoint(53.551075, 9.993672, '2014-05-30T17:12:58+00:00', 11, 78), $this->getTrackPoint(53.550085, 9.992682, '2014-05-30T17:12:59+00:00', 10, 88)), SportMapperInterface::RUNNING)); $expected->addTrack(new Track(array($this->getTrackPoint(53.549075, 9.991671999999999, '2014-05-30T17:13:00+00:00', 9, 98), $this->getTrackPoint(53.548085, 9.990682, '2014-05-30T17:13:01+00:00', 8, 108)), SportMapperInterface::SWIMMING)); $expected->setAuthor(new Author('John Doe')); $fileSystemMock = $this->createMock(FilesystemInterface::class); $gpx = new GPX($fileSystemMock); $actual = $gpx->fromString(file_get_contents(__DIR__ . '/Fixtures/testFromStringMultiTrack.gpx')); self::assertEquals($expected, $actual); }