DoctrineDbalSpec.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace spec\Gaufrette\Adapter;
  3. //hack - mock php built-in functions
  4. require_once 'functions.php';
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\DBAL\Driver\Statement;
  7. use PhpSpec\ObjectBehavior;
  8. use Prophecy\Argument;
  9. class DoctrineDbalSpec extends ObjectBehavior
  10. {
  11. /**
  12. * @param \Doctrine\DBAL\Connection $connection
  13. */
  14. function let(Connection $connection)
  15. {
  16. $this->beConstructedWith($connection, 'someTableName');
  17. }
  18. function it_is_adapter()
  19. {
  20. $this->shouldHaveType('Gaufrette\Adapter');
  21. }
  22. function it_is_checksum_calculator()
  23. {
  24. $this->shouldHaveType('Gaufrette\Adapter\ChecksumCalculator');
  25. }
  26. function it_does_not_handle_directories()
  27. {
  28. $this->isDirectory('filename')->shouldReturn(false);
  29. }
  30. /**
  31. * @param \Doctrine\DBAL\Connection $connection
  32. */
  33. function it_checks_if_file_exists(Connection $connection)
  34. {
  35. $connection
  36. ->quoteIdentifier(Argument::any())
  37. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  38. $connection
  39. ->fetchColumn('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', array('key' => 'filename'))
  40. ->willReturn(12);
  41. $this->exists('filename')->shouldReturn(true);
  42. $connection
  43. ->fetchColumn('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', array('key' => 'filename'))
  44. ->willReturn(0);
  45. $this->exists('filename')->shouldReturn(false);
  46. }
  47. /**
  48. * @param \Doctrine\DBAL\Connection $connection
  49. */
  50. function it_writes_to_new_file(Connection $connection)
  51. {
  52. $connection
  53. ->quoteIdentifier(Argument::any())
  54. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  55. $connection
  56. ->fetchColumn('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', array('key' => 'filename'))
  57. ->willReturn(false);
  58. $connection
  59. ->insert(
  60. 'someTableName',
  61. array(
  62. '"content"' => 'some content',
  63. '"mtime"' => strtotime('2012-10-10 23:10:10'),
  64. '"checksum"' => '9893532233caff98cd083a116b013c0b',
  65. '"key"' => 'filename'
  66. ))
  67. ->shouldBeCalled();
  68. $this->write('filename', 'some content')->shouldReturn(12);
  69. }
  70. /**
  71. * @param \Doctrine\DBAL\Connection $connection
  72. */
  73. function it_write_file(Connection $connection)
  74. {
  75. $connection
  76. ->quoteIdentifier(Argument::any())
  77. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  78. $connection
  79. ->fetchColumn('SELECT COUNT("key") FROM "someTableName" WHERE "key" = :key', array('key' => 'filename'))
  80. ->willReturn(true);
  81. $connection
  82. ->update(
  83. 'someTableName',
  84. array(
  85. '"content"' => 'some content',
  86. '"mtime"' => strtotime('2012-10-10 23:10:10'),
  87. '"checksum"' => '9893532233caff98cd083a116b013c0b',
  88. ),
  89. array(
  90. '"key"' => 'filename'
  91. ))
  92. ->shouldBeCalled();
  93. $this->write('filename', 'some content')->shouldReturn(12);
  94. }
  95. /**
  96. * @param \Doctrine\DBAL\Connection $connection
  97. */
  98. function it_reads_file(Connection $connection)
  99. {
  100. $connection
  101. ->quoteIdentifier(Argument::any())
  102. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  103. $connection
  104. ->fetchColumn('SELECT "content" FROM "someTableName" WHERE "key" = :key', array('key' => 'filename'))
  105. ->willReturn('some content');
  106. $this->read('filename')->shouldReturn('some content');
  107. }
  108. /**
  109. * @param \Doctrine\DBAL\Connection $connection
  110. */
  111. function it_calculates_checksum(Connection $connection)
  112. {
  113. $connection
  114. ->quoteIdentifier(Argument::any())
  115. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  116. $connection
  117. ->fetchColumn('SELECT "checksum" FROM "someTableName" WHERE "key" = :key', array('key' => 'filename'))
  118. ->willReturn(1234);
  119. $this->checksum('filename')->shouldReturn(1234);
  120. }
  121. /**
  122. * @param \Doctrine\DBAL\Connection $connection
  123. */
  124. function it_gets_mtime(Connection $connection)
  125. {
  126. $connection
  127. ->quoteIdentifier(Argument::any())
  128. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  129. $connection
  130. ->fetchColumn('SELECT "mtime" FROM "someTableName" WHERE "key" = :key', array('key' => 'filename'))
  131. ->willReturn(1234);
  132. $this->mtime('filename')->shouldReturn(1234);
  133. }
  134. /**
  135. * @param \Doctrine\DBAL\Connection $connection
  136. */
  137. function it_renames_file(Connection $connection)
  138. {
  139. $connection
  140. ->quoteIdentifier(Argument::any())
  141. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  142. $connection
  143. ->update(
  144. 'someTableName',
  145. array(
  146. '"key"' => 'newFile',
  147. ),
  148. array(
  149. '"key"' => 'filename'
  150. ))
  151. ->shouldBeCalled()
  152. ->willReturn(1);
  153. $this->rename('filename', 'newFile')->shouldReturn(true);
  154. }
  155. /**
  156. * @param \Doctrine\DBAL\Connection $connection
  157. * @param \Doctrine\DBAL\Statement $stmt
  158. */
  159. function it_get_keys(Connection $connection, Statement $stmt)
  160. {
  161. $stmt->fetchAll(\PDO::FETCH_COLUMN)->willReturn(array('filename', 'filename1', 'filename2'));
  162. $connection
  163. ->quoteIdentifier(Argument::any())
  164. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  165. $connection
  166. ->executeQuery('SELECT "key" FROM "someTableName"')
  167. ->willReturn($stmt);
  168. $this->keys()->shouldReturn(array('filename', 'filename1', 'filename2'));
  169. }
  170. /**
  171. * @param \Doctrine\DBAL\Connection $connection
  172. */
  173. function it_deletes_file(Connection $connection)
  174. {
  175. $connection
  176. ->quoteIdentifier(Argument::any())
  177. ->will(function ($argument) { return sprintf('"%s"', $argument[0]); });
  178. $connection
  179. ->delete('someTableName', array('"key"' => 'filename'))
  180. ->shouldBeCalled()
  181. ->willReturn(1);
  182. $this->delete('filename')->shouldReturn(true);
  183. }
  184. }