kopia lustrzana https://github.com/magicbug/Cloudlog
removed more unnecessary files
rodzic
271c4ff20e
commit
fce353575b
|
@ -1,328 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace AsciiTable\Test;
|
||||
|
||||
use AsciiTable\Builder;
|
||||
use AsciiTable\Exception\BuilderException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BuilderTest extends TestCase
|
||||
{
|
||||
public function testAddRows()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRow(['age' => 21, 'name' => 'John']);
|
||||
|
||||
$person = new class implements \JsonSerializable {
|
||||
|
||||
function jsonSerialize()
|
||||
{
|
||||
return [
|
||||
'age' => 32,
|
||||
'name' => 'Bill'
|
||||
];
|
||||
}
|
||||
};
|
||||
|
||||
$builder->addRow($person);
|
||||
|
||||
$table = $builder->getTable();
|
||||
$rows = $table->getRows();
|
||||
|
||||
$this->assertEquals(21, $rows[0]->getCell('age')->getValue());
|
||||
$this->assertEquals('John', $rows[0]->getCell('name')->getValue());
|
||||
|
||||
$this->assertEquals(32, $rows[1]->getCell('age')->getValue());
|
||||
$this->assertEquals('Bill', $rows[1]->getCell('name')->getValue());
|
||||
}
|
||||
|
||||
public function testAddInvalidRow()
|
||||
{
|
||||
$this->expectException(BuilderException::class);
|
||||
|
||||
$person = new class {
|
||||
public $name;
|
||||
};
|
||||
|
||||
$person->name = 'Rick';
|
||||
|
||||
$builder = new Builder();
|
||||
$builder->addRow($person);
|
||||
}
|
||||
|
||||
public function testRenderEmptyTable()
|
||||
{
|
||||
$this->expectException(BuilderException::class);
|
||||
|
||||
$builder = new Builder();
|
||||
$builder->renderTable();
|
||||
}
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRows([
|
||||
[
|
||||
'name' => 'John',
|
||||
'age' => 23,
|
||||
'sex' => 'male'
|
||||
],
|
||||
[
|
||||
'name' => 'Catherine',
|
||||
'age' => 22,
|
||||
'sex' => 'female'
|
||||
],
|
||||
[
|
||||
'name' => 'Johnathan',
|
||||
'age' => 44,
|
||||
'sex' => 'male'
|
||||
]
|
||||
]);
|
||||
|
||||
$result = $builder->renderTable();
|
||||
|
||||
$expected = <<<EOD
|
||||
┌───────────┬─────┬────────┐
|
||||
│ name │ age │ sex │
|
||||
├───────────┼─────┼────────┤
|
||||
│ John │ 23 │ male │
|
||||
│ Catherine │ 22 │ female │
|
||||
│ Johnathan │ 44 │ male │
|
||||
└───────────┴─────┴────────┘
|
||||
EOD;
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testRenderWithSomeEmptyCells()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRows([
|
||||
[
|
||||
'name' => 'John',
|
||||
'age' => 23,
|
||||
'sex' => 'male'
|
||||
],
|
||||
[
|
||||
'name' => 'Catherine',
|
||||
'sex' => 'female'
|
||||
],
|
||||
[
|
||||
'name' => 'Johnathan',
|
||||
'age' => 44,
|
||||
]
|
||||
]);
|
||||
|
||||
$result = $builder->renderTable();
|
||||
|
||||
$expected = <<<EOD
|
||||
┌───────────┬─────┬────────┐
|
||||
│ name │ age │ sex │
|
||||
├───────────┼─────┼────────┤
|
||||
│ John │ 23 │ male │
|
||||
│ Catherine │ │ female │
|
||||
│ Johnathan │ 44 │ │
|
||||
└───────────┴─────┴────────┘
|
||||
EOD;
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testShowsOnlyVisibleColumns()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRows([
|
||||
[
|
||||
'name' => 'John',
|
||||
'age' => 23,
|
||||
'sex' => 'male'
|
||||
],
|
||||
[
|
||||
'name' => 'Catherine',
|
||||
'age' => 22,
|
||||
'sex' => 'female'
|
||||
],
|
||||
[
|
||||
'name' => 'Johnathan',
|
||||
'age' => 44,
|
||||
'sex' => 'male'
|
||||
]
|
||||
]);
|
||||
|
||||
$builder->showColumns(['name', 'age']);
|
||||
$result = $builder->renderTable();
|
||||
|
||||
$expected = <<<EOD
|
||||
┌───────────┬─────┐
|
||||
│ name │ age │
|
||||
├───────────┼─────┤
|
||||
│ John │ 23 │
|
||||
│ Catherine │ 22 │
|
||||
│ Johnathan │ 44 │
|
||||
└───────────┴─────┘
|
||||
EOD;
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testRenderTableWithFloatingPoint()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRows([
|
||||
[
|
||||
'Order No' => 'A0001',
|
||||
'Product Name' => 'Intel CPU',
|
||||
'Price' => 700.00,
|
||||
'Quantity' => 1
|
||||
],
|
||||
[
|
||||
'Order No' => 'A0002',
|
||||
'Product Name' => 'Hard disk 10TB',
|
||||
'Price' => 500.00,
|
||||
'Quantity' => 2
|
||||
],
|
||||
[
|
||||
'Order No' => 'A0003',
|
||||
'Product Name' => 'Dell Laptop',
|
||||
'Price' => 11600.00,
|
||||
'Quantity' => 8
|
||||
],
|
||||
[
|
||||
'Order No' => 'A0004',
|
||||
'Product Name' => 'Intel CPU',
|
||||
'Price' => 5200.00,
|
||||
'Quantity' => 3
|
||||
]
|
||||
]);
|
||||
|
||||
$builder->addRow([
|
||||
'Order No' => 'A0005',
|
||||
'Product Name' => 'A4Tech Mouse',
|
||||
'Price' => 100.00,
|
||||
'Quantity' => 10
|
||||
]);
|
||||
|
||||
$result = $builder->renderTable();
|
||||
|
||||
$expected = <<<EOD
|
||||
┌──────────┬────────────────┬───────────┬──────────┐
|
||||
│ Order No │ Product Name │ Price │ Quantity │
|
||||
├──────────┼────────────────┼───────────┼──────────┤
|
||||
│ A0001 │ Intel CPU │ 700.00 │ 1 │
|
||||
│ A0002 │ Hard disk 10TB │ 500.00 │ 2 │
|
||||
│ A0003 │ Dell Laptop │ 11 600.00 │ 8 │
|
||||
│ A0004 │ Intel CPU │ 5 200.00 │ 3 │
|
||||
│ A0005 │ A4Tech Mouse │ 100.00 │ 10 │
|
||||
└──────────┴────────────────┴───────────┴──────────┘
|
||||
EOD;
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testRenderTableWithCyrillicWord()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRows([
|
||||
[
|
||||
'name' => 'John',
|
||||
'age' => 21
|
||||
],
|
||||
[
|
||||
'name' => 'Иван',
|
||||
'age' => 23
|
||||
]
|
||||
]);
|
||||
|
||||
$result = $builder->renderTable();
|
||||
|
||||
$expected = <<<EOD
|
||||
┌──────┬─────┐
|
||||
│ name │ age │
|
||||
├──────┼─────┤
|
||||
│ John │ 21 │
|
||||
│ Иван │ 23 │
|
||||
└──────┴─────┘
|
||||
EOD;
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testRenderWithCyrillicColumnNames()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRows([
|
||||
[
|
||||
'имя' => 'Джон',
|
||||
'год' => 21
|
||||
],
|
||||
[
|
||||
'имя' => 'Иван',
|
||||
'год' => 23
|
||||
]
|
||||
]);
|
||||
|
||||
$result = $builder->renderTable();
|
||||
|
||||
$expected = <<<EOD
|
||||
┌──────┬─────┐
|
||||
│ имя │ год │
|
||||
├──────┼─────┤
|
||||
│ Джон │ 21 │
|
||||
│ Иван │ 23 │
|
||||
└──────┴─────┘
|
||||
EOD;
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
public function testRenderWithChineseColumnNames()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRows([
|
||||
[
|
||||
'language' => 'English',
|
||||
'greeting' => 'Hello, World'
|
||||
],
|
||||
[
|
||||
'language' => 'mixed',
|
||||
'greeting' => 'Hello, 世界'
|
||||
]
|
||||
]);
|
||||
|
||||
$result = $builder->renderTable();
|
||||
|
||||
$expected = <<<EOD
|
||||
┌──────────┬──────────────┐
|
||||
│ language │ greeting │
|
||||
├──────────┼──────────────┤
|
||||
│ English │ Hello, World │
|
||||
│ mixed │ Hello, 世界 │
|
||||
└──────────┴──────────────┘
|
||||
EOD;
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testRenderWithTitle()
|
||||
{
|
||||
$builder = new Builder();
|
||||
$builder->addRows([
|
||||
[
|
||||
'language' => 'English',
|
||||
'greeting' => 'Hello, World'
|
||||
],
|
||||
[
|
||||
'language' => 'mixed',
|
||||
'greeting' => 'Hello, 世界'
|
||||
]
|
||||
]);
|
||||
$builder->setTitle('Greetings');
|
||||
|
||||
$result = $builder->renderTable();
|
||||
|
||||
$expected = <<<EOD
|
||||
Greetings
|
||||
┌──────────┬──────────────┐
|
||||
│ language │ greeting │
|
||||
├──────────┼──────────────┤
|
||||
│ English │ Hello, World │
|
||||
│ mixed │ Hello, 世界 │
|
||||
└──────────┴──────────────┘
|
||||
EOD;
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace AsciiTable\Test;
|
||||
|
||||
use AsciiTable\Cell;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CellTest extends TestCase
|
||||
{
|
||||
public function testCellWithDifferentTypesOfValues()
|
||||
{
|
||||
$cell = new Cell('age', '21');
|
||||
$this->assertEquals('21', $cell->getValue());
|
||||
$this->assertEquals(2, $cell->getWidth());
|
||||
|
||||
$cell->setValue(123);
|
||||
$this->assertEquals('123', $cell->getValue());
|
||||
$this->assertEquals(3, $cell->getWidth());
|
||||
|
||||
$ageObject = new class (2008) {
|
||||
private $year;
|
||||
|
||||
function __construct(int $year)
|
||||
{
|
||||
$this->year = $year;
|
||||
}
|
||||
|
||||
function __toString()
|
||||
{
|
||||
return strval(2017 - $this->year);
|
||||
}
|
||||
};
|
||||
|
||||
$cell->setValue($ageObject);
|
||||
$this->assertEquals(1, $cell->getWidth());
|
||||
|
||||
$floatCell = new Cell('price', 100.00);
|
||||
$this->assertEquals("100.00", $floatCell->getValue());
|
||||
|
||||
$floatCell->setValue(99.99);
|
||||
$this->assertEquals("99.99", $floatCell->getValue());
|
||||
}
|
||||
|
||||
public function testCellWidthWithMultiByteCharacters()
|
||||
{
|
||||
$cell = new Cell('name', 'Иван');
|
||||
$this->assertEquals(4, $cell->getWidth());
|
||||
|
||||
$cell = new Cell('message', 'Hello, 世界'); // 世界 - this string is with width 4
|
||||
$this->assertEquals(11, $cell->getWidth());
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace AsciiTable\Test;
|
||||
|
||||
use AsciiTable\Cell;
|
||||
use AsciiTable\Row;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RowTest extends TestCase
|
||||
{
|
||||
public function testCellNotDuplicated()
|
||||
{
|
||||
$row = new Row();
|
||||
$row->addCell(new Cell('name', 'John'));
|
||||
$row->addCell(new Cell('name', 'Rick'));
|
||||
|
||||
$this->assertCount(1, $row->getCells());
|
||||
|
||||
$cell = $row->getCells()->get('name');
|
||||
|
||||
$this->assertEquals($cell->getColumnName(), 'name');
|
||||
$this->assertEquals($cell->getValue(), 'Rick');
|
||||
}
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace AsciiTable\Test;
|
||||
|
||||
use AsciiTable\Cell;
|
||||
use AsciiTable\Row;
|
||||
use AsciiTable\Table;
|
||||
use Ds\Set;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class TableTest extends TestCase
|
||||
{
|
||||
public function testSetVisibleColumns()
|
||||
{
|
||||
$table = new Table();
|
||||
$table->setVisibleColumns(['name', 'age', 'location', 'sex']);
|
||||
|
||||
$set = new Set();
|
||||
$set->add('name');
|
||||
$set->add('age');
|
||||
$set->add('location');
|
||||
$set->add('sex');
|
||||
|
||||
self::assertEquals(0, $set->diff($table->getVisibleColumns())->count());
|
||||
self::assertEquals(0, $table->getVisibleColumns()->diff($set)->count());
|
||||
}
|
||||
|
||||
public function testGetVisibleColumnsWhenNotSet()
|
||||
{
|
||||
$row = new Row();
|
||||
$row->addCell(new Cell('name', 'Catherine'));
|
||||
$row->addCell(new Cell('sex', 'female'));
|
||||
$row->addCell(new Cell('height', 168));
|
||||
|
||||
$table = new Table();
|
||||
$table->addRow($row);
|
||||
|
||||
$this->assertSame(['name', 'sex', 'height'], $table->getVisibleColumns()->toArray());
|
||||
}
|
||||
|
||||
public function testGetAllColumns()
|
||||
{
|
||||
$row = new Row();
|
||||
$row->addCell(new Cell('name', 'Bill'));
|
||||
$row->addCell(new Cell('age', 21));
|
||||
|
||||
$row2 = new Row();
|
||||
$row2->addCell(new Cell('name', 'John'));
|
||||
$row2->addCell(new Cell('sex', 'male'));
|
||||
|
||||
$row3 = new Row();
|
||||
$row3->addCell(new Cell('name', 'Catherine'));
|
||||
$row3->addCell(new Cell('sex', 'female'));
|
||||
$row3->addCell(new Cell('height', 168));
|
||||
|
||||
$table = new Table();
|
||||
$table->addRow($row);
|
||||
$table->addRow($row2);
|
||||
$table->addRow($row3);
|
||||
|
||||
$this->assertSame(['name', 'age', 'sex', 'height'], $table->getAllColumns()->toArray());
|
||||
}
|
||||
|
||||
public function testColumnWidth()
|
||||
{
|
||||
$row = new Row();
|
||||
$row->addCell(new Cell('name', 'Jon'));
|
||||
$row->addCell(new Cell('age', 21));
|
||||
|
||||
$row2 = new Row();
|
||||
$row2->addCell(new Cell('name', 'John'));
|
||||
$row2->addCell(new Cell('age', 32));
|
||||
|
||||
$row3 = new Row();
|
||||
$row3->addCell(new Cell('name', 'Johnathan'));
|
||||
$row3->addCell(new Cell('age', 23));
|
||||
|
||||
$table = new Table();
|
||||
|
||||
$table->addRow($row);
|
||||
$table->addRow($row2);
|
||||
$table->addRow($row3);
|
||||
|
||||
$this->assertEquals(3, $table->getColumnWidth('age'));
|
||||
$this->assertEquals(9, $table->getColumnWidth('name'));
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue