Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use trait for table tests #910

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/Concerns/HandlesTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace atk4\ui\tests\Concerns;

use atk4\ui\Table;

trait HandlesTable
{
/**
* Extract only <tr> out from an atk4\ui\Table given the <tr> data-id attribute value.
*
* @param Table $table
* @param string $rowDataId
*
* @return string
*/
protected function extractTableRow(Table $table, $rowDataId = '1')
{
$matches = [];

preg_match('/<.*data-id="'.$rowDataId.'".*/m', $table->render(), $matches);

return str_replace(["\r", "\n"], '', $matches[0]);
}
}
21 changes: 7 additions & 14 deletions tests/GridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class GridTest extends \atk4\core\PHPUnit_AgileTestCase
{
use Concerns\HandlesTable;

public $m;

public function setUp()
Expand All @@ -15,7 +17,7 @@ public function setUp()
$a[1] = ['id' => 1, 'email' => 'test@test.com', 'password' => 'abc123', 'xtra' => 'xtra'];
$a[2] = ['id' => 2, 'email' => 'test@yahoo.com', 'password' => 'secret'];

$this->m = new MyModel(new \atk4\data\Persistence_Array($a));
$this->m = new MyModel(new \atk4\data\Persistence\Array_($a));
}

public function test1()
Expand All @@ -30,7 +32,7 @@ public function test1()
$this->assertEquals('<td>{$email}</td><td>password={$password}</td>', $t->getDataRowHTML());
$this->assertEquals(
'<tr data-id="1"><td>test@test.com</td><td>password=abc123</td></tr>',
$this->extract($t)
$this->extractTableRow($t)
);
}

Expand All @@ -46,7 +48,7 @@ public function test1a()
$this->assertEquals('<td>{$email}</td><td>***</td>', $t->getDataRowHTML());
$this->assertEquals(
'<tr data-id="1"><td>test@test.com</td><td>***</td></tr>',
$this->extract($t)
$this->extractTableRow($t)
);
}

Expand All @@ -60,7 +62,7 @@ public function test2()
$this->assertEquals('<td>{$email}</td><td><a href="#" title="Delete {$email}?" class="delete"><i class="ui red trash icon"></i>Delete</a></td>', $t->getDataRowHTML());
$this->assertEquals(
'<tr data-id="1"><td>test@test.com</td><td><a href="#" title="Delete test@test.com?" class="delete"><i class="ui red trash icon"></i>Delete</a></td></tr>',
$this->extract($t)
$this->extractTableRow($t)
);
}

Expand All @@ -74,18 +76,9 @@ public function test3()
$this->assertEquals('<td>{$email}</td><td>***</td>', $t->getDataRowHTML());
$this->assertEquals(
'<tr data-id="1"><td>test@test.com</td><td>***</td></tr>',
$this->extract($t)
$this->extractTableRow($t)
);
}

public function extract($t)
{
// extract only <tr> out
$val = $t->render();
preg_match('/<.*data-id="1".*/m', $val, $matches);

return $matches[0];
}
}

class MyModel extends \atk4\data\Model
Expand Down
18 changes: 6 additions & 12 deletions tests/TableColumnColorRatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@

class TableColumnColorRatingTest extends \atk4\core\PHPUnit_AgileTestCase
{
use Concerns\HandlesTable;

public $db;
/** @var Table */
public $table;
public $column;

protected function extract($val)
{
// extract only <tr> out
preg_match('/<.*data-id="1".*/m', $val, $matches);

return $matches[0];
}

public function setUp()
{
$arr = [
Expand Down Expand Up @@ -65,7 +59,7 @@ public function testValueGreaterThanMax()

$this->assertEquals(
'<tr data-id="1"><td>bar</td><td>ref123</td><td style="background-color:#00ff00;">3</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -88,7 +82,7 @@ public function testValueGreaterThanMaxNoColor()

$this->assertEquals(
'<tr data-id="1"><td>bar</td><td>ref123</td><td style="">3</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -115,7 +109,7 @@ public function testValueLowerThanMin()

$this->assertEquals(
'<tr data-id="1"><td>bar</td><td>ref123</td><td style="background-color:#ff0000;">3</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -138,7 +132,7 @@ public function testValueLowerThanMinNoColor()

$this->assertEquals(
'<tr data-id="1"><td>bar</td><td>ref123</td><td style="">3</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand Down
50 changes: 22 additions & 28 deletions tests/TableColumnLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class TableColumnLinkTest extends \atk4\core\PHPUnit_AgileTestCase
{
use Concerns\HandlesTable;

public $db;
public $table;
public $column;
Expand All @@ -27,7 +29,7 @@ public function testgetDataRowHTML()

$this->assertEquals(
'<tr data-id="1"><td>bar</td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -39,7 +41,7 @@ public function testMultipleFormatters()

$this->assertEquals(
'<tr data-id="1"><td><b>bar</b></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -54,7 +56,7 @@ public function testTDLast()

$this->assertEquals(
'<tr data-id="1"><td>bar</td><td>ref123</td><td class="negative right aligned single line">-123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -70,7 +72,7 @@ public function testTDNotLast()

$this->assertEquals(
'<tr data-id="1"><td>bar</td><td>ref123</td><td class="negative right aligned single line"><b>-123</b></td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -87,7 +89,7 @@ public function testTwoMoneys()

$this->assertEquals(
'<tr data-id="1"><td class=" right aligned single line">bar</td><td>ref123</td><td class="negative right aligned single line"><b>-123</b></td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -105,18 +107,10 @@ public function testTemplateStacking()

$this->assertEquals(
'<tr data-id="1"><td><u><b>bar</b></u></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

public function extract($val)
{
// extract only <tr> out
preg_match('/<.*data-id="1".*/m', $val, $matches);

return $matches[0];
}

public function testRender1()
{

Expand All @@ -126,7 +120,7 @@ public function testRender1()

$this->assertEquals(
'<tr data-id="1"><td><u><b>bar</b></u></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -143,7 +137,7 @@ public function testRender1a()

$this->assertEquals(
'<tr data-id="1"><td>bar</td><td>ref123</td><td>hello<b>world</b></td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -158,7 +152,7 @@ public function testLink1()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?id=1">bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -173,7 +167,7 @@ public function testLink1a()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?id=1">bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -185,7 +179,7 @@ public function testLink2()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?id=%7B%24id%7D">bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -195,7 +189,7 @@ public function testLink3()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?id=1">bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -205,7 +199,7 @@ public function testLink4()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?test=1">bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -215,7 +209,7 @@ public function testLink5()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?test=1">bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -225,7 +219,7 @@ public function testLink6()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?test=1" download="true" >bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -235,7 +229,7 @@ public function testLink7()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?test=1" target="_blank" >bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -245,7 +239,7 @@ public function testLink8()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?test=1"><i class="icon info"></i>bar</a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -255,7 +249,7 @@ public function testLink9()

$this->assertEquals(
'<tr data-id="1"><td><a href="example.php?test=1"></a></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -276,7 +270,7 @@ public function testLink10()

$this->assertEquals(
'<tr data-id="1"><td> --- </td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand All @@ -286,7 +280,7 @@ public function testLink11()

$this->assertEquals(
'<tr data-id="1"><td class=""> bar<span class="ui icon link " data-tooltip="ref123"><i class="ui icon info circle"></span></td><td>ref123</td></tr>',
$this->extract($this->table->render())
$this->extractTableRow($this->table)
);
}

Expand Down