Skip to content

Commit

Permalink
issue 78 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Feb 11, 2020
1 parent 85fe93d commit 440381f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 20 deletions.
21 changes: 19 additions & 2 deletions src/XBase/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/XBase/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Binary file added tests/Resources/socios.dbf
Binary file not shown.
86 changes: 68 additions & 18 deletions tests/WritableTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace XBase\Tests;

use PHPUnit\Framework\TestCase;
use XBase\Enum\TableType;
use XBase\Record;
use XBase\Table;
use XBase\WritableTable;
Expand All @@ -11,13 +12,18 @@ class WritableTableTest extends TestCase
{
const FILEPATH = __DIR__.'/Resources/cbr_072019b1.dbf';

public function testSet()
private function duplicateFile(string $file): string
{
$info = pathinfo(self::FILEPATH);
$info = pathinfo($file);
$newName = uniqid($info['filename']);
$copyTo = "{$info['dirname']}/$newName.{$info['extension']}";
self::assertTrue(copy(self::FILEPATH, $copyTo));
self::assertTrue(copy($file, $copyTo));
return $copyTo;
}

public function testSet()
{
$copyTo = $this->duplicateFile(self::FILEPATH);
try {
$table = new WritableTable($copyTo, null, 'cp866');
$table->openWrite();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
}
}

0 comments on commit 440381f

Please sign in to comment.