From 707d0f0d608cd2360c9c5b675a7873aebb91c4d7 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Sun, 4 Oct 2026 12:20:03 +0200 Subject: [PATCH] Seed sample positions for fixture members --- lib/GaletteMaps/Fixtures.php | 80 ++++++++++++++++++++++ tests/GaletteMaps/tests/units/Fixtures.php | 80 ++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 lib/GaletteMaps/Fixtures.php create mode 100644 tests/GaletteMaps/tests/units/Fixtures.php diff --git a/lib/GaletteMaps/Fixtures.php b/lib/GaletteMaps/Fixtures.php new file mode 100644 index 0000000..e09765d --- /dev/null +++ b/lib/GaletteMaps/Fixtures.php @@ -0,0 +1,80 @@ + + */ +class Fixtures implements FixturesProviderInterface +{ + /** @var list Towns latitude and longitude */ + private const array TOWNS = [ + [48.856614, 2.352222], //Paris + [45.764043, 4.835659], //Lyon + [43.296482, 5.369780], //Marseille + [50.629250, 3.057256], //Lille + [44.837789, -0.579180], //Bordeaux + [47.218371, -1.553621], //Nantes + [48.573405, 7.752111], //Strasbourg + [43.604652, 1.444209], //Toulouse + [48.117266, -1.677793], //Rennes + [47.322047, 5.041480], //Dijon + [43.710173, 7.261953], //Nice + [45.188529, 5.724524], //Grenoble + ]; + + /** + * Locate two members out of three + * + * @param FixturesContext $context Fixtures context + */ + public function seedFixtures(FixturesContext $context): string + { + $coordinates = new Coordinates($context->zdb, $context->login); + $count = 0; + foreach (array_values($context->members) as $position => $id_adh) { + if ($position % 3 === 2) { + continue; + } + + [$latitude, $longitude] = self::TOWNS[$position % count(self::TOWNS)]; + //spread members of a same town, within a few kilometers + $coordinates->set( + $id_adh, + $latitude + ((($position * 37) % 21) - 10) / 300, + $longitude + ((($position * 53) % 21) - 10) / 300 + ); + ++$count; + } + + return sprintf('Located %d members', $count); + } + + /** + * Remove fixture members positions + * + * @param FixturesContext $context Fixtures context + */ + public function cleanFixtures(FixturesContext $context): void + { + $coordinates = new Coordinates($context->zdb, $context->login); + foreach ($context->members as $id_adh) { + $coordinates->remove($id_adh); + } + } +} diff --git a/tests/GaletteMaps/tests/units/Fixtures.php b/tests/GaletteMaps/tests/units/Fixtures.php new file mode 100644 index 0000000..f076b26 --- /dev/null +++ b/tests/GaletteMaps/tests/units/Fixtures.php @@ -0,0 +1,80 @@ + + */ +class Fixtures extends GaletteTestCase +{ + protected int $seed = 20261004120000; + + /** + * Cleanup after each test method + */ + public function tearDown(): void + { + $delete = $this->zdb->delete(MAPS_PREFIX . \GaletteMaps\Coordinates::TABLE); + $this->zdb->execute($delete); + parent::tearDown(); + } + + /** + * Test seeding and cleaning fixtures + */ + public function testFixtures(): void + { + $member_one = $this->getMemberOne(); + $member_two = $this->getMemberTwo(); + $this->logSuperAdmin(); + + $context = new FixturesContext( + zdb: $this->zdb, + login: $this->login, + preferences: $this->preferences, + history: $this->history, + plugins: $this->plugins, + members: [ + 'one' => $member_one->id, + 'two' => $member_two->id, + //every third member is not located: an unknown one would fail on foreign key + 'unknown' => 999999, + ] + ); + + $fixtures = new \GaletteMaps\Fixtures(); + $this->assertSame('Located 2 members', $fixtures->seedFixtures($context)); + + $coords = new \GaletteMaps\Coordinates($this->zdb, $this->login); + $one = $coords->get($member_one->id); + $this->assertNotNull($one); + //around Paris + $this->assertEqualsWithDelta(48.856614, (float)$one['latitude'], 0.05); + $this->assertEqualsWithDelta(2.352222, (float)$one['longitude'], 0.05); + $two = $coords->get($member_two->id); + $this->assertNotNull($two); + //around Lyon + $this->assertEqualsWithDelta(45.764043, (float)$two['latitude'], 0.05); + $this->assertEqualsWithDelta(4.835659, (float)$two['longitude'], 0.05); + + //seeding again does not fail + $this->assertSame('Located 2 members', $fixtures->seedFixtures($context)); + + $fixtures->cleanFixtures($context); + $this->assertNull($coords->get($member_one->id)); + $this->assertNull($coords->get($member_two->id)); + } +}