MailMergeTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 TEST_IMAGE_1 = 'image-01.png';
  44. const TEST_IMAGE_2 = 'image-02.png';
  45. const ENDPOINT = 'https://api.livedocx.com/2.0/mailmerge.asmx?wsdl';
  46. public $path;
  47. public $phpLiveDocx;
  48. // -------------------------------------------------------------------------
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. public function setUp()
  55. {
  56. if (!constant('TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME')
  57. || !constant('TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD')
  58. ) {
  59. $this->markTestSkipped('LiveDocx tests disabled');
  60. return;
  61. }
  62. $this->phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  63. $this->phpLiveDocx->setUsername(TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME)
  64. ->setPassword(TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD);
  65. foreach($this->phpLiveDocx->listTemplates() as $template) {
  66. $this->phpLiveDocx->deleteTemplate($template['filename']);
  67. }
  68. $this->path = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MailMerge');
  69. }
  70. public function tearDown()
  71. {
  72. if (isset($this->phpLiveDocx)) {
  73. foreach($this->phpLiveDocx->listTemplates() as $template) {
  74. $this->phpLiveDocx->deleteTemplate($template['filename']);
  75. }
  76. unset($this->phpLiveDocx);
  77. }
  78. }
  79. // -------------------------------------------------------------------------
  80. public function testLoginUsernamePassword()
  81. {
  82. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  83. $phpLiveDocx->setUsername(TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME);
  84. $phpLiveDocx->setPassword(TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD);
  85. $this->assertTrue($phpLiveDocx->logIn());
  86. }
  87. public function testLoginUsernamePasswordSoapClient()
  88. {
  89. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  90. $phpLiveDocx->setUsername(TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME);
  91. $phpLiveDocx->setPassword(TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD);
  92. $phpLiveDocx->setSoapClient(new Zend_Soap_Client(self::ENDPOINT));
  93. $this->assertTrue($phpLiveDocx->logIn());
  94. }
  95. /**
  96. * @expectedException Zend_Service_LiveDocx_Exception
  97. */
  98. public function testLoginUsernamePasswordException()
  99. {
  100. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  101. $phpLiveDocx->setUsername('phpunitInvalidUsername');
  102. $phpLiveDocx->setPassword('phpunitInvalidPassword');
  103. $phpLiveDocx->logIn();
  104. }
  105. /**
  106. * @expectedException Zend_Service_LiveDocx_Exception
  107. */
  108. public function testLoginUsernamePasswordSoapClientException()
  109. {
  110. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  111. $phpLiveDocx->setUsername('phpunitInvalidUsername');
  112. $phpLiveDocx->setPassword('phpunitInvalidPassword');
  113. $phpLiveDocx->setSoapClient(new Zend_Soap_Client(self::ENDPOINT));
  114. $phpLiveDocx->logIn();
  115. }
  116. public function testConstructorOptionsUsernamePassword()
  117. {
  118. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
  119. array (
  120. 'username' => TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME,
  121. 'password' => TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD
  122. )
  123. );
  124. $this->assertTrue($phpLiveDocx->logIn());
  125. }
  126. public function testConstructorOptionsUsernamePasswordSoapClient()
  127. {
  128. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge(
  129. array (
  130. 'username' => TESTS_ZEND_SERVICE_LIVEDOCX_USERNAME,
  131. 'password' => TESTS_ZEND_SERVICE_LIVEDOCX_PASSWORD,
  132. 'soapClient' => new Zend_Soap_Client(self::ENDPOINT)
  133. )
  134. );
  135. $this->assertTrue($phpLiveDocx->logIn());
  136. }
  137. // -------------------------------------------------------------------------
  138. public function testSetLocalTemplate()
  139. {
  140. $this->assertTrue(is_a($this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1), 'Zend_Service_LiveDocx_MailMerge'));
  141. $this->setExpectedException('Zend_Service_LiveDocx_Exception');
  142. @$this->phpLiveDocx->setLocalTemplate('phpunit-nonexistent.doc');
  143. }
  144. public function testSetRemoteTemplate()
  145. {
  146. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  147. $this->assertTrue(is_a($this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1), 'Zend_Service_LiveDocx_MailMerge'));
  148. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  149. }
  150. public function testSetFieldValues()
  151. {
  152. $testValues = array('software' => 'phpunit');
  153. // Remote Template
  154. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  155. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1);
  156. $this->assertTrue(is_a($this->phpLiveDocx->setFieldValues($testValues), 'Zend_Service_LiveDocx_MailMerge'));
  157. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  158. // Local Template
  159. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  160. $this->assertTrue(is_a($this->phpLiveDocx->setFieldValues($testValues), 'Zend_Service_LiveDocx_MailMerge'));
  161. }
  162. public function testSetFieldValue()
  163. {
  164. $testKey = 'software';
  165. $testValue = 'phpunit';
  166. // Remote Template
  167. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  168. $this->assertTrue(is_a($this->phpLiveDocx->setFieldValue($testKey, $testValue), 'Zend_Service_LiveDocx_MailMerge'));
  169. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  170. // Local Template
  171. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  172. $this->assertTrue(is_a($this->phpLiveDocx->setFieldValue($testKey, $testValue), 'Zend_Service_LiveDocx_MailMerge'));
  173. }
  174. public function testAssign()
  175. {
  176. $testKey = 'software';
  177. $testValue = 'phpunit';
  178. // Remote Template
  179. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  180. $this->assertTrue(is_a($this->phpLiveDocx->assign($testKey, $testValue), 'Zend_Service_LiveDocx_MailMerge'));
  181. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  182. // Local Template
  183. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  184. $this->assertTrue(is_a($this->phpLiveDocx->assign($testKey, $testValue), 'Zend_Service_LiveDocx_MailMerge'));
  185. }
  186. public function testSetBlockFieldValues()
  187. {
  188. $testKey = 'connection';
  189. $testValues = array(array('connection_number' => 'unittest', 'connection_duration' => 'unittest', 'fee' => 'unittest'),
  190. array('connection_number' => 'unittest', 'connection_duration' => 'unittest', 'fee' => 'unittest'),
  191. array('connection_number' => 'unittest', 'connection_duration' => 'unittest', 'fee' => 'unittest'),
  192. array('connection_number' => 'unittest', 'connection_duration' => 'unittest', 'fee' => 'unittest') );
  193. // Remote Template
  194. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  195. $this->assertTrue(is_a($this->phpLiveDocx->setBlockFieldValues($testKey, $testValues), 'Zend_Service_LiveDocx_MailMerge'));
  196. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  197. // Local Template
  198. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  199. $this->assertTrue(is_a($this->phpLiveDocx->setBlockFieldValues($testKey, $testValues), 'Zend_Service_LiveDocx_MailMerge'));
  200. }
  201. // -------------------------------------------------------------------------
  202. public function testCreateDocument()
  203. {
  204. $testValues = array(
  205. 'software' => 'phpunit',
  206. 'licensee' => 'phpunit',
  207. 'company' => 'phpunit',
  208. 'date' => 'phpunit',
  209. 'time' => 'phpunit',
  210. 'city' => 'phpunit',
  211. 'country' => 'phpunit',
  212. );
  213. // Remote Template
  214. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  215. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1);
  216. $this->phpLiveDocx->assign($testValues);
  217. $this->assertNull($this->phpLiveDocx->createDocument());
  218. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  219. // Local Template
  220. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  221. $this->phpLiveDocx->assign($testValues);
  222. $this->assertNull($this->phpLiveDocx->createDocument());
  223. }
  224. public function testRetrieveDocument()
  225. {
  226. $testValues = array(
  227. 'software' => 'phpunit',
  228. 'licensee' => 'phpunit',
  229. 'company' => 'phpunit',
  230. 'date' => 'phpunit',
  231. 'time' => 'phpunit',
  232. 'city' => 'phpunit',
  233. 'country' => 'phpunit',
  234. );
  235. // PDF and DOCs are always slightly different:
  236. // - PDF because of the timestamp in meta data
  237. // - DOC because of ???
  238. $expectedResults = array(
  239. 'docx' => 'f21728491855c27a9e64a47266c2a720',
  240. 'rtf' => 'fb75deabf481b0264927cb4a5c9db765',
  241. 'txd' => 'd1f645405ded0718edff6ae6f50a496e',
  242. 'txt' => 'ec2f680646540edd79cd22773fa7e183',
  243. 'html' => 'e3a28523794b0071501c09f791f8c795',
  244. );
  245. // Remote Template
  246. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  247. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1);
  248. $this->phpLiveDocx->assign($testValues);
  249. $this->phpLiveDocx->createDocument();
  250. foreach($expectedResults as $format => $hash) {
  251. $document = $this->phpLiveDocx->retrieveDocument($format);
  252. $this->assertEquals($hash, md5($document));
  253. }
  254. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  255. // Local Template
  256. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  257. $this->phpLiveDocx->assign($testValues);
  258. $this->phpLiveDocx->createDocument();
  259. foreach($expectedResults as $format => $hash) {
  260. $document = $this->phpLiveDocx->retrieveDocument($format);
  261. $this->assertEquals($hash, md5($document));
  262. }
  263. }
  264. public function testRetrieveDocumentAppended()
  265. {
  266. $testValues = array(
  267. array(
  268. 'software' => 'phpunit - document 1',
  269. 'licensee' => 'phpunit - document 1',
  270. 'company' => 'phpunit - document 1',
  271. 'date' => 'phpunit - document 1',
  272. 'time' => 'phpunit - document 1',
  273. 'city' => 'phpunit - document 1',
  274. 'country' => 'phpunit - document 1',
  275. ),
  276. array(
  277. 'software' => 'phpunit - document 2',
  278. 'licensee' => 'phpunit - document 2',
  279. 'company' => 'phpunit - document 2',
  280. 'date' => 'phpunit - document 2',
  281. 'time' => 'phpunit - document 2',
  282. 'city' => 'phpunit - document 2',
  283. 'country' => 'phpunit - document 2',
  284. ),
  285. );
  286. // PDF and DOCs are always slightly different:
  287. // - PDF because of the timestamp in meta data
  288. // - DOC because of ???
  289. $expectedResults = array(
  290. 'docx' => '2757b4d10c8c031d8f501231be39fcfe',
  291. 'rtf' => '2997e531011d826f315291fca1351988',
  292. 'txd' => '8377a5a62f2e034974fc299c322d137f',
  293. 'txt' => 'a7d23668f81b314e15d653ab657316f9',
  294. 'html' => '57365a2ff02347a7863626317505e037',
  295. );
  296. // Remote Template
  297. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  298. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_1);
  299. $this->phpLiveDocx->assign($testValues);
  300. $this->phpLiveDocx->createDocument();
  301. foreach($expectedResults as $format => $hash) {
  302. $document = $this->phpLiveDocx->retrieveDocument($format);
  303. $this->assertEquals($hash, md5($document));
  304. }
  305. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  306. // Local Template
  307. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  308. $this->phpLiveDocx->assign($testValues);
  309. $this->phpLiveDocx->createDocument();
  310. foreach($expectedResults as $format => $hash) {
  311. $document = $this->phpLiveDocx->retrieveDocument($format);
  312. $this->assertEquals($hash, md5($document));
  313. }
  314. }
  315. // -------------------------------------------------------------------------
  316. public function testGetTemplateFormats()
  317. {
  318. $expectedResults = array('doc' , 'docx' , 'rtf' , 'txd');
  319. $this->assertEquals($expectedResults, $this->phpLiveDocx->getTemplateFormats());
  320. }
  321. public function testGetDocumentFormats()
  322. {
  323. $expectedResults = array('doc' , 'docx' , 'html' , 'pdf' , 'rtf' , 'txd' , 'txt');
  324. $this->assertEquals($expectedResults, $this->phpLiveDocx->getDocumentFormats());
  325. }
  326. public function testGetImageImportFormats()
  327. {
  328. $expectedResults = array('bmp' , 'gif' , 'jpg' , 'png' , 'tiff', 'wmf');
  329. $this->assertEquals($expectedResults, $this->phpLiveDocx->getImageImportFormats());
  330. }
  331. public function testGetImageExportFormats()
  332. {
  333. $expectedResults = array('bmp' , 'gif' , 'jpg' , 'png' , 'tiff');
  334. $this->assertEquals($expectedResults, $this->phpLiveDocx->getImageExportFormats());
  335. }
  336. // -------------------------------------------------------------------------
  337. public function testGetBitmaps()
  338. {
  339. $testValues = array(
  340. 'software' => 'phpunit',
  341. 'licensee' => 'phpunit',
  342. 'company' => 'phpunit',
  343. 'date' => 'phpunit',
  344. 'time' => 'phpunit',
  345. 'city' => 'phpunit',
  346. 'country' => 'phpunit',
  347. );
  348. $expectedResults = array(
  349. 'bmp' => 'a1934f2153172f021847af7ece9049ce',
  350. 'gif' => 'd7281d7b6352ff897917e25d6b92746f',
  351. 'jpg' => 'e0b20ea2c9a6252886f689f227109085',
  352. 'png' => 'c449f0c2726f869e9a42156e366f1bf9',
  353. 'tiff' => '20a96a94762a531e9879db0aa6bd673f',
  354. );
  355. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  356. $this->phpLiveDocx->assign($testValues);
  357. $this->phpLiveDocx->createDocument();
  358. foreach($this->phpLiveDocx->getImageExportFormats() as $format) {
  359. $bitmaps = $this->phpLiveDocx->getBitmaps(1, 1, 20, $format);
  360. $this->assertEquals($expectedResults[$format], md5(serialize($bitmaps)));
  361. }
  362. }
  363. public function testGetAllBitmaps()
  364. {
  365. $testValues = array(
  366. 'software' => 'phpunit',
  367. 'licensee' => 'phpunit',
  368. 'company' => 'phpunit',
  369. 'date' => 'phpunit',
  370. 'time' => 'phpunit',
  371. 'city' => 'phpunit',
  372. 'country' => 'phpunit',
  373. );
  374. $expectedResults = array(
  375. 'bmp' => 'e8a884ee61c394deec8520fb397d1cf1',
  376. 'gif' => '2255fee47b4af8438b109efc3cb0d304',
  377. 'jpg' => 'e1acfc3001fc62567de2a489eccdb552',
  378. 'png' => '15eac34d08e602cde042862b467fa865',
  379. 'tiff' => '98bad79380a80c9cc43dfffc5158d0f9',
  380. );
  381. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  382. $this->phpLiveDocx->assign($testValues);
  383. $this->phpLiveDocx->createDocument();
  384. foreach($this->phpLiveDocx->getImageExportFormats() as $format) {
  385. $bitmaps = $this->phpLiveDocx->getAllBitmaps(20, $format);
  386. $this->assertEquals($expectedResults[$format], md5(serialize($bitmaps)));
  387. }
  388. }
  389. public function testGetFontNames()
  390. {
  391. $fonts = $this->phpLiveDocx->getFontNames();
  392. if (is_array($fonts) && count($fonts) > 5) {
  393. foreach (array('Courier New' , 'Verdana' , 'Arial' , 'Times New Roman') as $font) {
  394. if (in_array($font, $fonts)) {
  395. $this->assertTrue(true);
  396. } else {
  397. $this->assertTrue(false);
  398. }
  399. }
  400. } else {
  401. $this->assertTrue(false);
  402. }
  403. }
  404. public function testGetFieldNames()
  405. {
  406. $expectedResults = array('phone', 'date', 'name', 'customer_number', 'invoice_number', 'account_number', 'service_phone', 'service_fax', 'month', 'monthly_fee', 'total_net', 'tax', 'tax_value', 'total');
  407. // Remote Template
  408. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  409. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_2);
  410. $this->assertEquals($expectedResults, $this->phpLiveDocx->getFieldNames());
  411. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  412. // Local Template
  413. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  414. $this->assertEquals($expectedResults, $this->phpLiveDocx->getFieldNames());
  415. }
  416. public function testGetBlockFieldNames()
  417. {
  418. $expectedResults = array('connection_number', 'connection_duration', 'fee');
  419. // Remote Template
  420. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  421. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_2);
  422. $this->assertEquals($expectedResults, $this->phpLiveDocx->getBlockFieldNames('connection'));
  423. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  424. // Local Template
  425. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  426. $this->assertEquals($expectedResults, $this->phpLiveDocx->getBlockFieldNames('connection'));
  427. }
  428. public function testGetBlockNames()
  429. {
  430. $expectedResults = array('connection');
  431. // Remote Template
  432. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  433. $this->phpLiveDocx->setRemoteTemplate(self::TEST_TEMPLATE_2);
  434. $this->assertEquals($expectedResults, $this->phpLiveDocx->getBlockNames());
  435. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  436. // Local Template
  437. $this->phpLiveDocx->setLocalTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  438. $this->assertEquals($expectedResults, $this->phpLiveDocx->getBlockNames());
  439. }
  440. // -------------------------------------------------------------------------
  441. public function testUploadTemplate()
  442. {
  443. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  444. $this->assertNull($this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2));
  445. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  446. }
  447. public function testDownloadTemplate()
  448. {
  449. $expectedResults = '2f076af778ca5f8afc9661cfb9deb7c6';
  450. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  451. $template = $this->phpLiveDocx->downloadTemplate(self::TEST_TEMPLATE_2);
  452. $this->assertEquals($expectedResults, md5($template));
  453. }
  454. public function testDeleteTemplate()
  455. {
  456. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  457. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  458. $templateDeleted = true;
  459. foreach($this->phpLiveDocx->listTemplates() as $template) {
  460. if($template['filename'] == self::TEST_TEMPLATE_2) {
  461. $templateDeleted = false;
  462. }
  463. }
  464. $this->assertTrue($templateDeleted);
  465. }
  466. public function testListTemplates()
  467. {
  468. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_1);
  469. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  470. // Where templates uploaded and are being listed?
  471. $testTemplate1Exists = false;
  472. $testTemplate2Exists = false;
  473. $templates = $this->phpLiveDocx->listTemplates();
  474. foreach($templates as $template) {
  475. if(self::TEST_TEMPLATE_1 === $template['filename']) {
  476. $testTemplate1Exists = true;
  477. } elseif(self::TEST_TEMPLATE_2 === $template['filename']) {
  478. $testTemplate2Exists = true;
  479. }
  480. }
  481. $this->assertTrue($testTemplate1Exists && $testTemplate2Exists);
  482. // Is all info about templates available?
  483. $expectedResults = array('filename', 'fileSize', 'createTime', 'modifyTime');
  484. foreach($templates as $template) {
  485. $this->assertEquals($expectedResults, array_keys($template));
  486. }
  487. // Is all info about templates correct?
  488. foreach($templates as $template) {
  489. $this->assertTrue(strlen($template['filename']) > 0);
  490. $this->assertTrue($template['fileSize'] > 1);
  491. $this->assertTrue($template['createTime'] > mktime(0, 0, 0, 1, 1, 1980));
  492. $this->assertTrue($template['modifyTime'] > mktime(0, 0, 0, 1, 1, 1980));
  493. }
  494. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_1);
  495. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  496. }
  497. public function testTemplateExists()
  498. {
  499. $this->phpLiveDocx->uploadTemplate($this->path . DIRECTORY_SEPARATOR . self::TEST_TEMPLATE_2);
  500. $this->assertTrue($this->phpLiveDocx->templateExists(self::TEST_TEMPLATE_2));
  501. $this->phpLiveDocx->deleteTemplate(self::TEST_TEMPLATE_2);
  502. }
  503. // -------------------------------------------------------------------------
  504. public function testUploadImage()
  505. {
  506. $this->phpLiveDocx->deleteImage(self::TEST_IMAGE_2);
  507. $this->assertNull($this->phpLiveDocx->uploadImage($this->path . DIRECTORY_SEPARATOR . self::TEST_IMAGE_2));
  508. $this->phpLiveDocx->deleteImage(self::TEST_IMAGE_2);
  509. }
  510. public function testDownloadImage()
  511. {
  512. $expectedResults = 'f8b663e465acd570414395d5c33541ab';
  513. $this->phpLiveDocx->uploadImage($this->path . DIRECTORY_SEPARATOR . self::TEST_IMAGE_2);
  514. $image = $this->phpLiveDocx->downloadImage(self::TEST_IMAGE_2);
  515. $this->assertEquals($expectedResults, md5($image));
  516. }
  517. public function testDeleteImage()
  518. {
  519. $this->phpLiveDocx->uploadImage($this->path . DIRECTORY_SEPARATOR . self::TEST_IMAGE_2);
  520. $this->phpLiveDocx->deleteImage(self::TEST_IMAGE_2);
  521. $imageDeleted = true;
  522. foreach($this->phpLiveDocx->listImages() as $image) {
  523. if($image['filename'] == self::TEST_IMAGE_2) {
  524. $imageDeleted = false;
  525. }
  526. }
  527. $this->assertTrue($imageDeleted);
  528. }
  529. public function testListImages()
  530. {
  531. $this->phpLiveDocx->uploadImage($this->path . DIRECTORY_SEPARATOR . self::TEST_IMAGE_1);
  532. $this->phpLiveDocx->uploadImage($this->path . DIRECTORY_SEPARATOR . self::TEST_IMAGE_2);
  533. // Where images uploaded and are being listed?
  534. $testImage1Exists = false;
  535. $testImage2Exists = false;
  536. $images = $this->phpLiveDocx->listImages();
  537. foreach($images as $image) {
  538. if(self::TEST_IMAGE_1 === $image['filename']) {
  539. $testImage1Exists = true;
  540. } elseif(self::TEST_IMAGE_2 === $image['filename']) {
  541. $testImage2Exists = true;
  542. }
  543. }
  544. $this->assertTrue($testImage1Exists && $testImage2Exists);
  545. // Is all info about images available?
  546. $expectedResults = array('filename', 'fileSize', 'createTime', 'modifyTime');
  547. foreach($images as $image) {
  548. $this->assertEquals($expectedResults, array_keys($image));
  549. }
  550. // Is all info about images correct?
  551. foreach($images as $image) {
  552. $this->assertTrue(strlen($image['filename']) > 0);
  553. $this->assertTrue($image['fileSize'] > 1);
  554. $this->assertTrue($image['createTime'] > mktime(0, 0, 0, 1, 1, 1980));
  555. $this->assertTrue($image['modifyTime'] > mktime(0, 0, 0, 1, 1, 1980));
  556. }
  557. $this->phpLiveDocx->deleteImage(self::TEST_IMAGE_1);
  558. $this->phpLiveDocx->deleteImage(self::TEST_IMAGE_2);
  559. }
  560. public function testImageExists()
  561. {
  562. $this->phpLiveDocx->uploadImage($this->path . DIRECTORY_SEPARATOR . self::TEST_IMAGE_2);
  563. $this->assertTrue($this->phpLiveDocx->imageExists(self::TEST_IMAGE_2));
  564. $this->phpLiveDocx->deleteImage(self::TEST_IMAGE_2);
  565. }
  566. // -------------------------------------------------------------------------
  567. public function testAssocArrayToArrayOfArrayOfString()
  568. {
  569. $testValues = array(
  570. 'a' => '1',
  571. 'b' => '2',
  572. 'c' => '3',
  573. );
  574. $expectedResults = array(
  575. array('a', 'b', 'c'),
  576. array('1', '2', '3'),
  577. );
  578. $actualResults = Zend_Service_LiveDocx_MailMerge::assocArrayToArrayOfArrayOfString($testValues);
  579. $this->assertEquals($expectedResults, $actualResults);
  580. }
  581. public function testMultiAssocArrayToArrayOfArrayOfString()
  582. {
  583. $testValues = array(
  584. array(
  585. 'a' => '1',
  586. 'b' => '2',
  587. 'c' => '3',
  588. ),
  589. array(
  590. 'a' => '4',
  591. 'b' => '5',
  592. 'c' => '6',
  593. ),
  594. array(
  595. 'a' => '7',
  596. 'b' => '8',
  597. 'c' => '9',
  598. ),
  599. );
  600. $expectedResults = array(
  601. array('a', 'b', 'c'),
  602. array('1', '2', '3'),
  603. array('4', '5', '6'),
  604. array('7', '8', '9'),
  605. );
  606. $actualResults = Zend_Service_LiveDocx_MailMerge::multiAssocArrayToArrayOfArrayOfString($testValues);
  607. $this->assertEquals($expectedResults, $actualResults);
  608. }
  609. }
  610. if (PHPUnit_MAIN_METHOD == 'Zend_Service_LiveDocx_MailMergeTest::main') {
  611. Zend_Service_LiveDocx_MailMergeTest::main();
  612. }