driver_transactions_tests.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /**
  3. * ezcDocumentPdfTransactionalDriverWrapperTests
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Document
  23. * @version //autogen//
  24. * @subpackage Tests
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. require_once 'base.php';
  28. /**
  29. * Test suite for class.
  30. *
  31. * @package Document
  32. * @subpackage Tests
  33. */
  34. class ezcDocumentPdfTransactionalDriverWrapperTests extends ezcDocumentPdfTestCase
  35. {
  36. protected $driver;
  37. protected $mock;
  38. public static function suite()
  39. {
  40. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  41. }
  42. protected function setUp()
  43. {
  44. parent::setUp();
  45. $this->driver = new ezcDocumentPdfTransactionalDriverWrapper();
  46. $this->driver->setDriver(
  47. $this->mock = $this->getMock( 'ezcDocumentPdfDriver', array(
  48. 'createPage',
  49. 'getCurrentLineHeight',
  50. 'calculateWordWidth',
  51. 'setTextFormatting',
  52. 'drawWord',
  53. 'drawImage',
  54. 'drawRectangle',
  55. 'save',
  56. ) )
  57. );
  58. }
  59. protected function tearDown()
  60. {
  61. parent::tearDown();
  62. $this->mock = null;
  63. $this->driver = null;
  64. }
  65. public function testNoIssuedWriteCallsToBackend()
  66. {
  67. $this->mock->expects( $this->never() )->method( 'drawWord' );
  68. // Cause and record calls
  69. $this->driver->drawWord( 0, 0, 'Hello world.' );
  70. }
  71. public function testPassReadCallsToBackend()
  72. {
  73. $this->mock->expects( $this->never() )->method( 'drawWord' );
  74. $this->mock->expects( $this->once() )->method( 'calculateWordWidth' );
  75. // Cause and record calls
  76. $this->driver->calculateWordWidth( 'Hello world.' );
  77. $this->driver->drawWord( 0, 0, 'Hello world.' );
  78. }
  79. public function testPassCombinedReadWriteCallToBackend()
  80. {
  81. $this->mock->expects( $this->once() )->method( 'setTextFormatting' );
  82. // Cause and record calls
  83. $this->driver->setTextFormatting( 'font-size', '12pt' );
  84. }
  85. public function testCommitSingleTransaction()
  86. {
  87. $this->mock->expects( $this->at( 0 ) )->method( 'drawWord' )->with(
  88. $this->equalTo( 0, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'Paragraphs' )
  89. );
  90. $this->mock->expects( $this->at( 1 ) )->method( 'drawWord' )->with(
  91. $this->equalTo( 44, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'are' )
  92. );
  93. // Cause and record calls
  94. $transaction = $this->driver->startTransaction();
  95. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  96. $this->driver->drawWord( 44, 0, 'are' );
  97. $this->driver->commit( $transaction );
  98. }
  99. public function testImplicitCommitMultipleTransactions()
  100. {
  101. $this->mock->expects( $this->at( 0 ) )->method( 'drawWord' )->with(
  102. $this->equalTo( 0, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'Paragraphs' )
  103. );
  104. $this->mock->expects( $this->at( 1 ) )->method( 'drawWord' )->with(
  105. $this->equalTo( 44, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'are' )
  106. );
  107. $this->mock->expects( $this->exactly( 2 ) )->method( 'drawWord' );
  108. // Cause and record calls
  109. $this->driver->startTransaction();
  110. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  111. $transaction = $this->driver->startTransaction();
  112. $this->driver->drawWord( 44, 0, 'are' );
  113. $this->driver->startTransaction();
  114. $this->driver->drawWord( 60, 0, 'separated' );
  115. $this->driver->commit( $transaction );
  116. }
  117. public function testCommitAll()
  118. {
  119. $this->mock->expects( $this->at( 0 ) )->method( 'drawWord' )->with(
  120. $this->equalTo( 0, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'Paragraphs' )
  121. );
  122. $this->mock->expects( $this->at( 1 ) )->method( 'drawWord' )->with(
  123. $this->equalTo( 44, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'are' )
  124. );
  125. // Cause and record calls
  126. $this->driver->startTransaction();
  127. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  128. $this->driver->startTransaction();
  129. $this->driver->drawWord( 44, 0, 'are' );
  130. $this->driver->commit();
  131. }
  132. public function testCommitUnknownTransaction()
  133. {
  134. $this->mock->expects( $this->never() )->method( 'drawWord' );
  135. // Cause and record calls
  136. $this->driver->startTransaction();
  137. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  138. $this->driver->startTransaction();
  139. $this->driver->drawWord( 44, 0, 'are' );
  140. $this->driver->commit( 'unknown id' );
  141. }
  142. public function testRevertTransactions()
  143. {
  144. $this->mock->expects( $this->never() )->method( 'drawWord' );
  145. // Cause and record calls
  146. $transaction = $this->driver->startTransaction();
  147. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  148. $this->driver->drawWord( 44, 0, 'are' );
  149. $this->driver->revert( $transaction );
  150. $this->driver->commit();
  151. }
  152. public function testPartialRevertTransactions()
  153. {
  154. $this->mock->expects( $this->at( 0 ) )->method( 'drawWord' )->with(
  155. $this->equalTo( 0, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'Paragraphs' )
  156. );
  157. $this->mock->expects( $this->exactly( 1 ) )->method( 'drawWord' );
  158. // Cause and record calls
  159. $this->driver->startTransaction();
  160. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  161. $transaction = $this->driver->startTransaction();
  162. $this->driver->drawWord( 44, 0, 'are' );
  163. $this->driver->revert( $transaction );
  164. $this->driver->commit();
  165. }
  166. public function testRevertMultipleTransactions()
  167. {
  168. $this->mock->expects( $this->never() )->method( 'drawWord' );
  169. // Cause and record calls
  170. $transaction = $this->driver->startTransaction();
  171. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  172. $this->driver->startTransaction();
  173. $this->driver->drawWord( 44, 0, 'are' );
  174. $this->driver->revert( $transaction );
  175. $this->driver->commit();
  176. }
  177. public function testAutoCommitOnSave()
  178. {
  179. $this->mock->expects( $this->at( 0 ) )->method( 'drawWord' )->with(
  180. $this->equalTo( 0, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'Paragraphs' )
  181. );
  182. $this->mock->expects( $this->at( 1 ) )->method( 'save' );
  183. // Cause and record calls
  184. $this->driver->startTransaction();
  185. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  186. $this->driver->save();
  187. }
  188. public function testCreatePageCall()
  189. {
  190. $this->mock->expects( $this->at( 0 ) )->method( 'createPage' )->with(
  191. $this->equalTo( 100, 1. ), $this->equalTo( 100, 1. )
  192. );
  193. $this->mock->expects( $this->exactly( 1 ) )->method( 'createPage' );
  194. // Cause and record calls
  195. $this->driver->startTransaction();
  196. $this->driver->createPage( 100, 100 );
  197. $this->driver->save();
  198. }
  199. public function testSetTextFormattingCall()
  200. {
  201. $this->mock->expects( $this->at( 0 ) )->method( 'setTextFormatting' )->with(
  202. $this->equalTo( 'font-size' ), $this->equalTo( '12pt' )
  203. );
  204. $this->mock->expects( $this->at( 1 ) )->method( 'setTextFormatting' )->with(
  205. $this->equalTo( 'font-size' ), $this->equalTo( '12pt' )
  206. );
  207. $this->mock->expects( $this->exactly( 2 ) )->method( 'setTextFormatting' );
  208. // Cause and record calls
  209. $this->driver->startTransaction();
  210. $this->driver->setTextFormatting( 'font-size', '12pt' );
  211. $this->driver->save();
  212. }
  213. public function testCalculateWordWidthCall()
  214. {
  215. $this->mock->expects( $this->at( 0 ) )->method( 'calculateWordWidth' )->with(
  216. $this->equalTo( 'my word' )
  217. )->will( $this->returnValue( 12 ) );
  218. $this->mock->expects( $this->exactly( 1 ) )->method( 'calculateWordWidth' );
  219. // Cause and record calls
  220. $this->driver->startTransaction();
  221. $this->assertEquals(
  222. 12,
  223. $this->driver->calculateWordWidth( 'my word' )
  224. );
  225. $this->driver->save();
  226. }
  227. public function testGetLineHeightCall()
  228. {
  229. $this->mock->expects( $this->at( 0 ) )->method( 'getCurrentLineHeight' )
  230. ->will( $this->returnValue( 12 ) );
  231. $this->mock->expects( $this->exactly( 1 ) )->method( 'getCurrentLineHeight' );
  232. // Cause and record calls
  233. $this->driver->startTransaction();
  234. $this->assertEquals(
  235. 12,
  236. $this->driver->getCurrentLineHeight()
  237. );
  238. $this->driver->save();
  239. }
  240. public function testPageLevelTransactions1()
  241. {
  242. $this->mock->expects( $this->never() )->method( 'drawWord' );
  243. // Cause and record calls
  244. $transaction = $this->driver->startTransaction();
  245. $page1 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  246. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  247. $this->assertSame( $page1, $this->driver->currentPage() );
  248. $page2 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  249. $this->driver->drawWord( 44, 0, 'are' );
  250. $this->assertNotSame( $page1, $page2 );
  251. $this->assertSame( $page2, $this->driver->currentPage() );
  252. $this->driver->revert( $transaction );
  253. $this->driver->commit();
  254. $this->assertSame( null, $this->driver->currentPage() );
  255. }
  256. public function testPageLevelTransactions2()
  257. {
  258. $this->mock->expects( $this->once() )->method( 'drawWord' );
  259. $this->mock->expects( $this->once() )->method( 'createPage' );
  260. // Cause and record calls
  261. $page1 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  262. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  263. $this->assertSame( $page1, $this->driver->currentPage() );
  264. $transaction = $this->driver->startTransaction();
  265. $this->driver->drawWord( 44, 0, 'are' );
  266. $this->driver->revert( $transaction );
  267. $this->driver->commit();
  268. $this->assertSame( $page1, $this->driver->currentPage() );
  269. }
  270. public function testPageLevelTransactions3()
  271. {
  272. $this->mock->expects( $this->once() )->method( 'drawWord' );
  273. $this->mock->expects( $this->once() )->method( 'createPage' );
  274. // Cause and record calls
  275. $page1 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  276. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  277. $this->assertSame( $page1, $this->driver->currentPage() );
  278. $transaction = $this->driver->startTransaction();
  279. $page2 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  280. $this->driver->drawWord( 44, 0, 'are' );
  281. $this->assertNotSame( $page1, $page2 );
  282. $this->assertSame( $page2, $this->driver->currentPage() );
  283. $this->driver->revert( $transaction );
  284. $this->driver->commit();
  285. $this->assertSame( $page1, $this->driver->currentPage() );
  286. }
  287. public function testPageLevelTransactions4()
  288. {
  289. $this->mock->expects( $this->once() )->method( 'drawWord' );
  290. // Cause and record calls
  291. $page1 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  292. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  293. $this->assertSame( $page1, $this->driver->currentPage() );
  294. $page2 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  295. $this->assertNotSame( $page1, $page2 );
  296. $this->assertSame( $page2, $this->driver->currentPage() );
  297. $transaction = $this->driver->startTransaction();
  298. $this->driver->drawWord( 44, 0, 'are' );
  299. $this->driver->revert( $transaction );
  300. $this->driver->commit();
  301. $this->assertSame( $page2, $this->driver->currentPage() );
  302. }
  303. public function testPageLevelTransactionsGoBack1()
  304. {
  305. $this->mock->expects( $this->at( 0 ) )->method( 'createPage' )->with(
  306. $this->equalTo( 210, 1. ), $this->equalTo( 297, 1. )
  307. );
  308. $this->mock->expects( $this->at( 1 ) )->method( 'drawWord' )->with(
  309. $this->equalTo( 0, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'Paragraphs' )
  310. );
  311. $this->mock->expects( $this->at( 2 ) )->method( 'drawWord' )->with(
  312. $this->equalTo( 44, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'are' )
  313. );
  314. $this->mock->expects( $this->at( 3 ) )->method( 'createPage' )->with(
  315. $this->equalTo( 210, 1. ), $this->equalTo( 297, 1. )
  316. );
  317. // Cause and record calls
  318. $page1 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  319. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  320. $page2 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  321. $this->driver->goBackOnePage();
  322. $this->assertSame( $page1, $this->driver->currentPage() );
  323. $this->driver->drawWord( 44, 0, 'are' );
  324. $this->driver->commit();
  325. }
  326. public function testPageLevelTransactionsGoBack2()
  327. {
  328. $this->mock->expects( $this->at( 0 ) )->method( 'createPage' )->with(
  329. $this->equalTo( 210, 1. ), $this->equalTo( 297, 1. )
  330. );
  331. $this->mock->expects( $this->at( 1 ) )->method( 'drawWord' )->with(
  332. $this->equalTo( 0, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'Paragraphs' )
  333. );
  334. $this->mock->expects( $this->at( 2 ) )->method( 'drawWord' )->with(
  335. $this->equalTo( 44, 1. ), $this->equalTo( 0, 1. ), $this->equalTo( 'are' )
  336. );
  337. $this->mock->expects( $this->at( 3 ) )->method( 'createPage' )->with(
  338. $this->equalTo( 210, 1. ), $this->equalTo( 297, 1. )
  339. );
  340. // Cause and record calls
  341. $page1 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  342. $this->driver->drawWord( 0, 0, 'Paragraphs' );
  343. $page2 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  344. $this->driver->goBackOnePage();
  345. $transaction = $this->driver->startTransaction();
  346. $page3 = $this->driver->appendPage( new ezcDocumentPcssStyleInferencer() );
  347. $this->assertSame( $page2, $page3 );
  348. $this->driver->drawWord( 44, 0, 'left' );
  349. $this->driver->revert( $transaction );
  350. $this->assertSame( $page1, $this->driver->currentPage() );
  351. $this->driver->drawWord( 44, 0, 'are' );
  352. $this->driver->commit();
  353. }
  354. }
  355. ?>