generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from ergebnis/feature/file
Enhancement: Add File
- Loading branch information
Showing
7 changed files
with
230 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License; | ||
|
||
final class File | ||
{ | ||
private $name; | ||
|
||
private $template; | ||
|
||
private $period; | ||
|
||
private $holder; | ||
|
||
private function __construct(string $name, Template $template, Period $period, Holder $holder) | ||
{ | ||
$this->name = $name; | ||
$this->period = $period; | ||
$this->holder = $holder; | ||
$this->template = $template; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param Template $template | ||
* @param Period $period | ||
* @param Holder $holder | ||
* | ||
* @throws Exception\InvalidFile | ||
* | ||
* @return self | ||
*/ | ||
public static function create(string $name, Template $template, Period $period, Holder $holder): self | ||
{ | ||
if ('' === \trim($name)) { | ||
throw Exception\InvalidFile::emptyFileName(); | ||
} | ||
|
||
return new self( | ||
$name, | ||
$template, | ||
$period, | ||
$holder | ||
); | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function save(): void | ||
{ | ||
\file_put_contents($this->name, $this->template->toString([ | ||
'<holder>' => $this->holder->toString(), | ||
'<range>' => $this->period->toString(), | ||
])); | ||
} | ||
} |
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,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/license | ||
*/ | ||
|
||
namespace Ergebnis\License\Test\Unit; | ||
|
||
use Ergebnis\License\Exception; | ||
use Ergebnis\License\File; | ||
use Ergebnis\License\Holder; | ||
use Ergebnis\License\Range; | ||
use Ergebnis\License\Template; | ||
use Ergebnis\License\Year; | ||
use Ergebnis\Test\Util\Helper; | ||
use PHPUnit\Framework; | ||
use Symfony\Component\Filesystem; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\License\File | ||
* | ||
* @uses \Ergebnis\License\Exception\InvalidFile | ||
* @uses \Ergebnis\License\Holder | ||
* @uses \Ergebnis\License\Range | ||
* @uses \Ergebnis\License\Template | ||
* @uses \Ergebnis\License\Year | ||
*/ | ||
final class FileTest extends Framework\TestCase | ||
{ | ||
use Helper; | ||
|
||
protected function setUp(): void | ||
{ | ||
$filesystem = new Filesystem\Filesystem(); | ||
|
||
$filesystem->mkdir(self::workspaceDirectory()); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$filesystem = new Filesystem\Filesystem(); | ||
|
||
$filesystem->remove(self::workspaceDirectory()); | ||
} | ||
|
||
/** | ||
* @dataProvider \Ergebnis\License\Test\Util\DataProvider\Text::blankOrEmptyString() | ||
* | ||
* @param string $name | ||
*/ | ||
public function testCreateRejectsBlankOrEmptyName(string $name): void | ||
{ | ||
$faker = self::faker(); | ||
|
||
$template = Template::fromFile(__DIR__ . '/../../resource/license/MIT.txt'); | ||
$range = Range::since( | ||
Year::fromString($faker->year), | ||
new \DateTimeZone($faker->timezone) | ||
); | ||
$holder = Holder::fromString($faker->name); | ||
|
||
$this->expectException(Exception\InvalidFile::class); | ||
|
||
File::create( | ||
$name, | ||
$template, | ||
$range, | ||
$holder | ||
); | ||
} | ||
|
||
public function testNameReturnsName(): void | ||
{ | ||
$faker = self::faker(); | ||
|
||
$name = \sprintf( | ||
'%s/%s.txt', | ||
__DIR__, | ||
$faker->slug | ||
); | ||
$template = Template::fromFile(__DIR__ . '/../../resource/license/MIT.txt'); | ||
$range = Range::since( | ||
Year::fromString($faker->year), | ||
new \DateTimeZone($faker->timezone) | ||
); | ||
$holder = Holder::fromString($faker->name); | ||
|
||
$file = File::create( | ||
$name, | ||
$template, | ||
$range, | ||
$holder | ||
); | ||
|
||
self::assertSame($name, $file->name()); | ||
} | ||
|
||
public function testSaveSavesFile(): void | ||
{ | ||
$faker = self::faker(); | ||
|
||
$name = \sprintf( | ||
'%s/%s.txt', | ||
self::workspaceDirectory(), | ||
$faker->slug | ||
); | ||
$template = Template::fromFile(__DIR__ . '/../../resource/license/MIT.txt'); | ||
$range = Range::since( | ||
Year::fromString($faker->year), | ||
new \DateTimeZone($faker->timezone) | ||
); | ||
$holder = Holder::fromString($faker->name); | ||
|
||
$file = File::create( | ||
$name, | ||
$template, | ||
$range, | ||
$holder | ||
); | ||
|
||
$file->save(); | ||
|
||
self::assertFileExists($file->name()); | ||
|
||
$expected = $template->toString([ | ||
'<holder>' => $holder->toString(), | ||
'<range>' => $range->toString(), | ||
]); | ||
|
||
self::assertSame($expected, \file_get_contents($file->name())); | ||
} | ||
|
||
private static function workspaceDirectory(): string | ||
{ | ||
return __DIR__ . '/../../.build/test'; | ||
} | ||
} |