Skip to content

Commit bedc99a

Browse files
ISSUE #1530: Improve tests
1 parent b5f1110 commit bedc99a

File tree

20 files changed

+221
-29
lines changed

20 files changed

+221
-29
lines changed

composer.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Application/Import/ImportActivities/ImportActivitiesCommandHandler.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,25 @@ public function handle(Command $command): void
189189
return;
190190
}
191191

192-
if ($command->isFullImport()) {
193-
if (count($activityIdsToDelete) === count($allActivityIds) && array_values($activityIdsToDelete) == $allActivityIds->toArray()) {
194-
throw new \RuntimeException('All activities appear to be marked for deletion. This seems like a configuration issue. Aborting to prevent data loss');
195-
}
192+
if (!$command->isFullImport()) {
193+
return;
194+
}
196195

197-
// Only delete activities during full imports to avoid accidental deletion of data.
198-
foreach ($activityIdsToDelete as $activityId) {
199-
$activity = $this->activityRepository->find($activityId);
200-
$activity->delete();
201-
$this->activityRepository->delete($activity);
196+
if (count($activityIdsToDelete) === count($allActivityIds) && array_values($activityIdsToDelete) == $allActivityIds->toArray()) {
197+
throw new \RuntimeException('All activities appear to be marked for deletion. This seems like a configuration issue. Aborting to prevent data loss');
198+
}
202199

203-
$command->getOutput()->writeln(sprintf(
204-
' => Deleted activity "%s - %s"',
205-
$activity->getName(),
206-
$activity->getStartDate()->format('d-m-Y'))
207-
);
208-
}
200+
// Only delete activities during full imports to avoid accidental deletion of data.
201+
foreach ($activityIdsToDelete as $activityId) {
202+
$activity = $this->activityRepository->find($activityId);
203+
$activity->delete();
204+
$this->activityRepository->delete($activity);
205+
206+
$command->getOutput()->writeln(sprintf(
207+
' => Deleted activity "%s - %s"',
208+
$activity->getName(),
209+
$activity->getStartDate()->format('d-m-Y'))
210+
);
209211
}
210212
}
211213
}

tests/Application/Import/ImportActivities/ImportActivitiesCommandHandlerTest.php

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Application\Import\ImportActivities\Pipeline\ActivityImportPipeline;
1111
use App\Application\Import\ImportActivities\SkipActivitiesRecordedBefore;
1212
use App\Domain\Activity\ActivityId;
13+
use App\Domain\Activity\ActivityIds;
1314
use App\Domain\Activity\ActivityRepository;
1415
use App\Domain\Activity\ActivityVisibility;
1516
use App\Domain\Activity\ActivityWithRawData;
@@ -58,7 +59,17 @@ class ImportActivitiesCommandHandlerTest extends ContainerTestCase
5859
private ImportActivitiesCommandHandler $importActivitiesCommandHandler;
5960
private SpyStrava $strava;
6061

61-
public function testHandleWithTooManyRequests(): void
62+
public function testHandleWithTooManyRequestsWhileInitializing(): void
63+
{
64+
$output = new SpyOutput();
65+
$this->strava->setMaxNumberOfCallsBeforeTriggering429(0);
66+
67+
$this->importActivitiesCommandHandler->handle(new ImportActivities($output, null));
68+
69+
$this->assertMatchesTextSnapshot((string) $output);
70+
}
71+
72+
public function testHandleWithTooManyRequestsWhileFetchingActivities(): void
6273
{
6374
$output = new SpyOutput();
6475
$this->strava->setMaxNumberOfCallsBeforeTriggering429(9);
@@ -93,7 +104,17 @@ public function testHandleWithTooManyRequests(): void
93104
));
94105
}
95106

96-
public function testHandleWithUnexpectedError(): void
107+
public function testHandleWithUnexpectedErrorWhileInitializing(): void
108+
{
109+
$output = new SpyOutput();
110+
$this->strava->setMaxNumberOfCallsBeforeTriggering429(1000);
111+
$this->strava->triggerExceptionOnNextCall();
112+
113+
$this->importActivitiesCommandHandler->handle(new ImportActivities($output, null));
114+
$this->assertMatchesTextSnapshot((string) $output);
115+
}
116+
117+
public function testHandleWithUnexpectedErrorWhileFetchingActivities(): void
97118
{
98119
$output = new SpyOutput();
99120
$this->strava->setMaxNumberOfCallsBeforeTriggering429(1000);
@@ -413,6 +434,58 @@ public function testHandleWithSportTypeIsNotIncluded(): void
413434
$this->assertMatchesTextSnapshot($output);
414435
}
415436

