/tests/units/classes/writers/file.php

https://github.com/tharkun/atoum · PHP · 184 lines · 174 code · 10 blank · 0 comment · 0 complexity · df173f6bbc00d7c6374a2f3dab4a8150 MD5 · raw file

  1. <?php
  2. namespace mageekguy\atoum\tests\units\writers;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\writers\file as testedClass
  6. ;
  7. require_once __DIR__ . '/../../runner.php';
  8. class file extends atoum\test
  9. {
  10. public function testClass()
  11. {
  12. $this
  13. ->testedClass
  14. ->implements('mageekguy\atoum\report\writers\realtime')
  15. ->implements('mageekguy\atoum\report\writers\asynchronous')
  16. ;
  17. }
  18. public function testClassConstants()
  19. {
  20. $this
  21. ->string(testedClass::defaultFileName)->isEqualTo('atoum.log')
  22. ;
  23. }
  24. public function test__construct()
  25. {
  26. $this
  27. ->if($file = new testedClass())
  28. ->then
  29. ->string($file->getFilename())->isEqualTo('atoum.log')
  30. ->object($file->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  31. ->if($file = new testedClass(null, $adapter = new atoum\test\adapter()))
  32. ->then
  33. ->object($file->getAdapter())->isIdenticalTo($adapter)
  34. ->string($file->getFilename())->isEqualTo('atoum.log')
  35. ->adapter($file->getAdapter())->call('fopen')->never()
  36. ->if($file = new testedClass($filename = uniqid()))
  37. ->then
  38. ->string($file->getFilename())->isEqualTo($filename)
  39. ->object($file->getAdapter())->isInstanceOf('mageekguy\atoum\adapter')
  40. ;
  41. }
  42. public function test__destruct()
  43. {
  44. $this
  45. ->if($adapter = new atoum\test\adapter())
  46. ->and($adapter->fopen = $resource = uniqid())
  47. ->and($adapter->flock = true)
  48. ->and($adapter->ftruncate = true)
  49. ->and($adapter->fwrite = function($resource, $data) { return strlen($data); })
  50. ->and($adapter->fflush = function() {})
  51. ->and($adapter->fclose = function() {})
  52. ->and($file = new testedClass(null, $adapter))
  53. ->when(function() use ($file) { $file->__destruct(); })
  54. ->then
  55. ->adapter($adapter)
  56. ->call('fclose')->never()
  57. ->if($file = new testedClass(null, $adapter))
  58. ->and($file->write('something'))
  59. ->when(function() use ($file) { $file->__destruct(); })
  60. ->then
  61. ->adapter($adapter)
  62. ->call('fclose')->withArguments($resource)->once()
  63. ;
  64. }
  65. public function testSetFilename()
  66. {
  67. $this
  68. ->if($adapter = new atoum\test\adapter())
  69. ->and($adapter->fopen = $resource = uniqid())
  70. ->and($adapter->flock = true)
  71. ->and($adapter->ftruncate = true)
  72. ->and($adapter->fflush = function() {})
  73. ->and($adapter->fclose = function() {})
  74. ->and($file = new testedClass(null, $adapter))
  75. ->then
  76. ->object($file->setFilename($filename = uniqid()))->isIdenticalTo($file)
  77. ->string($file->getFilename())->isEqualTo($filename)
  78. ->then
  79. ->object($file->setFilename())->isIdenticalTo($file)
  80. ->string($file->getFilename())->isEqualTo(testedClass::defaultFileName)
  81. ->if($adapter->fwrite = function($resource, $data) { return strlen($data); })
  82. ->and($obj = $file->write($string = uniqid()))
  83. ->and($file->setFilename('anotherNameAgain'))
  84. ->then
  85. ->string($file->getFilename())->isEqualTo('anotherNameAgain')
  86. ->adapter($adapter)
  87. ->afterFunctionCall('flock')->withArguments($resource, LOCK_UN)
  88. ->call('fclose')->withArguments($resource)->once()
  89. ;
  90. }
  91. public function testWrite()
  92. {
  93. $this
  94. ->if($adapter = new atoum\test\adapter())
  95. ->if($adapter->fopen = function() { trigger_error(uniqid()); return false; })
  96. ->and($file = new testedClass(null, $adapter))
  97. ->and($adapter->resetCalls())
  98. ->then
  99. ->exception(function() use ($file) { $file->write(uniqid()); })
  100. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  101. ->hasMessage('Unable to open file \'' . $file->getFilename() . '\'')
  102. ->error->notExists()
  103. ->if($adapter->fopen = $resource = uniqid())
  104. ->and($adapter->flock = false)
  105. ->and($adapter->resetCalls())
  106. ->then
  107. ->exception(function() use ($file) { $file->write(uniqid()); })
  108. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  109. ->hasMessage('Unable to lock file \'' . $file->getFilename() . '\'')
  110. ->if($file = new testedClass(null, $adapter))
  111. ->and($adapter->flock = true)
  112. ->and($adapter->ftruncate = true)
  113. ->and($adapter->fclose = function() {})
  114. ->and($adapter->fwrite = false)
  115. ->and($adapter->fflush = function() {})
  116. ->and($adapter->resetCalls())
  117. ->then
  118. ->exception(function() use ($file) { $file->write(uniqid()); })
  119. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  120. ->hasMessage('Unable to write in file \'' . $file->getFilename() . '\'')
  121. ->adapter($adapter)
  122. ->call('fopen')->withArguments($file->getFilename(), 'c')->once()
  123. ->call('flock')->withArguments($resource, LOCK_EX)->once()
  124. ->if($adapter->fwrite = function($resource, $data) { return strlen($data); })
  125. ->and($file = new testedClass(null, $adapter))
  126. ->and($adapter->resetCalls())
  127. ->then
  128. ->object($file->write($string = uniqid()))->isIdenticalTo($file)
  129. ->adapter($adapter)
  130. ->call('fopen')->withArguments($file->getFilename(), 'c')->once()
  131. ->call('flock')->withArguments($resource, LOCK_EX)->once()
  132. ->call('fwrite')->withArguments($resource, $string)->once()
  133. ->call('fflush')->withArguments($resource)->once()
  134. ->object($file->write($string = (uniqid() . "\n")))->isIdenticalTo($file)
  135. ->adapter($adapter)
  136. ->call('fopen')->withArguments($file->getFilename(), 'c')->once()
  137. ->afterFunctionCall('flock')->withArguments($resource, LOCK_EX)
  138. ->call('fwrite')->withArguments($resource, $string)->once()
  139. ->call('fflush')->withArguments($resource)->twice()
  140. ;
  141. }
  142. public function testClear()
  143. {
  144. $this
  145. ->if($adapter = new atoum\test\adapter())
  146. ->if($adapter->fopen = false)
  147. ->and($file = new testedClass(null, $adapter))
  148. ->and($adapter->resetCalls())
  149. ->then
  150. ->exception(function() use ($file) { $file->clear(); })
  151. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  152. ->hasMessage('Unable to open file \'' . $file->getFilename() . '\'')
  153. ->if($adapter->fopen = $resource = uniqid())
  154. ->and($adapter->flock = true)
  155. ->and($adapter->ftruncate = false)
  156. ->and($adapter->fclose = function() {})
  157. ->and($adapter->resetCalls())
  158. ->then
  159. ->exception(function() use ($file) { $file->clear(); })
  160. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  161. ->hasMessage('Unable to truncate file \'' . $file->getFilename() . '\'')
  162. ->adapter($adapter)
  163. ->call('fopen')->withArguments($file->getFilename(), 'c')->once()
  164. ->call('flock')->withArguments($resource, LOCK_EX)->once()
  165. ->if($adapter->ftruncate = true)
  166. ->then
  167. ->object($file->clear())->isIdenticalTo($file)
  168. ->adapter($adapter)
  169. ->call('fopen')->withArguments($file->getFilename(), 'c')->once()
  170. ->afterFunctionCall('flock')->withArguments($resource, LOCK_EX)
  171. ->call('ftruncate')->withArguments($resource, 0)->twice()
  172. ;
  173. }
  174. }