DoctrineDbalSpec.php 6.4 KB

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