1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Bpmn\Engine\Variable; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Jabe\Engine\Variable\Variables; |
7
|
|
|
use Jabe\Engine\Variable\Impl\Type\FileValueTypeImpl; |
8
|
|
|
use Jabe\Engine\Variable\Value\FileValueInterface; |
9
|
|
|
use Jabe\Engine\Variable\Value\TypedValueInterface; |
10
|
|
|
use Jabe\Model\Xml\Impl\Util\IoUtil; |
11
|
|
|
|
12
|
|
|
class FileValueTypeImplTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private $type; |
15
|
|
|
|
16
|
|
|
protected function setUp(): void |
17
|
|
|
{ |
18
|
|
|
$this->type = new FileValueTypeImpl(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testNameShouldBeFile(): void |
22
|
|
|
{ |
23
|
|
|
$this->assertEquals("file", $this->type->getName()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testShouldNotHaveParent(): void |
27
|
|
|
{ |
28
|
|
|
$this->assertNull($this->type->getParent()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testIsPrimitiveValue(): void |
32
|
|
|
{ |
33
|
|
|
$this->assertTrue($this->type->isPrimitiveValueType()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testIsNotAnAbstractType(): void |
37
|
|
|
{ |
38
|
|
|
$this->assertFalse($this->type->isAbstract()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCanNotConvertFromAnyValue(): void |
42
|
|
|
{ |
43
|
|
|
// we just use null to make sure false is always returned |
44
|
|
|
$this->assertFalse($this->type->canConvertFromTypedValue(null)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testConvertingThrowsException(): void |
48
|
|
|
{ |
49
|
|
|
$this->expectException(\InvalidArgumentException::class); |
50
|
|
|
$this->type->convertFromTypedValue(Variables::untypedNullValue()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testCreateValueFromFile(): void |
54
|
|
|
{ |
55
|
|
|
$path = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
56
|
|
|
$file = fopen($path, 'r+'); |
57
|
|
|
$value = $this->type->createValue($file, [FileValueTypeImpl::VALUE_INFO_FILE_NAME => $path]); |
58
|
|
|
$this->assertTrue($value instanceof FileValueInterface); |
59
|
|
|
$this->assertTrue($value->getType() instanceof FileValueTypeImpl); |
60
|
|
|
$this->checkStreamFromValue($value, "text"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testCreateValueFromObject(): void |
64
|
|
|
{ |
65
|
|
|
$path = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
66
|
|
|
$this->expectException(\InvalidArgumentException::class); |
67
|
|
|
$this->type->createValue(new \stdClass(), [FileValueTypeImpl::VALUE_INFO_FILE_NAME => $path]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testCreateValueWithProperties(): void |
71
|
|
|
{ |
72
|
|
|
// given |
73
|
|
|
$path = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
74
|
|
|
$file = fopen($path, 'r+'); |
75
|
|
|
$properties = []; |
76
|
|
|
$properties["filename"] = $path; |
77
|
|
|
$properties["mimeType"] = "someMimeType"; |
78
|
|
|
$properties["encoding"] = "someEncoding"; |
79
|
|
|
|
80
|
|
|
$value = $this->type->createValue($file, $properties); |
81
|
|
|
|
82
|
|
|
$this->assertTrue($value instanceof FileValueInterface); |
83
|
|
|
$this->assertEquals($path, $value->getFilename()); |
84
|
|
|
$this->assertEquals("someMimeType", $value->getMimeType()); |
85
|
|
|
$this->assertEquals("someEncoding", $value->getEncoding()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testMimeTypeSetToNull(): void |
89
|
|
|
{ |
90
|
|
|
// given |
91
|
|
|
$path = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
92
|
|
|
$file = fopen($path, 'r+'); |
93
|
|
|
$properties = []; |
94
|
|
|
$properties["filename"] = $path; |
95
|
|
|
$properties["mimeType"] = null; |
96
|
|
|
$properties["encoding"] = "someEncoding"; |
97
|
|
|
$this->expectException(\InvalidArgumentException::class); |
98
|
|
|
$value = $this->type->createValue($file, $properties); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testEncodingSetToNull(): void |
102
|
|
|
{ |
103
|
|
|
// given |
104
|
|
|
$path = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
105
|
|
|
$file = fopen($path, 'r+'); |
106
|
|
|
$properties = []; |
107
|
|
|
$properties["filename"] = $path; |
108
|
|
|
$properties["mimeType"] = "someMimeType"; |
109
|
|
|
$properties["encoding"] = null; |
110
|
|
|
$this->expectException(\InvalidArgumentException::class); |
111
|
|
|
$value = $this->type->createValue($file, $properties); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/*public function testCannotCreateFileWithoutName(): void |
115
|
|
|
{ |
116
|
|
|
$path = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
117
|
|
|
$file = fopen($path, 'r+'); |
118
|
|
|
$this->expectException(\InvalidArgumentException::class); |
119
|
|
|
$this->type->createValue($file, []); |
120
|
|
|
}*/ |
121
|
|
|
|
122
|
|
|
public function testCannotCreateFileWithoutValueInfo(): void |
123
|
|
|
{ |
124
|
|
|
$path = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
125
|
|
|
$file = fopen($path, 'r+'); |
126
|
|
|
$this->expectException(\InvalidArgumentException::class); |
127
|
|
|
$this->type->createValue($file, null); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testCannotCreateFileWithInvalidTransientFlag(): void |
131
|
|
|
{ |
132
|
|
|
$path = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
133
|
|
|
$file = fopen($path, 'r+'); |
134
|
|
|
$info = []; |
135
|
|
|
$info["filename"] = $path; |
136
|
|
|
$info["transient"] = "foo"; |
137
|
|
|
$this->expectException(\InvalidArgumentException::class); |
138
|
|
|
$this->type->createValue($file, $info); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testValueInfoContainsFileTypeNameTransientFlagAndEncoding(): void |
142
|
|
|
{ |
143
|
|
|
$fileName = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
144
|
|
|
$file = fopen($fileName, 'r+'); |
145
|
|
|
$fileType = "text/plain"; |
146
|
|
|
$encoding = "UTF-8"; |
147
|
|
|
$fileValue = Variables::fileValue($fileName)->file($file)->mimeType($fileType) |
148
|
|
|
->encoding($encoding)->setTransient(true)->create(); |
149
|
|
|
$info = $this->type->getValueInfo($fileValue); |
150
|
|
|
$this->assertContains($fileName, $info); |
151
|
|
|
$this->assertContains($fileType, $info); |
152
|
|
|
$this->assertContains($encoding, $info); |
153
|
|
|
$this->assertContains(true, $info); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testFileByteArrayIsEqualToFileValueContent(): void |
157
|
|
|
{ |
158
|
|
|
$fileName = 'tests/Bpmn/Engine/Variable/Resources/simpleFile.txt'; |
159
|
|
|
$file = fopen($fileName, 'r+'); |
160
|
|
|
$fileValue = Variables::fileValue($fileName)->file($file)->create(); |
161
|
|
|
$this->assertEquals(IoUtil::getStringFromInputStream(fopen($fileName, 'r+')), $fileValue->getByteArray()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testDoesNotHaveParent(): void |
165
|
|
|
{ |
166
|
|
|
$this->assertNull($this->type->getParent()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function checkStreamFromValue(TypedValueInterface $value, string $expected): void |
170
|
|
|
{ |
171
|
|
|
$this->assertEquals($expected, $value->getByteArray()); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|