/tests/PhpWord/Tests/MediaTest.php

https://github.com/cyrillkalita/PHPWord · PHP · 119 lines · 61 code · 14 blank · 44 comment · 0 complexity · 0050648ce17d126e749a4ccb8be867d3 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Tests;
  18. use PhpOffice\PhpWord\Element\Image;
  19. use PhpOffice\PhpWord\Media;
  20. /**
  21. * Test class for PhpOffice\PhpWord\Media
  22. *
  23. * @runTestsInSeparateProcesses
  24. */
  25. class MediaTest extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * Get section media elements
  29. */
  30. public function testGetSectionMediaElementsWithNull()
  31. {
  32. $this->assertEquals(Media::getElements('section'), array());
  33. }
  34. /**
  35. * Count section media elements
  36. */
  37. public function testCountSectionMediaElementsWithNull()
  38. {
  39. $this->assertEquals(Media::countElements('section'), 0);
  40. }
  41. /**
  42. * Add section media element
  43. */
  44. public function testAddSectionMediaElement()
  45. {
  46. $local = __DIR__ . "/_files/images/mars.jpg";
  47. $object = __DIR__ . "/_files/documents/sheet.xls";
  48. $remote = 'http://php.net/images/logos/php-med-trans-light.gif';
  49. Media::addElement('section', 'image', $local, new Image($local));
  50. Media::addElement('section', 'image', $local, new Image($local));
  51. Media::addElement('section', 'image', $remote, new Image($local));
  52. Media::addElement('section', 'object', $object);
  53. Media::addElement('section', 'object', $object);
  54. $this->assertEquals(3, Media::countElements('section'));
  55. }
  56. /**
  57. * Add section link
  58. */
  59. public function testAddSectionLinkElement()
  60. {
  61. $expected = Media::countElements('section') + 1;
  62. $actual = Media::addElement('section', 'link', 'http://test.com');
  63. $this->assertEquals($expected, $actual);
  64. $this->assertEquals(1, Media::countElements('section', 'link'));
  65. $this->assertEquals(1, count(Media::getElements('section', 'link')));
  66. }
  67. /**
  68. * Add header media element
  69. */
  70. public function testAddHeaderMediaElement()
  71. {
  72. $local = __DIR__ . "/_files/images/mars.jpg";
  73. $remote = 'http://php.net/images/logos/php-med-trans-light.gif';
  74. Media::addElement('header1', 'image', $local, new Image($local));
  75. Media::addElement('header1', 'image', $local, new Image($local));
  76. Media::addElement('header1', 'image', $remote, new Image($remote));
  77. $this->assertEquals(2, Media::countElements('header1'));
  78. $this->assertEquals(2, count(Media::getElements('header1')));
  79. $this->assertEmpty(Media::getElements('header2'));
  80. }
  81. /**
  82. * Add footer media element and reset media
  83. */
  84. public function testAddFooterMediaElement()
  85. {
  86. $local = __DIR__ . "/_files/images/mars.jpg";
  87. $remote = 'http://php.net/images/logos/php-med-trans-light.gif';
  88. Media::addElement('footer1', 'image', $local, new Image($local));
  89. Media::addElement('footer1', 'image', $local, new Image($local));
  90. Media::addElement('footer1', 'image', $remote, new Image($remote));
  91. $this->assertEquals(2, Media::countElements('footer1'));
  92. Media::resetElements();
  93. $this->assertEquals(0, Media::countElements('footer1'));
  94. }
  95. /**
  96. * Add image element exception
  97. *
  98. * @expectedException Exception
  99. * @expectedExceptionMessage Image object not assigned.
  100. */
  101. public function testAddElementImageException()
  102. {
  103. Media::addElement('section', 'image', __DIR__ . "/_files/images/mars.jpg");
  104. }
  105. }