ulogger-server/.tests/tests/PositionTest.php

187 lines
9.4 KiB
PHP
Raw Normal View History

2017-09-05 09:36:10 +02:00
<?php
require_once(__DIR__ . "/../lib/UloggerDatabaseTestCase.php");
require_once(__DIR__ . "/../../helpers/track.php");
class PositionTest extends UloggerDatabaseTestCase {
2021-04-22 19:47:52 +02:00
public function testAddPosition(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$trackId = $this->addTestTrack($userId);
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
2017-09-05 09:36:10 +02:00
2019-07-12 21:50:21 +02:00
$posId = uPosition::add($userId, $trackId + 1, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImage);
2021-04-22 19:47:52 +02:00
self::assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
self::assertFalse($posId, "Adding position with nonexistant track should fail");
2017-09-05 09:36:10 +02:00
2019-07-12 21:50:21 +02:00
$posId = uPosition::add($userId + 1, $trackId, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImage);
2021-04-22 19:47:52 +02:00
self::assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
self::assertFalse($posId, "Adding position with wrong user should fail");
2017-09-05 09:36:10 +02:00
2019-07-12 21:50:21 +02:00
$posId = uPosition::add($userId, $trackId, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImage);
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$expected = [
"id" => $posId,
"user_id" => $this->testUserId,
"track_id" => $trackId,
"time" => $this->testTimestamp,
"latitude" => $this->testLat,
"longitude" => $this->testLon,
"altitude" => $this->testAltitude,
"speed" => $this->testSpeed,
"bearing" => $this->testBearing,
"accuracy" => $this->testAccuracy,
"provider" => $this->testProvider,
"comment" => $this->testComment,
2019-07-12 21:50:21 +02:00
"image" => $this->testImage
2017-09-05 09:36:10 +02:00
];
$actual = $this->getConnection()->createQueryTable(
"positions",
2019-07-12 21:50:21 +02:00
"SELECT id, user_id, track_id, " . $this->unix_timestamp('time') . " AS time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image FROM positions"
2017-09-05 09:36:10 +02:00
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
2022-03-19 17:48:44 +01:00
$posId = uPosition::add($userId, $trackId, null, $this->testLat, $this->testLon);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with null time stamp should fail");
2022-03-19 17:48:44 +01:00
$posId = uPosition::add($userId, $trackId, $this->testTimestamp, null, $this->testLon);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with null latitude should fail");
2022-03-19 17:48:44 +01:00
$posId = uPosition::add($userId, $trackId, $this->testTimestamp, $this->testLat, null);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with null longitude should fail");
2017-09-05 09:36:10 +02:00
2019-01-24 19:07:41 +01:00
$posId = uPosition::add($userId, $trackId, "", $this->testLat, $this->testLon);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with empty time stamp should fail");
2019-01-24 19:07:41 +01:00
$posId = uPosition::add($userId, $trackId, $this->testTimestamp, "", $this->testLon);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with empty latitude should fail");
2019-01-24 19:07:41 +01:00
$posId = uPosition::add($userId, $trackId, $this->testTimestamp, $this->testLat, "");
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with empty longitude should fail");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testDeleteAll(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$userId2 = $this->addTestUser($this->testUser2);
$trackId = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId);
$trackId2 = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId2);
$trackId3 = $this->addTestTrack($userId2);
$this->addTestPosition($userId2, $trackId3);
2021-04-22 19:47:52 +02:00
self::assertEquals(3, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(3, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
2021-04-22 19:47:52 +02:00
self::assertTrue(uPosition::deleteAll($userId), "Deleting failed");
self::assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testDeleteAllWIthTrackId(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$userId2 = $this->addTestUser($this->testUser2);
$trackId = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId);
$trackId2 = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId2);
$trackId3 = $this->addTestTrack($userId2);
$this->addTestPosition($userId2, $trackId3);
2021-04-22 19:47:52 +02:00
self::assertEquals(3, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(3, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
2021-04-22 19:47:52 +02:00
self::assertTrue(uPosition::deleteAll($userId, $trackId), "Deleting failed");
self::assertEquals(2, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testGetLast(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$userId2 = $this->addTestUser($this->testUser2);
$trackId1 = $this->addTestTrack($userId);
$trackId2 = $this->addTestTrack($userId);
$pos1 = $this->addTestPosition($userId, $trackId1, $this->testTimestamp + 3);
$pos2 = $this->addTestPosition($userId2, $trackId2, $this->testTimestamp + 1);
$pos3 = $this->addTestPosition($userId, $trackId1, $this->testTimestamp);
$pos4 = $this->addTestPosition($userId2, $trackId2, $this->testTimestamp + 2);
2021-04-22 19:47:52 +02:00
self::assertEquals(2, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(4, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$lastPosition = uPosition::getLast();
2021-04-22 19:47:52 +02:00
self::assertEquals($lastPosition->id, $pos1, "Wrong last position");
2017-09-05 09:36:10 +02:00
$lastPosition = uPosition::getLast($this->testUserId2);
2021-04-22 19:47:52 +02:00
self::assertEquals($lastPosition->id, $pos4, "Wrong last position (user)");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testGetLastAllUsers(): void {
$userId = $this->addTestUser();
$userId2 = $this->addTestUser($this->testUser2);
$trackId1 = $this->addTestTrack($userId);
$trackId2 = $this->addTestTrack($userId);
$pos1 = $this->addTestPosition($userId, $trackId1, $this->testTimestamp + 3);
$pos2 = $this->addTestPosition($userId2, $trackId2, $this->testTimestamp + 1);
$pos3 = $this->addTestPosition($userId, $trackId1, $this->testTimestamp);
$pos4 = $this->addTestPosition($userId2, $trackId2, $this->testTimestamp + 2);
2021-04-22 19:47:52 +02:00
self::assertEquals(2, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(4, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$posArr = uPosition::getLastAllUsers();
2021-04-22 19:47:52 +02:00
self::assertCount(2, $posArr, "Wrong row count");
foreach ($posArr as $position) {
/** @var uPosition $position */
switch ($position->id) {
case 1:
2021-04-22 19:47:52 +02:00
self::assertEquals($this->testTimestamp + 3, $position->timestamp);
self::assertEquals($userId, $position->userId);
self::assertEquals($trackId1, $position->trackId);
break;
case 4:
2021-04-22 19:47:52 +02:00
self::assertEquals($this->testTimestamp + 2, $position->timestamp);
self::assertEquals($userId2, $position->userId);
self::assertEquals($trackId2, $position->trackId);
break;
default:
2021-04-22 19:47:52 +02:00
self::assertTrue(false, "Unexpected position: $position->id");
}
}
}
2021-04-22 19:47:52 +02:00
public function testGetAll(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$userId2 = $this->addTestUser($this->testUser2);
$userId3 = $this->addTestUser("testUser3");
$trackId = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId);
$trackId2 = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId2);
$trackId3 = $this->addTestTrack($userId2);
$this->addTestPosition($userId2, $trackId3);
2021-04-22 19:47:52 +02:00
self::assertEquals(3, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(3, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$posArr = uPosition::getAll();
2021-04-22 19:47:52 +02:00
self::assertCount(3, $posArr, "Wrong row count");
2019-01-24 19:07:41 +01:00
$posArr = uPosition::getAll($userId);
2021-04-22 19:47:52 +02:00
self::assertCount(2, $posArr, "Wrong row count");
2019-01-24 19:07:41 +01:00
$posArr = uPosition::getAll($userId, $trackId);
2021-04-22 19:47:52 +02:00
self::assertCount(1, $posArr, "Wrong row count");
2022-03-19 17:48:44 +01:00
$posArr = uPosition::getAll(null, $trackId);
2021-04-22 19:47:52 +02:00
self::assertCount(1, $posArr, "Wrong row count");
2019-01-24 19:07:41 +01:00
$posArr = uPosition::getAll($userId3);
2021-04-22 19:47:52 +02:00
self::assertCount(0, $posArr, "Wrong row count");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testDistanceTo(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$trackId = $this->addTestTrack($userId);
$pos1 = $this->addTestPosition($userId, $trackId, $this->testTimestamp, 0, 0);
$pos2 = $this->addTestPosition($userId, $trackId, $this->testTimestamp, 0, 1);
2017-09-05 09:36:10 +02:00
$posArr = uPosition::getAll();
2021-04-22 19:47:52 +02:00
self::assertCount(2, $posArr, "Wrong row count");
self::assertEquals(111195, round($posArr[0]->distanceTo($posArr[1])), "Wrong distance");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testSecondsTo(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$trackId = $this->addTestTrack($userId);
$pos1 = $this->addTestPosition($userId, $trackId, $this->testTimestamp);
$pos2 = $this->addTestPosition($userId, $trackId, $this->testTimestamp + 1);
2017-09-05 09:36:10 +02:00
$posArr = uPosition::getAll();
2021-04-22 19:47:52 +02:00
self::assertCount(2, $posArr, "Wrong row count");
self::assertEquals(-1, $posArr[0]->secondsTo($posArr[1]), "Wrong time difference");
2017-09-05 09:36:10 +02:00
}
}
?>