Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a weekly file pattern. #17

Merged
merged 1 commit into from
Aug 10, 2020
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
43 changes: 43 additions & 0 deletions src/Files/WeeklyFilePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Drupal\campaignion_csv\Files;

/**
* File pattern that creates weekly files.
*/
class WeeklyFilePattern extends DateIntervalFilePattern {

/**
* Create a new instance from an info-array.
*
* @param array $info
* The info-array as defined in hook_campaignion_csv_info(). Keys are:
* - path: The path pattern for the file relative to the root. The path
* is expanded using `strftime()`.
* - retention_period: A \DateInterval specifying how long old files should
* be kept (or generated).
* - include_current: Whether the ongoing month should be included.
* - refresh_interval: A \DateInterval that defines how often files for the
* ongoing period are regenerated.
* @param \DateTimeInterface $now
* The time considered to be now. Defaults to the date and time.
*/
public static function fromInfo(array $info, \DateTimeInterface $now = NULL) {
if (!$now) {
$now = new \DateTimeImmutable();
}
$info += [
'include_current' => TRUE,
];
$interval = new \DateInterval('P1W');

$end = $now->modify('+1 day')->modify('last monday')->setTime(0, 0, 0, 0);
$start = $end->sub($info['retention_period']);
if ($info['include_current']) {
$end = $end->add($interval);
}
$period = new \DatePeriod($start, $interval, $end);
return new static($info['path'], $period, $interval, $info);
}

}
76 changes: 76 additions & 0 deletions tests/Files/WeeklyFilePatternTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Drupal\campaignion_csv\Files;

/**
* Test the weekly file pattern.
*/
class WeeklyFilePatternTest extends \DrupalUnitTestCase {

/**
* Test that array keys match the path pattern.
*/
public function testExpandKeysArePaths() {
$pattern = 'a/%Y-%m-%d';
$interval = new \DateInterval('P1W');
$period = new \DatePeriod(new \DateTimeImmutable('2020-07-27'), $interval, new \DateTimeImmutable('2020-08-11'));
$file_pattern = new WeeklyFilePattern($pattern, $period, $interval, []);
$this->assertEqual([
'a/2020-07-27',
'a/2020-08-03',
'a/2020-08-10',
], array_keys($file_pattern->expand('/root')));

}

/**
* Test that expand returns proper FileInfo instances.
*/
public function testExpandReturnsFiles() {
$pattern = 'a/%Y-%m-%d';
$interval = new \DateInterval('P1W');
$period = new \DatePeriod(new \DateTimeImmutable('2020-07-27'), $interval, new \DateTimeImmutable('2020-08-11'));
$file_pattern = new WeeklyFilePattern($pattern, $period, $interval, []);
$files = $file_pattern->expand('/root');
$this->assertCount(3, $files);
$this->assertContainsOnlyInstancesOf(TimeframeFileInfo::class, $files);
}

/**
* Test creating files based on the info array.
*/
public function testFromInfo() {
$info = [
'path' => 'a/%Y-%m-%d',
'retention_period' => new \DateInterval('P3W'),
'include_current' => FALSE,
];
$now = new \DateTimeImmutable('2020-08-20');
$file_pattern = WeeklyFilePattern::fromInfo($info, $now);
$this->assertEqual([
'a/2020-07-27',
'a/2020-08-03',
'a/2020-08-10',
], array_keys($file_pattern->expand('/root')));
}

/**
* Test that current month is included if the option is set.
*/
public function testFromInfoIncludingCurrentMonth() {
$info = [
'path' => 'a/%Y-%m-%d',
'retention_period' => new \DateInterval('P3W'),
'include_current' => TRUE,
];
$now = new \DateTimeImmutable('2020-08-20');
$file_pattern = WeeklyFilePattern::fromInfo($info, $now);
$this->assertEqual([
'a/2020-07-27',
'a/2020-08-03',
'a/2020-08-10',
'a/2020-08-17',
], array_keys($file_pattern->expand('/root')));
}

}