class XlsTest in Excel Serialization 8
Tests the XLS encoder.
@group xls_serialization
@coversDefaultClass \Drupal\xls_serialization\Encoder\Xls
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\xls_serialization\Unit\Encoder\XlsTest
Expanded class hierarchy of XlsTest
File
- tests/
src/ Unit/ Encoder/ XlsTest.php, line 17
Namespace
Drupal\Tests\xls_serialization\Unit\EncoderView source
class XlsTest extends UnitTestCase {
/**
* @covers ::supportsEncoding
*/
public function testSupportsEncoding() {
$encoder = new Xls();
$this
->assertTrue($encoder
->supportsEncoding('xls'));
$this
->assertFalse($encoder
->supportsEncoding('doc'));
}
/**
* @covers ::encode
*/
public function testEncode() {
$data = [
[
'foo' => 'bar',
'biz' => 'baz',
],
[
'foo' => 'bar1',
'biz' => 'baz1',
],
[
'foo' => 'bar2',
'biz' => 'baz2',
],
];
$encoder = new Xls();
$encoded = $encoder
->encode($data, 'xlsx');
// Load the file and verify the data.
$file = $this
->loadXlsFile($encoded);
$sheet = $file
->getSheet(0);
// Verify headers.
$this
->assertEquals('foo', $sheet
->getCellByColumnAndRow(1, 1)
->getValue());
$this
->assertEquals('biz', $sheet
->getCellByColumnAndRow(2, 1)
->getValue());
// Verify some of the data.
$this
->assertEquals('bar1', $sheet
->getCellByColumnAndRow(1, 3)
->getValue());
$this
->assertEquals('baz2', $sheet
->getCellByColumnAndRow(2, 4)
->getValue());
}
/**
* Tests metadata.
*
* @covers ::encode
*/
public function testEncodeMetaData() {
// Test metadata.
$style_plugin = new \stdClass();
$style_plugin->options = [
'xls_settings' => [
'xls_format' => 'Excel2007',
'metadata' => [
'creator' => 'J Author',
'last_modified_by' => 'That one guy, down the hall?',
'created' => 1320998400,
'modified' => 1355299200,
'title' => 'A fantastic title. The best title.',
'description' => 'Such a great description. Everybody is saying.',
'subject' => 'This spreadsheet is about numbers.',
'keywords' => 'testing xls spreadsheets',
'category' => 'test category',
'manager' => 'J Q Manager',
'company' => 'Drupal',
'custom_properties' => [
'foo' => 'bar',
'biz' => [
12345.12,
'f',
],
'baz' => [
1320998400,
'd',
],
],
],
],
];
$context['views_style_plugin'] = $style_plugin;
$encoder = new Xls();
$encoded = $encoder
->encode([], 'xlsx', $context);
$file = $this
->loadXlsFile($encoded, 'xlsx');
$metadata = $style_plugin->options['xls_settings']['metadata'];
$properties = $file
->getProperties();
$this
->assertEquals($metadata['creator'], $properties
->getCreator());
$this
->assertEquals($metadata['last_modified_by'], $properties
->getLastModifiedBy());
$this
->assertEquals($metadata['created'], $properties
->getCreated());
$this
->assertEquals($metadata['modified'], $properties
->getModified());
$this
->assertEquals($metadata['title'], $properties
->getTitle());
$this
->assertEquals($metadata['description'], $properties
->getDescription());
$this
->assertEquals($metadata['subject'], $properties
->getSubject());
$this
->assertEquals($metadata['keywords'], $properties
->getKeywords());
$this
->assertEquals($metadata['category'], $properties
->getCategory());
$this
->assertEquals($metadata['manager'], $properties
->getManager());
$this
->assertEquals($metadata['company'], $properties
->getCompany());
// Verify custom properties.
$this
->assertEquals('bar', $properties
->getCustomPropertyValue('foo'));
$this
->assertEquals('12345.12', $properties
->getCustomPropertyValue('biz'));
$this
->assertEquals('1320998400', $properties
->getCustomPropertyValue('baz'));
}
/**
* Helper function to retrieve an xls object for a xls file.
*
* @param object $xls
* The xls file contents.
* @param string $format
* The format the xls file is in. Defaults to 'xls'.
*
* @return \PHPExcel
* The excel object.
*/
protected function loadXlsFile($xls, $format = 'xls') {
// PHPExcel only supports files, so write the xls to a temporary file.
$xls_file = @tempnam(File::sysGetTempDir(), 'phpxltmp.' . $format);
file_put_contents($xls_file, $xls);
return IOFactory::load($xls_file);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 | |
XlsTest:: |
protected | function | Helper function to retrieve an xls object for a xls file. | |
XlsTest:: |
public | function | @covers ::encode | |
XlsTest:: |
public | function | Tests metadata. | |
XlsTest:: |
public | function | @covers ::supportsEncoding |