|
| 1 | +<?php |
| 2 | +namespace PHPWord\Tests; |
| 3 | + |
| 4 | +use PHPUnit_Framework_TestCase; |
| 5 | +use PHPWord; |
| 6 | +use PHPWord_DocumentProperties; |
| 7 | +use PHPWord_Section; |
| 8 | +use PHPWord_Style; |
| 9 | + |
| 10 | +/** |
| 11 | + * @covers PHPWord |
| 12 | + */ |
| 13 | +class PHPWordTest extends PHPUnit_Framework_TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var PHPWord |
| 17 | + */ |
| 18 | + protected $object; |
| 19 | + |
| 20 | + /** |
| 21 | + * @covers PHPWord::__construct |
| 22 | + * @covers PHPWord::getProperties |
| 23 | + * @covers PHPWord::getDefaultFontName |
| 24 | + * @covers PHPWord::getDefaultFontSize |
| 25 | + */ |
| 26 | + public function testConstruct() |
| 27 | + { |
| 28 | + $object = new PHPWord(); |
| 29 | + $this->assertEquals( |
| 30 | + new PHPWord_DocumentProperties(), |
| 31 | + $object->getProperties() |
| 32 | + ); |
| 33 | + $this->assertEquals( |
| 34 | + PHPWord::DEFAULT_FONT_NAME, |
| 35 | + $object->getDefaultFontName() |
| 36 | + ); |
| 37 | + $this->assertEquals( |
| 38 | + PHPWord::DEFAULT_FONT_SIZE, |
| 39 | + $object->getDefaultFontSize() |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @covers PHPWord::setProperties |
| 45 | + * @covers PHPWord::getProperties |
| 46 | + */ |
| 47 | + public function testSetGetProperties() |
| 48 | + { |
| 49 | + $object = new PHPWord(); |
| 50 | + $creator = 'PHPWord'; |
| 51 | + $properties = $object->getProperties(); |
| 52 | + $properties->setCreator($creator); |
| 53 | + $object->setProperties($properties); |
| 54 | + $this->assertEquals($creator, $object->getProperties()->getCreator()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @covers PHPWord::createSection |
| 59 | + * @covers PHPWord::getSections |
| 60 | + */ |
| 61 | + public function testCreateGetSections() |
| 62 | + { |
| 63 | + $object = new PHPWord(); |
| 64 | + $this->assertEquals(new PHPWord_Section(1), $object->createSection()); |
| 65 | + $object->createSection(); |
| 66 | + $this->assertEquals(2, count($object->getSections())); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @covers PHPWord::setDefaultFontName |
| 71 | + * @covers PHPWord::getDefaultFontName |
| 72 | + */ |
| 73 | + public function testSetGetDefaultFontName() |
| 74 | + { |
| 75 | + $object = new PHPWord(); |
| 76 | + $fontName = 'Times New Roman'; |
| 77 | + $this->assertEquals( |
| 78 | + PHPWord::DEFAULT_FONT_NAME, |
| 79 | + $object->getDefaultFontName() |
| 80 | + ); |
| 81 | + $object->setDefaultFontName($fontName); |
| 82 | + $this->assertEquals($fontName, $object->getDefaultFontName()); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @covers PHPWord::setDefaultFontSize |
| 87 | + * @covers PHPWord::getDefaultFontSize |
| 88 | + */ |
| 89 | + public function testSetGetDefaultFontSize() |
| 90 | + { |
| 91 | + $object = new PHPWord(); |
| 92 | + $fontSize = 16; |
| 93 | + $this->assertEquals( |
| 94 | + PHPWord::DEFAULT_FONT_SIZE, |
| 95 | + $object->getDefaultFontSize() |
| 96 | + ); |
| 97 | + $object->setDefaultFontSize($fontSize); |
| 98 | + $this->assertEquals($fontSize, $object->getDefaultFontSize()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @covers PHPWord::setDefaultParagraphStyle |
| 103 | + * @covers PHPWord::loadTemplate |
| 104 | + */ |
| 105 | + public function testSetDefaultParagraphStyle() |
| 106 | + { |
| 107 | + $object = new PHPWord(); |
| 108 | + $object->setDefaultParagraphStyle(array()); |
| 109 | + $this->assertInstanceOf( |
| 110 | + 'PHPWord_Style_Paragraph', |
| 111 | + PHPWord_Style::getStyle('Normal') |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @covers PHPWord::addParagraphStyle |
| 117 | + * @covers PHPWord::addFontStyle |
| 118 | + * @covers PHPWord::addTableStyle |
| 119 | + * @covers PHPWord::addLinkStyle |
| 120 | + */ |
| 121 | + public function testAddStyles() |
| 122 | + { |
| 123 | + $object = new PHPWord(); |
| 124 | + $styles = array('Paragraph' => 'Paragraph', 'Font' => 'Font', |
| 125 | + 'Table' => 'TableFull', 'Link' => 'Font'); |
| 126 | + foreach ($styles as $key => $value) { |
| 127 | + $method = "add{$key}Style"; |
| 128 | + $styleId = "{$key} Style"; |
| 129 | + $styleType = "PHPWord_Style_{$value}"; |
| 130 | + $object->$method($styleId, array()); |
| 131 | + $this->assertInstanceOf( |
| 132 | + $styleType, |
| 133 | + PHPWord_Style::getStyle($styleId) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @covers PHPWord::addTitleStyle |
| 141 | + */ |
| 142 | + public function testAddTitleStyle() |
| 143 | + { |
| 144 | + $object = new PHPWord(); |
| 145 | + $titleLevel = 1; |
| 146 | + $titleName = "Heading_{$titleLevel}"; |
| 147 | + $object->addTitleStyle($titleLevel, array()); |
| 148 | + $this->assertInstanceOf( |
| 149 | + 'PHPWord_Style_Font', |
| 150 | + PHPWord_Style::getStyle($titleName) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @covers PHPWord::loadTemplate |
| 156 | + */ |
| 157 | + public function testLoadTemplate() |
| 158 | + { |
| 159 | + $file = join( |
| 160 | + DIRECTORY_SEPARATOR, |
| 161 | + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx') |
| 162 | + ); |
| 163 | + $object = new PHPWord(); |
| 164 | + $this->assertInstanceOf( |
| 165 | + 'PHPWord_Template', |
| 166 | + $object->loadTemplate($file) |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @covers PHPWord::loadTemplate |
| 172 | + * @expectedException PHPWord_Exception |
| 173 | + */ |
| 174 | + public function testLoadTemplateException() |
| 175 | + { |
| 176 | + $file = join( |
| 177 | + DIRECTORY_SEPARATOR, |
| 178 | + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blanks.docx') |
| 179 | + ); |
| 180 | + $object = new PHPWord(); |
| 181 | + $object->loadTemplate($file); |
| 182 | + } |
| 183 | +} |
0 commit comments