Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56d0267
commit 8591313
Showing
10 changed files
with
199 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Kimai time-tracking app. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Export; | ||
|
||
use App\Entity\Customer; | ||
use App\Repository\Query\TimesheetQuery; | ||
use App\Utils\FileHelper; | ||
|
||
final class ExportFilename | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $filename; | ||
|
||
public function __construct(TimesheetQuery $query) | ||
{ | ||
$filename = date('Ymd'); | ||
$hasName = false; | ||
|
||
$customers = $query->getCustomers(); | ||
if (\count($customers) === 1) { | ||
$filename .= '-' . $this->convert($this->getCustomerName($customers[0])); | ||
$hasName = true; | ||
} | ||
|
||
$projects = $query->getProjects(); | ||
if (\count($projects) === 1) { | ||
if (!$hasName) { | ||
$filename .= '-' . $this->convert($this->getCustomerName($projects[0]->getCustomer())); | ||
} | ||
$filename .= '-' . $this->convert($projects[0]->getName()); | ||
$hasName = true; | ||
} | ||
|
||
$users = $query->getUsers(); | ||
if (\count($users) === 1) { | ||
$filename .= '-' . $this->convert($users[0]->getDisplayName()); | ||
$hasName = true; | ||
} | ||
|
||
if (!$hasName) { | ||
$filename .= '-kimai-export'; | ||
} | ||
|
||
$filename = str_replace(['/', '\\'], '-', $filename); | ||
|
||
$this->filename = $filename; | ||
} | ||
|
||
private function getCustomerName(Customer $customer): string | ||
{ | ||
$company = $customer->getCompany(); | ||
if (empty($company)) { | ||
$company = $customer->getName(); | ||
} | ||
|
||
return $company; | ||
} | ||
|
||
private function convert(string $filename): string | ||
{ | ||
return FileHelper::convertToAsciiFilename($filename); | ||
} | ||
|
||
public function getFilename() | ||
{ | ||
return $this->filename; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->getFilename(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Kimai time-tracking app. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests\Export; | ||
|
||
use App\Entity\Customer; | ||
use App\Entity\Project; | ||
use App\Entity\User; | ||
use App\Export\ExportFilename; | ||
use App\Repository\Query\TimesheetQuery; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \App\Export\ExportFilename | ||
*/ | ||
class ExportFilenameTest extends TestCase | ||
{ | ||
public function testExportFilename() | ||
{ | ||
$datePrefix = date('Ymd'); | ||
|
||
$query = new TimesheetQuery(); | ||
|
||
$sut = new ExportFilename($query); | ||
|
||
self::assertEquals($datePrefix . '-kimai-export', $sut->getFilename()); | ||
self::assertEquals($datePrefix . '-kimai-export', (string) $sut); | ||
|
||
$customer = new Customer(); | ||
$customer->setName('foo'); | ||
$query = new TimesheetQuery(); | ||
$query->addCustomer($customer); | ||
$sut = new ExportFilename($query); | ||
|
||
self::assertEquals($datePrefix . '-foo', $sut->getFilename()); | ||
self::assertEquals($datePrefix . '-foo', (string) $sut); | ||
|
||
$customer->setCompany('barß / laölala # ldksjf 123 MyAwesome GmbH'); | ||
$sut = new ExportFilename($query); | ||
|
||
self::assertEquals($datePrefix . '-barss_laolala_ldksjf_123_MyAwesome_GmbH', $sut->getFilename()); | ||
self::assertEquals($datePrefix . '-barss_laolala_ldksjf_123_MyAwesome_GmbH', (string) $sut); | ||
|
||
$customer->setCompany('까깨꺄꺠꺼께껴꼐꼬꽈sssss'); | ||
$sut = new ExportFilename($query); | ||
self::assertEquals($datePrefix . '-kkakkaekkyakkyaekkeokkekkyeokkyekkokkwasssss', $sut->getFilename()); | ||
|
||
$customer->setCompany('\"#+ß.!$%&/()=?\\n=/*-+´_<>@' . "\n"); | ||
$sut = new ExportFilename($query); | ||
self::assertEquals($datePrefix . '-ss_n_-', $sut->getFilename()); | ||
|
||
$project = new Project(); | ||
$project->setName('Demo ProjecT1'); | ||
$customer->setCompany('\"#+ß.!$%&/()=?\\n=/*-+´_<>@' . "\n"); | ||
$query->addProject($project); | ||
|
||
$sut = new ExportFilename($query); | ||
self::assertEquals($datePrefix . '-ss_n_--Demo_ProjecT1', $sut->getFilename()); | ||
|
||
$user = new User(); | ||
$user->setUsername('thorsten'); | ||
$query->addUser($user); | ||
|
||
$sut = new ExportFilename($query); | ||
self::assertEquals($datePrefix . '-ss_n_--Demo_ProjecT1-thorsten', $sut->getFilename()); | ||
$user->setAlias('Martin Müller-Lüdenscheidt'); | ||
|
||
$sut = new ExportFilename($query); | ||
self::assertEquals($datePrefix . '-ss_n_--Demo_ProjecT1-Martin_Muller-Ludenscheidt', $sut->getFilename()); | ||
|
||
$user = $this->createMock(User::class); | ||
$user->method('getId')->willReturn(1); | ||
$query->addUser($user); | ||
$sut = new ExportFilename($query); | ||
self::assertEquals($datePrefix . '-ss_n_--Demo_ProjecT1', $sut->getFilename()); | ||
|
||
$project = new Project(); | ||
$project->setName('Project2'); | ||
$query->addProject($project); | ||
$sut = new ExportFilename($query); | ||
self::assertEquals($datePrefix . '-ss_n_-', $sut->getFilename()); | ||
|
||
$customer = new Customer(); | ||
$customer->setName('Customer AAAA'); | ||
$query->addCustomer($customer); | ||
|
||
$sut = new ExportFilename($query); | ||
self::assertEquals($datePrefix . '-kimai-export', $sut->getFilename()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters