XMLWriterTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * This file is part of PHPOffice Common
  4. *
  5. * PHPOffice Common is free software distributed under the terms of the GNU Lesser
  6. * General Public License version 3 as published by the Free Software Foundation.
  7. *
  8. * For the full copyright and license information, please read the LICENSE
  9. * file that was distributed with this source code. For the full list of
  10. * contributors, visit https://github.com/PHPOffice/Common/contributors.
  11. *
  12. * @link https://github.com/PHPOffice/Common
  13. * @copyright 2009-2016 PHPOffice Common contributors
  14. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  15. */
  16. namespace PhpOffice\Common\Tests;
  17. use PhpOffice\Common\XMLWriter;
  18. /**
  19. * Test class for XMLWriter
  20. *
  21. * @coversDefaultClass PhpOffice\Common\XMLWriter
  22. */
  23. class XMLWriterTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. */
  27. public function testConstruct()
  28. {
  29. // Memory
  30. $object = new XMLWriter();
  31. $object->startElement('element');
  32. $object->text('AAA');
  33. $object->endElement();
  34. $this->assertEquals('<element>AAA</element>'.chr(10), $object->getData());
  35. // Disk
  36. $object = new XMLWriter(XMLWriter::STORAGE_DISK);
  37. $object->startElement('element');
  38. $object->text('BBB');
  39. $object->endElement();
  40. $this->assertEquals('<element>BBB</element>'.chr(10), $object->getData());
  41. }
  42. public function testWriteAttribute()
  43. {
  44. $xmlWriter = new XMLWriter();
  45. $xmlWriter->startElement('element');
  46. $xmlWriter->writeAttribute('name', 'value');
  47. $xmlWriter->endElement();
  48. $this->assertSame('<element name="value"/>' . chr(10), $xmlWriter->getData());
  49. }
  50. public function testWriteAttributeShouldWriteFloatValueLocaleIndependent()
  51. {
  52. $value = 1.2;
  53. $xmlWriter = new XMLWriter();
  54. $xmlWriter->startElement('element');
  55. $xmlWriter->writeAttribute('name', $value);
  56. $xmlWriter->endElement();
  57. setlocale(LC_NUMERIC, 'de_DE.UTF-8', 'de');
  58. $this->assertSame('1,2', (string)$value);
  59. $this->assertSame('<element name="1.2"/>' . chr(10), $xmlWriter->getData());
  60. }
  61. }