|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Netgen\Bundle\EnhancedBinaryFileBundle\Tests\Core\FieldType\Persistence\Legacy\Content\FieldValue\Converter; |
| 4 | + |
| 5 | +use eZ\Publish\Core\MVC\ConfigResolverInterface; |
| 6 | +use eZ\Publish\SPI\Persistence\Content\FieldValue; |
| 7 | +use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue; |
| 8 | +use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition; |
| 9 | +use eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints; |
| 10 | +use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition; |
| 11 | +use Netgen\Bundle\EnhancedBinaryFileBundle\Core\Persistence\Legacy\Content\FieldValue\Converter\Converter; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class ConverterTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var Converter |
| 18 | + */ |
| 19 | + protected $converter; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + protected $configResolver; |
| 25 | + |
| 26 | + public function setUp() |
| 27 | + { |
| 28 | + $this->configResolver = $this->createMock(ConfigResolverInterface::class); |
| 29 | + $this->converter = new Converter($this->configResolver); |
| 30 | + } |
| 31 | + |
| 32 | + public function testInstanceOfConverterInterface() |
| 33 | + { |
| 34 | + $this->assertInstanceOf(\eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter::class, $this->converter); |
| 35 | + } |
| 36 | + |
| 37 | + public function testToStorageValueShouldDoNothing() |
| 38 | + { |
| 39 | + $this->converter->toStorageValue( |
| 40 | + $this->createMock(FieldValue::class), |
| 41 | + $this->createMock(StorageFieldValue::class) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + public function testToFieldValueShouldDoNothing() |
| 46 | + { |
| 47 | + $this->converter->toFieldValue( |
| 48 | + $this->createMock(StorageFieldValue::class), |
| 49 | + $this->createMock(FieldValue::class) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function testGetIndexColumnShouldReturnFalse() |
| 54 | + { |
| 55 | + $this->assertFalse($this->converter->getIndexColumn()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testToStorageFieldDefinitionWithoutConstraints() |
| 59 | + { |
| 60 | + $fieldDefinition = new FieldDefinition(); |
| 61 | + $storage = new StorageFieldDefinition(); |
| 62 | + $this->converter->toStorageFieldDefinition($fieldDefinition, $storage); |
| 63 | + |
| 64 | + $this->assertEquals(0, $storage->dataInt1); |
| 65 | + $this->assertEquals('', $storage->dataText1); |
| 66 | + } |
| 67 | + |
| 68 | + public function testToStorageFieldDefinition() |
| 69 | + { |
| 70 | + $fieldDefinition = new FieldDefinition(); |
| 71 | + $fieldDefinition->fieldTypeConstraints->validators = [ |
| 72 | + 'FileSizeValidator' => [ |
| 73 | + 'maxFileSize' => 14, |
| 74 | + ], |
| 75 | + ]; |
| 76 | + $fieldDefinition->fieldTypeConstraints->fieldSettings = [ |
| 77 | + 'allowedTypes' => [ |
| 78 | + 'text/plain' |
| 79 | + ], |
| 80 | + ]; |
| 81 | + $storage = new StorageFieldDefinition(); |
| 82 | + $this->converter->toStorageFieldDefinition($fieldDefinition, $storage); |
| 83 | + |
| 84 | + $this->assertEquals(14, $storage->dataInt1); |
| 85 | + $this->assertEquals(['text/plain'], $storage->dataText1); |
| 86 | + } |
| 87 | + |
| 88 | + public function testToFieldDefinition() |
| 89 | + { |
| 90 | + $fieldDefinition = new FieldDefinition(); |
| 91 | + $storage = new StorageFieldDefinition(); |
| 92 | + |
| 93 | + $this->converter->toFieldDefinition($storage, $fieldDefinition); |
| 94 | + |
| 95 | + $this->assertInstanceOf(FieldTypeConstraints::class, $fieldDefinition->fieldTypeConstraints); |
| 96 | + $this->assertEquals(false, $fieldDefinition->fieldTypeConstraints->validators['FileSizeValidator']['maxFileSize']); |
| 97 | + $this->assertEquals('', $fieldDefinition->fieldTypeConstraints->fieldSettings['allowedTypes']); |
| 98 | + } |
| 99 | + |
| 100 | + public function testToFieldDefinitionWithValidator() |
| 101 | + { |
| 102 | + $fieldDefinition = new FieldDefinition(); |
| 103 | + $storage = new StorageFieldDefinition(); |
| 104 | + $storage->dataInt1 = 55; |
| 105 | + $storage->dataText1 = "text/plain"; |
| 106 | + |
| 107 | + $this->converter->toFieldDefinition($storage, $fieldDefinition); |
| 108 | + |
| 109 | + $this->assertInstanceOf(FieldTypeConstraints::class, $fieldDefinition->fieldTypeConstraints); |
| 110 | + $this->assertEquals(55, $fieldDefinition->fieldTypeConstraints->validators['FileSizeValidator']['maxFileSize']); |
| 111 | + $this->assertEquals("text/plain", $fieldDefinition->fieldTypeConstraints->fieldSettings['allowedTypes']); |
| 112 | + } |
| 113 | +} |
0 commit comments