From 440381f7684a411e9abdeb05f3c72395e20e9294 Mon Sep 17 00:00:00 2001 From: gam6itko Date: Tue, 11 Feb 2020 14:00:08 +0300 Subject: [PATCH] issue 78 tests --- src/XBase/Record.php | 21 ++++++++- src/XBase/Table.php | 1 + tests/Resources/socios.dbf | Bin 0 -> 11177 bytes tests/WritableTableTest.php | 86 ++++++++++++++++++++++++++++-------- 4 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 tests/Resources/socios.dbf diff --git a/src/XBase/Record.php b/src/XBase/Record.php index 9ff7c70..2330d94 100755 --- a/src/XBase/Record.php +++ b/src/XBase/Record.php @@ -269,6 +269,18 @@ public function getDateTime($columnName) return $longdate + ($inttime / 1000); } + /** + * Get DATE(D) or DATETIME(T) data as object of \DateTime class + */ + public function getDateTimeObject(string $columnName): \DateTime + { + $column = $this->getColumn($columnName); + if (!in_array($column->getType(), [self::DBFFIELD_TYPE_DATE, self::DBFFIELD_TYPE_DATETIME])) { + trigger_error($column->getName().' is not a Date or DateTime column', E_USER_ERROR); + } + return new \DateTime($this->forceGetString($columnName)); + } + /** * @param string $columnName * @@ -447,7 +459,7 @@ public function setString($columnObj, $value) if ($columnObj->getType() == self::DBFFIELD_TYPE_CHAR) { $this->forceSetString($columnObj, $value); } else { - if ($columnObj->getType() == self::DBFFIELD_TYPE_DATETIME || $columnObj->getType() == self::DBFFIELD_TYPE_DATE) { + if (($columnObj->getType() == self::DBFFIELD_TYPE_DATETIME || $columnObj->getType() == self::DBFFIELD_TYPE_DATE) && is_string($value)) { $value = strtotime($value); } @@ -541,7 +553,8 @@ public function setDate($columnObj, $value) } if ($value instanceof \DateTimeInterface) { - $value = $value->format('U'); + $this->forceSetString($columnObj, $value->format('Ymd')); + return false; } if (strlen($value) == 0) { @@ -564,6 +577,10 @@ public function setDateTime($columnObj, $value) trigger_error($columnObj->getName().' is not a DateTime column', E_USER_ERROR); } + if ($value instanceof \DateTimeInterface) { + $value = $value->format('U'); + } + if (strlen($value) == 0) { $this->forceSetString($columnObj, ''); return false; diff --git a/src/XBase/Table.php b/src/XBase/Table.php index be75a34..784fdc5 100755 --- a/src/XBase/Table.php +++ b/src/XBase/Table.php @@ -120,6 +120,7 @@ protected function readHeader() } /* some checking */ + clearstatcache(); if ($this->headerLength > filesize($this->tableName)) { throw new TableException(sprintf('File %s is not DBF', $this->tableName)); } diff --git a/tests/Resources/socios.dbf b/tests/Resources/socios.dbf new file mode 100644 index 0000000000000000000000000000000000000000..166bc58b2db726098c121f4b519771833d163845 GIT binary patch literal 11177 zcmeI2O^h5z7025)B*737kcq@0Q6VH0gdJvPy*3sBspfUfY&+f6o&Fe%1BCJJDjH?H zYj)QWAh;k!!G|E3j|1WqA`}iJ3Id@>A&0Oc7bGs6IKmBy4<#TGPVj%#T{|eZ{)wR`Nu(c{O0Ab9Pbx9-AaHUShVMQQz%j^O(4*%AS%u!^O5 zNO$0JkHa+zG>_F-=9baz$RU#wI z4JZ0NzV0{K$rm~=^V&l6U-9^^qZ7utFv=CdgmfOzzi`iUIGuKQiTOw8T`n`3HzPzB zz)wsIBVVdm^-`VY&~FUx>Th`ozUASR>d#kwGeGnWaCdxVmZE*`K%jpBeuvY)o{BWg zm$eFi{o+ zNH2Ie=8t&rko1e-#~lS_uB4%me;J&8#dL!GVR4uu`W0|TJkXN@^eVVJzePn=4kb{h z{)b1tj574!v`BA&<1>@6K6m(=fb4^&6RNgczXk604?{CBs~7z{3gzAULzR`*{~ZH& z?Vpxp-sl&81l+a1qAIdP83WKC2ghe7QI(dGJq-H)36^&X)b^J}m_WVY<4aU3g9H6( zFV}L=aPsmjzS!`Gz5IM-wOhA(?KrD4>s2bi7!cgH`H88rh&V-J^0 zX`ZKMg!N@l{g%V-^wmB10{A!g;HSZ#@^E#bRJ!rc&w#&cW_Dpz>|e@Uc9Fdf?%KcM zjs0(X>aXP$uMheibEkgzzA)`o*d7A?2k_ngiIy+}>?6WI`s5WYC90In|K#CGnKt5q z{tI*bPABk2JkW1=cv{-+GtmD5e~%*%+LK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iak`TJ^yMkEw%}X{W8f?EH zMHfAb-Y#sjn)u=#%WO~V{jRGm;9PgbJw=r$ajyM;I0L6fzCDb4+(F?}s>5^;BOrA! zW84Q(=V2YU;|SR8VaA+pnVUN9;SFQIskpkfwJ}^qNYKPR#p=eBBSb#?t>8w=*)}#s zvbr)_UO%<8zIkP|H5_xa1yv3*0Afir@eFV+1s_-?=*OBvAuZ4+%*m0xMQz$ z#zZ|h?~#QlWtT6)9CzaXVMNNuJoxKe9-U|1P2_`kwLT2u6C(WUM~Ta+8S%Nz`QjcP zWsWXw2j&Xf0?u{UNZ>E+(hlz2%4UF5Bi|mzJ?@|!#y#$c+~_?H`rpp{ZnWw{x!Y<4 z?DjBYPPfbrne!fO7yC}#;l{>jxH1$e)#p9oXme|0bm=M|`rw#H$j7e^MYg^*yo8ga zuZc;>!fd=eT-!phG9KelXj2Tu+UQA~4C$dcG8)g*;+<$?(soduplicateFile(self::FILEPATH); try { $table = new WritableTable($copyTo, null, 'cp866'); $table->openWrite(); @@ -42,11 +48,7 @@ public function testSet() */ public function testAppendRecord() { - $info = pathinfo(self::FILEPATH); - $newName = uniqid($info['filename']); - $copyTo = "{$info['dirname']}/$newName.{$info['extension']}"; - self::assertTrue(copy(self::FILEPATH, $copyTo)); - + $copyTo = $this->duplicateFile(self::FILEPATH); try { $table = new WritableTable($copyTo, null, 'cp866'); $table->openWrite(); @@ -65,6 +67,10 @@ public function testAppendRecord() $table->writeRecord(); $table->close(); + clearstatcache(); + $expectedSize = $table->headerLength + ($table->recordCount * $table->recordByteLength); // Last byte must be 0x1A + self::assertSame($expectedSize, filesize($copyTo)); + $table = new Table($copyTo, null, 'cp866'); self::assertEquals(11, $table->getRecordCount()); $record = $table->pickRecord(10); @@ -85,11 +91,7 @@ public function testAppendRecord() public function testDeleteRecord() { - $info = pathinfo(self::FILEPATH); - $newName = uniqid($info['filename']); - $copyTo = "{$info['dirname']}/$newName.{$info['extension']}"; - self::assertTrue(copy(self::FILEPATH, $copyTo)); - + $copyTo = $this->duplicateFile(self::FILEPATH); try { $table = new WritableTable($copyTo, null, 'cp866'); $table->openWrite(); @@ -110,11 +112,7 @@ public function testDeleteRecord() public function testDeletePackRecord() { - $info = pathinfo(self::FILEPATH); - $newName = uniqid($info['filename']); - $copyTo = "{$info['dirname']}/$newName.{$info['extension']}"; - self::assertTrue(copy(self::FILEPATH, $copyTo)); - + $copyTo = $this->duplicateFile(self::FILEPATH); try { $table = new WritableTable($copyTo, null, 'cp866'); $table->openWrite(); @@ -130,4 +128,56 @@ public function testDeletePackRecord() unlink($copyTo); } } + + public function testIssue78() + { + $fecnacim = date("m/d/Y", 86400); + $fecingreso = date("m/d/Y", 86400 * 2); + + $copyTo = $this->duplicateFile(__DIR__.'/Resources/socios.dbf'); + try { + $table = new WritableTable($copyTo); + self::assertEquals(3, $table->getRecordCount()); + $table->openWrite(); + // fill new newRecord + $newRecord = $table->appendRecord(); + $newRecord->segsocial = '000000000000'; + $newRecord->socio = 'socio'; + $newRecord->apellido = 'apellido'; + $newRecord->nombre = 'nombre'; + $newRecord->fecnacim = $fecnacim; + $newRecord->fecingreso = $fecingreso; + $newRecord->sexo = 'M'; + $newRecord->apartado = '600'; + $newRecord->telefonor = '12345678'; + $newRecord->email = 'someone@email.com'; + $newRecord->venciced = \DateTime::createFromFormat("U", -777859200); + $newRecord->nriesgo = "B"; + //save + $table->writeRecord(); + $table->pack(); + $table->close(); + unset($newRecord); + + $table = new Table($copyTo); + self::assertEquals(4, $table->getRecordCount()); + $record = $table->pickRecord(3); + self::assertEquals('000000000000', $record->segsocial); + self::assertSame('socio', $record->socio); + self::assertSame('apellido', $record->apellido); + self::assertSame('nombre', $record->nombre); + self::assertSame(86400, $record->getDate('fecnacim')); + self::assertSame($fecnacim, $record->getDateTimeObject('fecnacim')->format('m/d/Y')); + self::assertSame($fecingreso, $record->getDateTimeObject('fecingreso')->format('m/d/Y')); + self::assertSame('M', $record->sexo); + self::assertSame('600', $record->apartado); + self::assertSame('12345678', $record->telefonor); + self::assertSame('someone@email.com', $record->email); + self::assertSame('1945-05-09', $record->getDateTimeObject('venciced')->format('Y-m-d')); + self::assertSame("B", $record->nriesgo); + $table->close(); + } finally { + unlink($copyTo); + } + } }