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

Add TableNode::fromList() method #120

Merged
merged 1 commit into from Aug 30, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Behat/Gherkin/Node/TableNode.php
Expand Up @@ -62,6 +62,27 @@ public function __construct(array $table)
}
}

/**
* Creates a table from a given list.
*
* @param array $list One-dimensional array
*
* @return TableNode
*
* @throws NodeException If the given list is not a one-dimensional array
*/
public static function fromList(array $list)
{
if (count($list) !== count($list, COUNT_RECURSIVE)) {
throw new NodeException('List is not a one-dimensional array.');
}

array_walk($list, function (&$item) {
$item = array($item);
});
return new self($list);
}

/**
* Returns node type.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Behat/Gherkin/Node/TableNodeTest.php
Expand Up @@ -219,4 +219,30 @@ public function testGetTableAsString()
TABLE;
$this->assertEquals($expected, $table->getTableAsString());
}

public function testFromList()
{
$table = TableNode::fromList(array(
'everzet',
'antono'
));

$expected = new TableNode(array(
array('everzet'),
array('antono'),
));
$this->assertEquals($expected, $table);
}

/**
* @expectedException \Behat\Gherkin\Exception\NodeException
*/
public function testGetTableFromListWithMultidimensionalArrayArgument()
{
TableNode::fromList(array(
array(1, 2, 3),
array(4, 5, 6)
));
}

}