No Description

MediaTest.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(0, Media::countElements('section'));
  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->assertCount(3, Media::getElements('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->assertCount(1, Media::getElements('section', 'link'));
  65. }
  66. /**
  67. * Add header media element
  68. */
  69. public function testAddHeaderMediaElement()
  70. {
  71. $local = __DIR__ . "/_files/images/mars.jpg";
  72. $remote = 'http://php.net/images/logos/php-med-trans-light.gif';
  73. Media::addElement('header1', 'image', $local, new Image($local));
  74. Media::addElement('header1', 'image', $local, new Image($local));
  75. Media::addElement('header1', 'image', $remote, new Image($remote));
  76. $this->assertCount(2, Media::getElements('header1'));
  77. $this->assertEmpty(Media::getElements('header2'));
  78. }
  79. /**
  80. * Add footer media element and reset media
  81. */
  82. public function testAddFooterMediaElement()
  83. {
  84. $local = __DIR__ . "/_files/images/mars.jpg";
  85. $remote = 'http://php.net/images/logos/php-med-trans-light.gif';
  86. Media::addElement('footer1', 'image', $local, new Image($local));
  87. Media::addElement('footer1', 'image', $local, new Image($local));
  88. Media::addElement('footer1', 'image', $remote, new Image($remote));
  89. $this->assertCount(2, Media::getElements('footer1'));
  90. Media::resetElements();
  91. $this->assertCount(0, Media::getElements('footer1'));
  92. }
  93. /**
  94. * Add image element exception
  95. *
  96. * @expectedException Exception
  97. * @expectedExceptionMessage Image object not assigned.
  98. */
  99. public function testAddElementImageException()
  100. {
  101. Media::addElement('section', 'image', __DIR__ . "/_files/images/mars.jpg");
  102. }
  103. }