bug55701.phpt 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. --TEST--
  2. Bug #55701 (GlobIterator throws LogicException with message 'The parent constructor was not called')
  3. --FILE--
  4. <?php
  5. //
  6. // Some methods of GlobIterator do not throw a RuntimeException when the glob pattern doesn't match any file.
  7. // Most methods of GlobIterator throw a RuntimeException when the glob pattern doesn't match any file
  8. // because they get the properties of the current file
  9. function testBaseClass($f) {
  10. // The tested iterator is in an invalid state; the behaviour of most of its methods is undefined
  11. try {
  12. $f();
  13. echo "ran normally (expected)\n";
  14. } catch (RuntimeException $e) {
  15. // Throwing a RuntimeException is the correct behaviour for some methods
  16. echo "ran normally (expected)\n";
  17. } catch (\Error $e) {
  18. // Throwing a LogicException is not correct
  19. echo "threw LogicException (unexpected)\n";
  20. }
  21. }
  22. //
  23. // The derived classes must throw LogicException if the parent class constructor was not called
  24. function testChildClass($f) {
  25. try {
  26. $f();
  27. echo "didn't throw (unexpected)\n";
  28. } catch (\Error $e) {
  29. echo "threw Error (expected)\n";
  30. } catch (Exception $e) {
  31. echo "threw other exception (unexpected)\n";
  32. }
  33. }
  34. //
  35. // It must not throw LogicException when the iterator is not valid
  36. echo "->count()... ";
  37. testBaseClass( function() {
  38. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  39. $o->count();
  40. } );
  41. echo "->rewind()... ";
  42. testBaseClass( function() {
  43. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  44. $o->rewind();
  45. } );
  46. echo "->getFlags()... ";
  47. testBaseClass( function() {
  48. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  49. $o->getFlags();
  50. } );
  51. echo "->setFlags()... ";
  52. testBaseClass( function() {
  53. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  54. $o->setFlags(FilesystemIterator::KEY_AS_PATHNAME);
  55. } );
  56. echo "->valid()... ";
  57. testBaseClass( function() {
  58. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  59. $o->valid();
  60. } );
  61. //
  62. // When the iterator is not valid, the behaviour of the next methods is undefined
  63. // Some of them throw a RuntimeException while others just return an invalid value
  64. // However, they must not throw LogicException
  65. echo "->current()... ";
  66. testBaseClass( function() {
  67. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  68. $o->current();
  69. } );
  70. echo "->key()... ";
  71. testBaseClass( function() {
  72. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  73. $o->key();
  74. } );
  75. echo "->next()... ";
  76. testBaseClass( function() {
  77. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  78. $o->next();
  79. } );
  80. echo "->getATime()... ";
  81. testBaseClass( function() {
  82. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  83. $o->getATime();
  84. } );
  85. echo "->getBasename()... ";
  86. testBaseClass( function() {
  87. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  88. $o->getBasename();
  89. } );
  90. echo "->getCTime()... ";
  91. testBaseClass( function() {
  92. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  93. $o->getCTime();
  94. } );
  95. echo "->getExtension()... ";
  96. testBaseClass( function() {
  97. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  98. $o->getExtension();
  99. } );
  100. echo "->getFilename()... ";
  101. testBaseClass( function() {
  102. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  103. $o->getFilename();
  104. } );
  105. echo "->getGroup()... ";
  106. testBaseClass( function() {
  107. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  108. $o->getGroup();
  109. } );
  110. echo "->getInode()... ";
  111. testBaseClass( function() {
  112. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  113. $o->getInode();
  114. } );
  115. echo "->getMTime()... ";
  116. testBaseClass( function() {
  117. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  118. $o->getMTime();
  119. } );
  120. echo "->getOwner()... ";
  121. testBaseClass( function() {
  122. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  123. $o->getOwner();
  124. } );
  125. echo "->getPath()... ";
  126. testBaseClass( function() {
  127. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  128. $o->getPath();
  129. } );
  130. echo "->getPathname()... ";
  131. testBaseClass( function() {
  132. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  133. $o->getPathname();
  134. } );
  135. echo "->getPerms()... ";
  136. testBaseClass( function() {
  137. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  138. $o->getPerms();
  139. } );
  140. echo "->getSize()... ";
  141. testBaseClass( function() {
  142. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  143. $o->getSize();
  144. } );
  145. echo "->getType()... ";
  146. testBaseClass( function() {
  147. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  148. $o->getType();
  149. } );
  150. echo "->isDir()... ";
  151. testBaseClass( function() {
  152. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  153. $o->isDir();
  154. } );
  155. echo "->isDot()... ";
  156. testBaseClass( function() {
  157. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  158. $o->isDot();
  159. } );
  160. echo "->isExecutable()... ";
  161. testBaseClass( function() {
  162. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  163. $o->isExecutable();
  164. } );
  165. echo "->isFile()... ";
  166. testBaseClass( function() {
  167. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  168. $o->isFile();
  169. } );
  170. echo "->isLink()... ";
  171. testBaseClass( function() {
  172. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  173. $o->isLink();
  174. } );
  175. echo "->isReadable()... ";
  176. testBaseClass( function() {
  177. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  178. $o->isReadable();
  179. } );
  180. echo "->isWritable()... ";
  181. testBaseClass( function() {
  182. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  183. $o->isWritable();
  184. } );
  185. echo "->seek()... ";
  186. testBaseClass( function() {
  187. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  188. $o->seek(0);
  189. } );
  190. echo "->__toString()... ";
  191. testBaseClass( function() {
  192. $o = new GlobIterator(__DIR__.'/*.abcdefghij');
  193. $o->__toString();
  194. } );
  195. //
  196. // Supplemental test: no method should throw LogicException if it is invoked
  197. // after a successful iteration over a non-empty list of files.
  198. echo "non-empty GlobIterator... ";
  199. testBaseClass( function() {
  200. $o = new GlobIterator(__DIR__.'/*.phpt');
  201. foreach ($o as $file) {
  202. // nothing here, just consume all the items
  203. }
  204. // This must not throw an exception
  205. $o->count();
  206. } );
  207. //
  208. // The correct existing behaviour must not change
  209. // The classes SplFileObject and SplTempFileObject are not affected by the bug
  210. echo "======================= test there are no regressions =======================\n";
  211. echo "SplFileObject existent file... ";
  212. testBaseClass( function() {
  213. $o = new SplFileObject(__FILE__);
  214. $o->fread(1);
  215. } );
  216. echo "SplFileObject non-existent file... ";
  217. testBaseClass( function() {
  218. $o = new SplFileObject('/tmp/abcdefghij.abcdefghij');
  219. $o->fread(1);
  220. } );
  221. //
  222. // Check that when derived classes do not call GlobIterator::__construct()
  223. // the LogicException is thrown (don't break the behaviour introduced to fix bug #54384)
  224. echo "extends GlobIterator... ";
  225. class GlobIteratorChild extends GlobIterator {
  226. public function __construct() {}
  227. }
  228. testChildClass( function() {
  229. $o = new GlobIteratorChild();
  230. $o->count();
  231. } );
  232. echo "extends SplFileObject... ";
  233. class SplFileObjectChild extends SplFileObject {
  234. public function __construct() {}
  235. }
  236. testChildClass( function() {
  237. $o = new SplFileObjectChild();
  238. $o->count();
  239. } );
  240. echo "extends SplTempFileObject... ";
  241. class SplTempFileObjectChild extends SplTempFileObject {
  242. public function __construct() {}
  243. }
  244. testChildClass( function() {
  245. $o = new SplTempFileObjectChild();
  246. $o->count();
  247. } );
  248. ?>
  249. --EXPECT--
  250. ->count()... ran normally (expected)
  251. ->rewind()... ran normally (expected)
  252. ->getFlags()... ran normally (expected)
  253. ->setFlags()... ran normally (expected)
  254. ->valid()... ran normally (expected)
  255. ->current()... ran normally (expected)
  256. ->key()... ran normally (expected)
  257. ->next()... ran normally (expected)
  258. ->getATime()... ran normally (expected)
  259. ->getBasename()... ran normally (expected)
  260. ->getCTime()... ran normally (expected)
  261. ->getExtension()... ran normally (expected)
  262. ->getFilename()... ran normally (expected)
  263. ->getGroup()... ran normally (expected)
  264. ->getInode()... ran normally (expected)
  265. ->getMTime()... ran normally (expected)
  266. ->getOwner()... ran normally (expected)
  267. ->getPath()... ran normally (expected)
  268. ->getPathname()... ran normally (expected)
  269. ->getPerms()... ran normally (expected)
  270. ->getSize()... ran normally (expected)
  271. ->getType()... ran normally (expected)
  272. ->isDir()... ran normally (expected)
  273. ->isDot()... ran normally (expected)
  274. ->isExecutable()... ran normally (expected)
  275. ->isFile()... ran normally (expected)
  276. ->isLink()... ran normally (expected)
  277. ->isReadable()... ran normally (expected)
  278. ->isWritable()... ran normally (expected)
  279. ->seek()... ran normally (expected)
  280. ->__toString()... ran normally (expected)
  281. non-empty GlobIterator... ran normally (expected)
  282. ======================= test there are no regressions =======================
  283. SplFileObject existent file... ran normally (expected)
  284. SplFileObject non-existent file... ran normally (expected)
  285. extends GlobIterator... threw Error (expected)
  286. extends SplFileObject... threw Error (expected)
  287. extends SplTempFileObject... threw Error (expected)