437+
public function testHandlePartialImport(): void
438+
{
439+
$output = new SpyOutput();
440+
$this->strava->setMaxNumberOfCallsBeforeTriggering429(1000);
441+
442+
$this->getContainer()->get(ActivityWithRawDataRepository::class)->add(ActivityWithRawData::fromState(
443+
ActivityBuilder::fromDefaults()
444+
->withActivityId(ActivityId::fromUnprefixed(4))
445+
->build(),
446+
[]
447+
));
448+
449+
$this->getContainer()->get(ActivityWithRawDataRepository::class)->add(ActivityWithRawData::fromState(
450+
ActivityBuilder::fromDefaults()
451+
->withActivityId(ActivityId::fromUnprefixed(1000))
452+
->withStartingCoordinate(Coordinate::createFromLatAndLng(
453+
Latitude::fromString('51.2'),
454+
Longitude::fromString('3.18')
455+
))
456+
->withKudoCount(1)
457+
->withName('Delete this one')
458+
->build(),
459+
[
460+
'kudos_count' => 1,
461+
'name' => 'Delete this one',
462+
]
463+
));
464+
465+
$segmentEffortOne = SegmentEffortBuilder::fromDefaults()
466+
->withActivityId(ActivityId::fromUnprefixed(1000))
467+
->build();
468+
$this->getContainer()->get(SegmentEffortRepository::class)->add($segmentEffortOne);
469+
470+
$stream = ActivityStreamBuilder::fromDefaults()
471+
->withActivityId(ActivityId::fromUnprefixed(1000))
472+
->build();
473+
$this->getContainer()->get(ActivityStreamRepository::class)->add($stream);
474+
475+
$this->getContainer()->get(ActivityWithRawDataRepository::class)->add(ActivityWithRawData::fromState(
476+
ActivityBuilder::fromDefaults()
477+
->withKudoCount(1)
478+
->withName('Delete this one as well')
479+
->withActivityId(ActivityId::fromUnprefixed(1001))
480+
->build(),
481+
[]
482+
));
483+
484+
$this->importActivitiesCommandHandler->handle(new ImportActivities($output, ActivityIds::fromArray([ActivityId::fromUnprefixed(4)])));
485+
486+
$this->assertMatchesTextSnapshot($output);
487+
}
488+
416489
#[\Override]
417490
protected function setUp(): void
418491
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Importing activities...
2+
=> [1/1] Updated activity: "Night Ride3 - 10-10-2023"

tests/Application/Import/ImportActivities/__snapshots__/ImportActivitiesCommandHandlerTest__testHandleWithTooManyRequests__1.txt renamed to tests/Application/Import/ImportActivities/__snapshots__/ImportActivitiesCommandHandlerTest__testHandleWithTooManyRequestsWhileFetchingActivities__1.txt

File renamed without changes.

tests/Application/Import/ImportActivities/__snapshots__/ImportActivitiesCommandHandlerTest__testHandleWithTooManyRequests__2.txt renamed to tests/Application/Import/ImportActivities/__snapshots__/ImportActivitiesCommandHandlerTest__testHandleWithTooManyRequestsWhileFetchingActivities__2.txt

File renamed without changes.

tests/Application/Import/ImportActivities/__snapshots__/ImportActivitiesCommandHandlerTest__testHandleWithTooManyRequests__3.json renamed to tests/Application/Import/ImportActivities/__snapshots__/ImportActivitiesCommandHandlerTest__testHandleWithTooManyRequestsWhileFetchingActivities__3.json

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Importing activities...
2+
<error>You reached the daily Strava API rate limit. You will need to import the rest of your data tomorrow</error>

tests/Application/Import/ImportActivities/__snapshots__/ImportActivitiesCommandHandlerTest__testHandleWithUnexpectedError__1.txt renamed to tests/Application/Import/ImportActivities/__snapshots__/ImportActivitiesCommandHandlerTest__testHandleWithUnexpectedErrorWhileFetchingActivities__1.txt

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Importing activities...
2+
<error>Strava API threw error: The error</error>

0 commit comments

Comments
 (0)