Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions lib/GaletteMaps/Fixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* This file is part of Galette Maps plugin (https://galette.eu).
* SPDX-FileCopyrightText: Copyright © 2012-2026 The Galette Team
* SPDX-License-Identifier: GPL-3.0-or-later
*/

declare(strict_types=1);

namespace GaletteMaps;

use Galette\Core\Plugins\FixturesContext;
use Galette\Core\Plugins\FixturesProviderInterface;

/**
* Sample positions for fixture members, run by galette:seed-fixtures
*
* No geocoding: towns positions are known, members are spread around them.
*
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
class Fixtures implements FixturesProviderInterface
{
/** @var list<array{0: float, 1: float}> 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);
}
}
}
80 changes: 80 additions & 0 deletions tests/GaletteMaps/tests/units/Fixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* This file is part of Galette Maps plugin (https://galette.eu).
* SPDX-FileCopyrightText: Copyright © 2012-2026 The Galette Team
* SPDX-License-Identifier: GPL-3.0-or-later
*/

declare(strict_types=1);

namespace GaletteMaps\tests\units;

use Galette\Core\Plugins\FixturesContext;
use Galette\Tests\GaletteTestCase;

/**
* Fixtures tests
*
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
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));
}
}
Loading