alex-kalanis /
kw_input
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace LoadersTests; |
||||
| 4 | |||||
| 5 | |||||
| 6 | use CommonTestClass; |
||||
| 7 | use kalanis\kw_input\Interfaces\IEntry; |
||||
| 8 | use kalanis\kw_input\Loaders; |
||||
| 9 | use kalanis\kw_input\Parsers; |
||||
| 10 | |||||
| 11 | |||||
| 12 | class JsonTest extends CommonTestClass |
||||
| 13 | { |
||||
| 14 | protected $tempFile = ''; |
||||
| 15 | |||||
| 16 | protected function tearDown(): void |
||||
| 17 | { |
||||
| 18 | parent::tearDown(); |
||||
| 19 | if (is_file($this->tempFile)) { |
||||
| 20 | @unlink($this->tempFile); |
||||
|
0 ignored issues
–
show
|
|||||
| 21 | } |
||||
| 22 | } |
||||
| 23 | |||||
| 24 | public function testJson(): void |
||||
| 25 | { |
||||
| 26 | $parser = new Parsers\Json(); |
||||
| 27 | $loader = new Loaders\Json(); |
||||
| 28 | |||||
| 29 | $this->assertInstanceOf(Loaders\ALoader::class, $loader); |
||||
| 30 | |||||
| 31 | $this->setTempData($this->jsonDataset()); |
||||
| 32 | |||||
| 33 | $entries = $loader->loadVars(IEntry::SOURCE_JSON, $parser->parseInput([$this->tempFile])); |
||||
| 34 | |||||
| 35 | $entry = reset($entries); |
||||
| 36 | $this->assertEquals(IEntry::SOURCE_JSON, $entry->getSource()); |
||||
| 37 | $this->assertEquals('foo', $entry->getKey()); |
||||
| 38 | $this->assertEquals('bar', $entry->getValue()); |
||||
| 39 | |||||
| 40 | $entry = next($entries); |
||||
| 41 | $this->assertEquals(IEntry::SOURCE_JSON, $entry->getSource()); |
||||
| 42 | $this->assertEquals('baz', $entry->getKey()); |
||||
| 43 | $this->assertEquals(['rfv' => 123, 'edc'=> 456], $entry->getValue()); |
||||
| 44 | |||||
| 45 | $entry = next($entries); |
||||
| 46 | $this->assertEquals(IEntry::SOURCE_JSON, $entry->getSource()); |
||||
| 47 | $this->assertEquals('sbr', $entry->getKey()); |
||||
| 48 | $this->assertEquals(['cde', 'dgs'], $entry->getValue()); |
||||
| 49 | } |
||||
| 50 | |||||
| 51 | public function testJsonString(): void |
||||
| 52 | { |
||||
| 53 | $parser = new Parsers\Json(); |
||||
| 54 | $loader = new Loaders\Json(); |
||||
| 55 | |||||
| 56 | $this->assertInstanceOf(Loaders\ALoader::class, $loader); |
||||
| 57 | |||||
| 58 | $this->setTempData($this->jsonStringDataset()); |
||||
| 59 | |||||
| 60 | $entries = $loader->loadVars(IEntry::SOURCE_JSON, $parser->parseInput([$this->tempFile])); |
||||
| 61 | |||||
| 62 | $entry = reset($entries); |
||||
| 63 | $this->assertEquals(IEntry::SOURCE_JSON, $entry->getSource()); |
||||
| 64 | $this->assertEquals(0, $entry->getKey()); |
||||
| 65 | $this->assertEquals('Just content', $entry->getValue()); |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | public function testJsonFile(): void |
||||
| 69 | { |
||||
| 70 | $loader = new Loaders\Json(); |
||||
| 71 | $parser = new Parsers\Json(); |
||||
| 72 | |||||
| 73 | $this->assertInstanceOf(Loaders\ALoader::class, $loader); |
||||
| 74 | |||||
| 75 | $this->setTempData($this->jsonFileDataset()); |
||||
| 76 | |||||
| 77 | $entries = $loader->loadVars(IEntry::SOURCE_JSON, $parser->parseInput([$this->tempFile])); |
||||
| 78 | |||||
| 79 | /** @var \kalanis\kw_input\Entries\FileEntry $entry */ |
||||
| 80 | $entry = reset($entries); |
||||
| 81 | $this->assertEquals(IEntry::SOURCE_FILES, $entry->getSource()); |
||||
| 82 | $this->assertEquals('foo', $entry->getKey()); |
||||
| 83 | $this->assertEquals('foo.json', $entry->getValue()); |
||||
| 84 | |||||
| 85 | // now file content |
||||
| 86 | $this->assertNotEmpty($entry->getTempName()); |
||||
| 87 | $this->assertEquals('application/octet-stream', $entry->getMimeType()); |
||||
| 88 | $this->assertEquals(21, $entry->getSize()); |
||||
| 89 | $this->assertEquals('This won' . chr(0) . 't be changed', file_get_contents($entry->getTempName())); |
||||
| 90 | @unlink($entry->getTempName()); |
||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
unlink(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||||
| 91 | |||||
| 92 | // second record is not a file |
||||
| 93 | $entry = next($entries); |
||||
| 94 | $this->assertEquals(IEntry::SOURCE_JSON, $entry->getSource()); |
||||
| 95 | $this->assertEquals('bar', $entry->getKey()); |
||||
| 96 | $this->assertEquals(['ijn' => ['FILE' => 'This will be changed']], $entry->getValue()); |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | public function testJsonNothing(): void |
||||
| 100 | { |
||||
| 101 | $loader = new Loaders\Json(); |
||||
| 102 | $parser = new Parsers\Json(); |
||||
| 103 | |||||
| 104 | $this->setTempData('{}'); |
||||
| 105 | |||||
| 106 | $entries = $loader->loadVars(IEntry::SOURCE_JSON, $parser->parseInput([$this->tempFile])); |
||||
| 107 | |||||
| 108 | $this->assertEmpty($entries); |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | public function testJsonBadToDecode(): void |
||||
| 112 | { |
||||
| 113 | $loader = new Loaders\Json(); |
||||
| 114 | $parser = new Parsers\Json(); |
||||
| 115 | |||||
| 116 | $this->setTempData('This is not a valid JSON string {\0'); |
||||
| 117 | |||||
| 118 | $entries = $loader->loadVars(IEntry::SOURCE_JSON, $parser->parseInput([$this->tempFile])); |
||||
| 119 | |||||
| 120 | $this->assertEmpty($entries); |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | public function testJsonNoToDecode(): void |
||||
| 124 | { |
||||
| 125 | $loader = new Loaders\Json(); |
||||
| 126 | $parser = new Parsers\Json(); |
||||
| 127 | |||||
| 128 | $this->setTempData('This is not a valid JSON string {\0'); |
||||
| 129 | |||||
| 130 | $entries = $loader->loadVars(IEntry::SOURCE_JSON, $parser->parseInput([])); |
||||
| 131 | |||||
| 132 | $this->assertEmpty($entries); |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | public function testJsonNoFile(): void |
||||
| 136 | { |
||||
| 137 | $loader = new Loaders\Json(); |
||||
| 138 | $parser = new Parsers\Json(); |
||||
| 139 | |||||
| 140 | $entries = $loader->loadVars(IEntry::SOURCE_JSON, $parser->parseInput(['not_exists'])); |
||||
| 141 | |||||
| 142 | $this->assertEmpty($entries); |
||||
| 143 | } |
||||
| 144 | |||||
| 145 | protected function setTempData(string $dataset): void |
||||
| 146 | { |
||||
| 147 | $this->tempFile = tempnam(sys_get_temp_dir(), 'js_test_'); |
||||
| 148 | file_put_contents($this->tempFile, $dataset); |
||||
| 149 | } |
||||
| 150 | } |
||||
| 151 |
If you suppress an error, we recommend checking for the error condition explicitly: