Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from zynga/addingFileTypeBox
Browse files Browse the repository at this point in the history
Added FileType box
  • Loading branch information
jarredwsimmer authored Apr 11, 2018
2 parents c4c8b1a + 57f93f7 commit 0262ae7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Zynga/Framework/Type/V1/FileType/Box.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?hh // strict

namespace Zynga\Framework\Type\V1\FileType;

use Zynga\Framework\Type\V1\StringBox;
use Zynga\Framework\Type\V1\FileType\BoxFactory as FileTypeBoxFactory;
use Zynga\Framework\Type\V1\FileType\Exception\UnknownFileTypeException;

/**
* Representation of a file type
*/
class Box extends StringBox {

<<__Override>>
protected function importFromString(string $value): bool {
if (FileTypeBoxFactory::isValidValue($value)) {
return parent::importFromString($value);
}

throw new UnknownFileTypeException('value='.$value);
}

}
29 changes: 29 additions & 0 deletions src/Zynga/Framework/Type/V1/FileType/BoxFactory.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?hh // strict

namespace Zynga\Framework\Type\V1\FileType;

use Zynga\Framework\Type\V1\FileType\Box as FileTypeBox;
use Zynga\Framework\Type\V1\FileType\Enum\FileType as FileTypeEnum;

/**
* Generates and validates file types
*/
class BoxFactory {

public static function json(): FileTypeBox {
$value = new FileTypeBox();
$value->set(FileTypeEnum::JSON);
return $value;
}

public static function csv(): FileTypeBox {
$value = new FileTypeBox();
$value->set(FileTypeEnum::CSV);
return $value;
}

public static function isValidValue(string $value): bool {
return FileTypeEnum::coerce($value) !== null;
}

}
28 changes: 28 additions & 0 deletions src/Zynga/Framework/Type/V1/FileType/BoxFactoryTest.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?hh // strict

namespace Zynga\Framework\Type\V1\FileType;

use Zynga\Framework\Testing\TestCase\V2\Base as ZyngaTestCase;
use Zynga\Framework\Type\V1\FileType\BoxFactory;
use Zynga\Framework\Type\V1\FileType\Enum\FileType as FileTypeEnum;

class BoxFactoryTest extends ZyngaTestCase {

public function testJsonReturnsValidType(): void {
$this->assertEquals((string)FileTypeEnum::JSON, BoxFactory::json()->get());
}

public function testCsvReturnsValidType(): void {
$this->assertEquals((string)FileTypeEnum::CSV, BoxFactory::csv()->get());
}

public function testIsValidValueReturnsFalseCorrectly(): void {
$this->assertFalse(BoxFactory::isValidValue('pumpernickel'));
}

public function testIsValidValueReturnsTrueCorrectly(): void {
$this->assertTrue(BoxFactory::isValidValue((string)FileTypeEnum::JSON));
$this->assertTrue(BoxFactory::isValidValue((string)FileTypeEnum::CSV));
}

}
18 changes: 18 additions & 0 deletions src/Zynga/Framework/Type/V1/FileType/BoxTest.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?hh // strict

namespace Zynga\Framework\Type\V1\FileType;

use Zynga\Framework\Testing\TestCase\V2\Base as ZyngaTestCase;
use Zynga\Framework\Type\V1\FileType\BoxFactory;
use Zynga\Framework\Type\V1\FileType\Box as FileTypeBox;
use Zynga\Framework\Type\V1\FileType\Exception\UnknownFileTypeException;

class BoxTest extends ZyngaTestCase {

public function testInvalidSetThrowsUnknownFileTypeException(): void {
$fileType = new FileTypeBox();
$this->expectException(UnknownFileTypeException::class);
$fileType->set('pumpernickel');
}

}
10 changes: 10 additions & 0 deletions src/Zynga/Framework/Type/V1/FileType/Enum/FileType.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?hh // strict

namespace Zynga\Framework\Type\V1\FileType\Enum;

enum FileType : string as string {
// @codeCoverageIgnoreStart
JSON = 'json';
CSV = 'csv';
// @codeCoverageIgnoreEnd
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?hh // strict

namespace Zynga\Framework\Type\V1\FileType\Exception;

use Zynga\Framework\Exception\V1\Exception as BaseException;

/**
* Representation that an unknown file type was attempted to be used
*/
class UnknownFileTypeException extends BaseException {}

0 comments on commit 0262ae7

Please sign in to comment.