I contribute to sonata / exportorter , the library used to export data in many formats (CSV, JSON, XML, XLS, ...).
I am working on a Writer that converts boolean values ββto strings (e.g. yes / no), encapsulating another Writer (e.g. CsvWriter or XlsWriter).
This is my first experience with phpunit.
All unit tests made on existing writers use this logic:
- Create a file.
- Writing data to a file using the appropriate format.
- Make assertEquals on file_get_contents(filename).
So, I wrote this test:
public function setUp()
{
$this->filename = 'formatedbool.xls';
$this->sampleWriter = new XlsWriter($this->filename, false);
$this->trueLabel = 'oui';
$this->falseLabel = 'non';
if (is_file($this->filename)) {
unlink($this->filename);
}
}
public function testValidDataFormat()
{
$writer = new FormatedBoolWriter($this->sampleWriter, $this->trueLabel, $this->falseLabel);
$writer->open();
$writer->write(array('john', 'doe', false, true));
$writer->close();
$expected = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name=ProgId content=Excel.Sheet><meta name=Generator content="https://github.com/sonata-project/exporter"></head><body><table><tr><td>john</td><td>doe</td><td>non</td><td>oui</td></tr></table></body></html>';
$this->assertEquals($expected, trim(file_get_contents($this->filename)));
}
When sending my PR, the owner tells me:
mock , . . https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.mock-objects.examples.with-consecutive.php
Mock, file_get_contents, .
"write" .
, , bools, , .
?
$ ?
EDIT @Cerad, , :
public function testValidDataFormat()
{
$data = array('john', 'doe', false, true);
$expected = array('john', 'doe', 'no', 'yes');
$mock = $this->getMockBuilder('Exporter\Writer\XlsWriter')
->setConstructorArgs(array('formattedbool.xls', false))
->getMock();
$mock->expects($this->any())
->method('write')
->with($this->equalTo($expected));
$writer = new FormattedBoolWriter($mock, $this->trueLabel, $this->falseLabel);
$writer->open();
$writer->write($data);
$writer->close();
}
.
EDIT PR https://github.com/sonata-project/exporter/pull/56