MailMergeTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_LiveDocx
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: $
  21. */
  22. require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_Service_LiveDocx_MailMergeTest::main');
  25. }
  26. require_once 'Zend/Service/LiveDocx/MailMerge.php';
  27. /**
  28. * Zend_Service_LiveDocx test case
  29. *
  30. * @category Zend
  31. * @package Zend_Service_LiveDocx
  32. * @subpackage UnitTests
  33. * @group Zend_Service
  34. * @group Zend_Service_LiveDocx
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @version $Id: $
  38. */
  39. class Zend_Service_LiveDocx_MailMergeTest extends PHPUnit_Framework_TestCase
  40. {
  41. const TEST_TEMPLATE_1 = 'phpunit-template.docx';
  42. const TEST_TEMPLATE_2 = 'phpunit-template-block-fields.doc';
  43. const ENDPOINT = 'https://api.livedocx.com/1.2/mailmerge.asmx?wsdl';
  44. public $path;
  45. public $phpLiveDocx;
  46. // -------------------------------------------------------------------------
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. public function setUp()
  53. {
  54. if (!constant('TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME')
  55. || !constant('TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD')
  56. ) {
  57. $this->markTestSkipped('LiveDocx tests disabled');
  58. return;
  59. }
  60. $this->phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  61. $this->phpLiveDocx->setUsername(TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME)
  62. ->setPassword(TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD);
  63. foreach($this->phpLiveDocx->listTemplates() as $template) {
  64. $this->phpLiveDocx->deleteTemplate($template['filename']);
  65. }
  66. $this->path = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MailMerge');
  67. }
  68. public function tearDown()
  69. {
  70. if (isset($this->phpLiveDocx)) {
  71. foreach($this->phpLiveDocx->listTemplates() as $template) {
  72. $this->phpLiveDocx->deleteTemplate($template['filename']);
  73. }
  74. unset($this->phpLiveDocx);
  75. }
  76. }
  77. // -------------------------------------------------------------------------
  78. public function testLoginUsernamePassword()
  79. {
  80. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  81. $phpLiveDocx->setUsername(TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME);
  82. $phpLiveDocx->setPassword(TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD);
  83. $this->assertTrue($phpLiveDocx->logIn());
  84. }
  85. public function testLoginUsernamePasswordSoapClient()
  86. {
  87. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  88. $phpLiveDocx->setUsername(TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME);
  89. $phpLiveDocx->setPassword(TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD);
  90. $phpLiveDocx->setSoapClient(new Zend_Soap_Client(self::ENDPOINT));
  91. $this->assertTrue($phpLiveDocx->logIn());
  92. }
  93. /**
  94. * @expectedException Zend_Service_LiveDocx_Exception
  95. */
  96. public function testLoginUsernamePasswordException()
  97. {
  98. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  99. $phpLiveDocx->setUsername('phpunitInvalidUsername');
  100. $phpLiveDocx->setPassword('phpunitInvalidPassword');
  101. $phpLiveDocx->logIn();
  102. }
  103. /**
  104. * @expectedException Zend_Service_LiveDocx_Exception
  105. */
  106. public function testLoginUsernamePasswordSoapClientException()
  107. {
  108. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  109. $phpLiveDocx->setUsername('phpunitInvalidUsername');
  110. $phpLiveDocx->setPassword('phpunitInvalidPassword');
  111. $phpLiveDocx->setSoapClient(new Zend_Soap_Client(self::ENDPOINT));
  112. $phpLiveDocx->logIn();
  113. }
  114. public function testConstructorOptionsUsernamePassword()
  115. {
  116. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
  117. array (
  118. 'username' => TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME,
  119. 'password' => TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD
  120. )
  121. );
  122. $this->assertTrue($phpLiveDocx->logIn());
  123. }
  124. public function testConstructorOptionsUsernamePasswordSoapClient()
  125. {
  126. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
  127. array (
  128. 'username' => TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME,
  129. 'password' => TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD,
  130. 'soapClient' => new Zend_Soap_Client(self::ENDPOINT)
  131. )
  132. );
  133. $this->assertTrue($phpLiveDocx->logIn());
  134. }
  135. // -------------------------------------------------------------------------
  136. public function testSetLocalTemplate()
  137. {
  138. $this->assertTrue(is_a($this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1), 'Zend_Service_LiveDocx_MailMerge'));
  139. $this->setExpectedException('Zend_Service_LiveDocx_Exception');
  140. @$this->phpLiveDocx->setLocalTemplate('phpunit-nonexistent.doc');
  141. }
  142. public function testSetRemoteTemplate()
  143. {
  144. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  145. $this->assertTrue(is_a($this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1), 'Zend_Service_LiveDocx_MailMerge'));
  146. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  147. }
  148. public function testSetFieldValues()
  149. {
  150. $testValues = array('software' => 'phpunit');
  151. // Remote Template
  152. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  153. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1);
  154. $this->assertTrue(is_a($this->phpLiveDocx->setFieldValues($testValues), 'Zend_Service_LiveDocx_MailMerge'));
  155. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  156. // Local Template
  157. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  158. $this->assertTrue(is_a($this->phpLiveDocx->setFieldValues($testValues), 'Zend_Service_LiveDocx_MailMerge'));
  159. }
  160. public function testSetFieldValue()
  161. {
  162. $testKey = 'software';
  163. $testValue = 'phpunit';
  164. // Remote Template
  165. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  166. $this->assertTrue(is_a($this->phpLiveDocx->setFieldValue($testKey, $testValue), 'Zend_Service_LiveDocx_MailMerge'));
  167. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  168. // Local Template
  169. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  170. $this->assertTrue(is_a($this->phpLiveDocx->setFieldValue($testKey, $testValue), 'Zend_Service_LiveDocx_MailMerge'));
  171. }
  172. public function testAssign()
  173. {
  174. $testKey = 'software';
  175. $testValue = 'phpunit';
  176. // Remote Template
  177. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  178. $this->assertTrue(is_a($this->phpLiveDocx->assign($testKey, $testValue), 'Zend_Service_LiveDocx_MailMerge'));
  179. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  180. // Local Template
  181. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  182. $this->assertTrue(is_a($this->phpLiveDocx->assign($testKey, $testValue), 'Zend_Service_LiveDocx_MailMerge'));
  183. }
  184. public function testSetBlockFieldValues()
  185. {
  186. $testKey = 'connection';
  187. $testValues = array(array('connection_number' => 'unittest', 'connection_duration' => 'unittest', 'fee' => 'unittest'),
  188. array('connection_number' => 'unittest', 'connection_duration' => 'unittest', 'fee' => 'unittest'),
  189. array('connection_number' => 'unittest', 'connection_duration' => 'unittest', 'fee' => 'unittest'),
  190. array('connection_number' => 'unittest', 'connection_duration' => 'unittest', 'fee' => 'unittest') );
  191. // Remote Template
  192. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  193. $this->assertTrue(is_a($this->phpLiveDocx->setBlockFieldValues($testKey, $testValues), 'Zend_Service_LiveDocx_MailMerge'));
  194. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  195. // Local Template
  196. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  197. $this->assertTrue(is_a($this->phpLiveDocx->setBlockFieldValues($testKey, $testValues), 'Zend_Service_LiveDocx_MailMerge'));
  198. }
  199. // -------------------------------------------------------------------------
  200. public function testCreateDocument()
  201. {
  202. $testValues = array(
  203. 'software' => 'phpunit',
  204. 'licensee' => 'phpunit',
  205. 'company' => 'phpunit',
  206. 'date' => 'phpunit',
  207. 'time' => 'phpunit',
  208. 'city' => 'phpunit',
  209. 'country' => 'phpunit',
  210. );
  211. // Remote Template
  212. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  213. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1);
  214. $this->phpLiveDocx->assign($testValues);
  215. $this->assertNull($this->phpLiveDocx->createDocument());
  216. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  217. // Local Template
  218. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  219. $this->phpLiveDocx->assign($testValues);
  220. $this->assertNull($this->phpLiveDocx->createDocument());
  221. }
  222. public function testRetrieveDocument()
  223. {
  224. $testValues = array(
  225. 'software' => 'phpunit',
  226. 'licensee' => 'phpunit',
  227. 'company' => 'phpunit',
  228. 'date' => 'phpunit',
  229. 'time' => 'phpunit',
  230. 'city' => 'phpunit',
  231. 'country' => 'phpunit',
  232. );
  233. // PDF and DOCs are always slightly different:
  234. // - PDF because of the timestamp in meta data
  235. // - DOC because of ???
  236. $expectedResults = array(
  237. 'docx' => '50fe2abd9b42e67c3126d355768b6e75',
  238. 'rtf' => '24f950ff620ba194fe5900c3a5360570',
  239. 'txd' => '22d7a7558b19ba8be9fe03b35068cf20',
  240. 'txt' => '3dc103f033ef6efba770c8196059d96d',
  241. 'html' => '8b91dc8617651b6e3142d0716c0f616a',
  242. );
  243. // Remote Template
  244. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  245. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1);
  246. $this->phpLiveDocx->assign($testValues);
  247. $this->phpLiveDocx->createDocument();
  248. foreach($expectedResults as $format => $hash) {
  249. $document = $this->phpLiveDocx->retrieveDocument($format);
  250. $this->assertEquals($hash, md5($document));
  251. }
  252. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  253. // Local Template
  254. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  255. $this->phpLiveDocx->assign($testValues);
  256. $this->phpLiveDocx->createDocument();
  257. foreach($expectedResults as $format => $hash) {
  258. $document = $this->phpLiveDocx->retrieveDocument($format);
  259. $this->assertEquals($hash, md5($document));
  260. }
  261. }
  262. public function testRetrieveDocumentAppended()
  263. {
  264. $testValues = array(
  265. array(
  266. 'software' => 'phpunit - document 1',
  267. 'licensee' => 'phpunit - document 1',
  268. 'company' => 'phpunit - document 1',
  269. 'date' => 'phpunit - document 1',
  270. 'time' => 'phpunit - document 1',
  271. 'city' => 'phpunit - document 1',
  272. 'country' => 'phpunit - document 1',
  273. ),
  274. array(
  275. 'software' => 'phpunit - document 2',
  276. 'licensee' => 'phpunit - document 2',
  277. 'company' => 'phpunit - document 2',
  278. 'date' => 'phpunit - document 2',
  279. 'time' => 'phpunit - document 2',
  280. 'city' => 'phpunit - document 2',
  281. 'country' => 'phpunit - document 2',
  282. ),
  283. );
  284. // PDF and DOCs are always slightly different:
  285. // - PDF because of the timestamp in meta data
  286. // - DOC because of ???
  287. $expectedResults = array(
  288. 'docx' => '0697e57da0c886dee9fa2d5c98335121',
  289. 'rtf' => '9a3f448519e2be0da08a13702fd9d48b',
  290. 'txd' => 'f76a6575e74db5b15b4c4be76157bc03',
  291. 'txt' => 'e997415fd0d5e766b2490fed9386da21',
  292. 'html' => '2dfafbb8f81281dbbae99e131963cd50',
  293. );
  294. // Remote Template
  295. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  296. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1);
  297. $this->phpLiveDocx->assign($testValues);
  298. $this->phpLiveDocx->createDocument();
  299. foreach($expectedResults as $format => $hash) {
  300. $document = $this->phpLiveDocx->retrieveDocument($format);
  301. $this->assertEquals($hash, md5($document));
  302. }
  303. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  304. // Local Template
  305. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  306. $this->phpLiveDocx->assign($testValues);
  307. $this->phpLiveDocx->createDocument();
  308. foreach($expectedResults as $format => $hash) {
  309. $document = $this->phpLiveDocx->retrieveDocument($format);
  310. $this->assertEquals($hash, md5($document));
  311. }
  312. }
  313. // -------------------------------------------------------------------------
  314. public function testGetTemplateFormats()
  315. {
  316. $expectedResults = array('doc' , 'docx' , 'rtf' , 'txd');
  317. $this->assertEquals($expectedResults, $this->phpLiveDocx->getTemplateFormats());
  318. }
  319. public function testGetDocumentFormats()
  320. {
  321. $expectedResults = array('doc' , 'docx' , 'html' , 'pdf' , 'rtf' , 'txd' , 'txt');
  322. $this->assertEquals($expectedResults, $this->phpLiveDocx->getDocumentFormats());
  323. }
  324. public function testGetImageFormats()
  325. {
  326. $expectedResults = array('bmp' , 'gif' , 'jpg' , 'png' , 'tiff');
  327. $this->assertEquals($expectedResults, $this->phpLiveDocx->getImageFormats());
  328. }
  329. // -------------------------------------------------------------------------
  330. public function testGetBitmaps()
  331. {
  332. $testValues = array(
  333. 'software' => 'phpunit',
  334. 'licensee' => 'phpunit',
  335. 'company' => 'phpunit',
  336. 'date' => 'phpunit',
  337. 'time' => 'phpunit',
  338. 'city' => 'phpunit',
  339. 'country' => 'phpunit',
  340. );
  341. $expectedResults = array(
  342. 'bmp' => 'c588ee10d63e0598fe3541a032f509d6',
  343. 'gif' => '2edd4066dda5f4c2049717137d76cf58',
  344. 'jpg' => '8766618c572f19ceccc39af7ad0c8478',
  345. 'png' => '1e12e4937b9ccb0fa6d78dcd342b7f28',
  346. 'tiff' => '014ae48643e3a50f691b7d9442605426',
  347. );
  348. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  349. $this->phpLiveDocx->assign($testValues);
  350. $this->phpLiveDocx->createDocument();
  351. foreach($this->phpLiveDocx->getImageFormats() as $format) {
  352. $bitmaps = $this->phpLiveDocx->getBitmaps(1, 1, 20, $format);
  353. $this->assertEquals($expectedResults[$format], md5(serialize($bitmaps)));
  354. }
  355. }
  356. public function testGetAllBitmaps()
  357. {
  358. $testValues = array(
  359. 'software' => 'phpunit',
  360. 'licensee' => 'phpunit',
  361. 'company' => 'phpunit',
  362. 'date' => 'phpunit',
  363. 'time' => 'phpunit',
  364. 'city' => 'phpunit',
  365. 'country' => 'phpunit',
  366. );
  367. $expectedResults = array(
  368. 'bmp' => '0ae732498dd3798fc51c1ccccd09e3e3',
  369. 'gif' => '9a5f7bfa2aafd8b99f6955b8bdbb8bf7',
  370. 'jpg' => '38550446bfc84af3ddd1a0f3339a84dd',
  371. 'png' => 'a3b5517bb118db67b8a8259652a389c2',
  372. 'tiff' => 'b49aa783c14bc7f07776d816085894a3',
  373. );
  374. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  375. $this->phpLiveDocx->assign($testValues);
  376. $this->phpLiveDocx->createDocument();
  377. foreach($this->phpLiveDocx->getImageFormats() as $format) {
  378. $bitmaps = $this->phpLiveDocx->getAllBitmaps(20, $format);
  379. $this->assertEquals($expectedResults[$format], md5(serialize($bitmaps)));
  380. }
  381. }
  382. public function testGetFontNames()
  383. {
  384. $fonts = $this->phpLiveDocx->getFontNames();
  385. if (is_array($fonts) && count($fonts) > 5) {
  386. foreach (array('Courier New' , 'Verdana' , 'Arial' , 'Times New Roman') as $font) {
  387. if (in_array($font, $fonts)) {
  388. $this->assertTrue(true);
  389. } else {
  390. $this->assertTrue(false);
  391. }
  392. }
  393. } else {
  394. $this->assertTrue(false);
  395. }
  396. }
  397. public function testGetFieldNames()
  398. {
  399. $expectedResults = array('phone', 'date', 'name', 'customer_number', 'invoice_number', 'account_number', 'service_phone', 'service_fax', 'month', 'monthly_fee', 'total_net', 'tax', 'tax_value', 'total');
  400. // Remote Template
  401. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  402. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_2);
  403. $this->assertEquals($expectedResults, $this->phpLiveDocx->getFieldNames());
  404. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  405. // Local Template
  406. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  407. $this->assertEquals($expectedResults, $this->phpLiveDocx->getFieldNames());
  408. }
  409. public function testGetBlockFieldNames()
  410. {
  411. $expectedResults = array('connection_number', 'connection_duration', 'fee');
  412. // Remote Template
  413. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  414. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_2);
  415. $this->assertEquals($expectedResults, $this->phpLiveDocx->getBlockFieldNames('connection'));
  416. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  417. // Local Template
  418. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  419. $this->assertEquals($expectedResults, $this->phpLiveDocx->getBlockFieldNames('connection'));
  420. }
  421. public function testGetBlockNames()
  422. {
  423. $expectedResults = array('connection');
  424. // Remote Template
  425. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  426. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_2);
  427. $this->assertEquals($expectedResults, $this->phpLiveDocx->getBlockNames());
  428. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  429. // Local Template
  430. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  431. $this->assertEquals($expectedResults, $this->phpLiveDocx->getBlockNames());
  432. }
  433. // -------------------------------------------------------------------------
  434. public function testUploadTemplate()
  435. {
  436. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  437. $this->assertNull($this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2));
  438. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  439. }
  440. public function testDownloadTemplate()
  441. {
  442. $expectedResults = '2f076af778ca5f8afc9661cfb9deb7c6';
  443. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  444. $template = $this->phpLiveDocx->downloadTemplate(self::TEST_TEMPLATE_2);
  445. $this->assertEquals($expectedResults, md5($template));
  446. }
  447. public function testDeleteTemplate()
  448. {
  449. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  450. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  451. $templateDeleted = true;
  452. foreach($this->phpLiveDocx->listTemplates() as $template) {
  453. if($template['filename'] == self::TEST_TEMPLATE_2) {
  454. $templateDeleted = false;
  455. }
  456. }
  457. $this->assertTrue($templateDeleted);
  458. }
  459. public function testListTemplates()
  460. {
  461. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  462. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  463. // Where templates uploaded and are being listed?
  464. $testTemplate1Exists = false;
  465. $testTemplate2Exists = false;
  466. $templates = $this->phpLiveDocx->listTemplates();
  467. foreach($templates as $template) {
  468. if(self::TEST_TEMPLATE_1 === $template['filename']) {
  469. $testTemplate1Exists = true;
  470. } elseif(self::TEST_TEMPLATE_2 === $template['filename']) {
  471. $testTemplate2Exists = true;
  472. }
  473. }
  474. $this->assertTrue($testTemplate1Exists && $testTemplate2Exists);
  475. // Is all info about templates available?
  476. $expectedResults = array('filename', 'fileSize', 'createTime', 'modifyTime');
  477. foreach($templates as $template) {
  478. $this->assertEquals($expectedResults, array_keys($template));
  479. }
  480. // Is all info about templates correct?
  481. foreach($templates as $template) {
  482. $this->assertTrue(strlen($template['filename']) > 0);
  483. $this->assertTrue($template['fileSize'] > 1);
  484. $this->assertTrue($template['createTime'] > mktime(0, 0, 0, 1, 1, 1980));
  485. $this->assertTrue($template['modifyTime'] > mktime(0, 0, 0, 1, 1, 1980));
  486. }
  487. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  488. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  489. }
  490. public function testTemplateExists()
  491. {
  492. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  493. $this->assertTrue($this->phpLiveDocx->templateExists(self::TEST_TEMPLATE_2));
  494. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  495. }
  496. // -------------------------------------------------------------------------
  497. public function testAssocArrayToArrayOfArrayOfString()
  498. {
  499. $testValues = array(
  500. 'a' => '1',
  501. 'b' => '2',
  502. 'c' => '3',
  503. );
  504. $expectedResults = array(
  505. array('a', 'b', 'c'),
  506. array('1', '2', '3'),
  507. );
  508. $actualResults = Zend_Service_LiveDocx_MailMerge::assocArrayToArrayOfArrayOfString($testValues);
  509. $this->assertEquals($expectedResults, $actualResults);
  510. }
  511. public function testMultiAssocArrayToArrayOfArrayOfString()
  512. {
  513. $testValues = array(
  514. array(
  515. 'a' => '1',
  516. 'b' => '2',
  517. 'c' => '3',
  518. ),
  519. array(
  520. 'a' => '4',
  521. 'b' => '5',
  522. 'c' => '6',
  523. ),
  524. array(
  525. 'a' => '7',
  526. 'b' => '8',
  527. 'c' => '9',
  528. ),
  529. );
  530. $expectedResults = array(
  531. array('a', 'b', 'c'),
  532. array('1', '2', '3'),
  533. array('4', '5', '6'),
  534. array('7', '8', '9'),
  535. );
  536. $actualResults = Zend_Service_LiveDocx_MailMerge::multiAssocArrayToArrayOfArrayOfString($testValues);
  537. $this->assertEquals($expectedResults, $actualResults);
  538. }
  539. }
  540. if (PHPUnit_MAIN_METHOD == 'Zend_Service_LiveDocx_MailMergeTest::main') {
  541. Zend_Service_LiveDocx_MailMergeTest::main();
  542. }