PageRenderTime 100ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/mock/stream.php

http://github.com/mageekguy/atoum
PHP | 106 lines | 97 code | 9 blank | 0 comment | 1 complexity | 3d99fbafdb5c76183137bb24aca0f489 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\mock;
  3. use
  4. mageekguy\atoum\test,
  5. mageekguy\atoum\adapter,
  6. mageekguy\atoum\mock\stream as testedClass
  7. ;
  8. require_once __DIR__ . '/../../runner.php';
  9. class stream extends test
  10. {
  11. public function testClassConstants()
  12. {
  13. $this
  14. ->string(testedClass::defaultProtocol)->isEqualTo('atoum')
  15. ->string(testedClass::protocolSeparator)->isEqualTo('://')
  16. ;
  17. }
  18. public function testGetAdapter()
  19. {
  20. $this
  21. ->object(testedClass::getAdapter())->isEqualTo(new adapter())
  22. ->if(testedClass::setAdapter($adapter = new adapter()))
  23. ->then
  24. ->object(testedClass::getAdapter())->isIdenticalTo($adapter)
  25. ;
  26. }
  27. public function testGet()
  28. {
  29. $this
  30. ->if(testedClass::setAdapter($adapter = new test\adapter()))
  31. ->and($adapter->stream_get_wrappers = array())
  32. ->and($adapter->stream_wrapper_register = true)
  33. ->then
  34. ->object($streamController = testedClass::get($stream = uniqid()))->isInstanceOf('mageekguy\atoum\mock\stream\controller')
  35. ->string($streamController->getPath())->isEqualTo(testedClass::defaultProtocol . '://' . testedClass::setDirectorySeparator($stream))
  36. ->adapter($adapter)
  37. ->call('stream_wrapper_register')->withArguments(testedClass::defaultProtocol, 'mageekguy\atoum\mock\stream')->once()
  38. ->if($adapter->stream_get_wrappers = array(testedClass::defaultProtocol))
  39. ->then
  40. ->object($streamController = testedClass::get())->isInstanceOf('mageekguy\atoum\mock\stream\controller')
  41. ->string($streamController->getPath())->matches('#^' . testedClass::defaultProtocol . '://\w+$#')
  42. ->adapter($adapter)
  43. ->call('stream_wrapper_register')->withArguments(testedClass::defaultProtocol, 'mageekguy\atoum\mock\stream')->once()
  44. ->object(testedClass::get($stream))->isIdenticalTo($streamController = testedClass::get($stream))
  45. ->adapter($adapter)
  46. ->call('stream_wrapper_register')->withArguments(testedClass::defaultProtocol, 'mageekguy\atoum\mock\stream')->once()
  47. ->object(testedClass::get($otherStream = ($protocol = uniqid()) . '://' . uniqid()))->isNotIdenticalTo($streamController)
  48. ->adapter($adapter)
  49. ->call('stream_wrapper_register')->withArguments($protocol, 'mageekguy\atoum\mock\stream')->once()
  50. ->if($adapter->stream_get_wrappers = array(testedClass::defaultProtocol, $protocol))
  51. ->then
  52. ->object(testedClass::get($otherStream))->isIdenticalTo(testedClass::get($otherStream))
  53. ->object(testedClass::get($otherStream))->isIdenticalTo(testedClass::get($otherStream))
  54. ->adapter($adapter)
  55. ->call('stream_wrapper_register')->withArguments($protocol, 'mageekguy\atoum\mock\stream')->once()
  56. ->if($adapter->stream_get_wrappers = array())
  57. ->and($adapter->stream_wrapper_register = false)
  58. ->then
  59. ->exception(function() use ($protocol) { testedClass::get($protocol . '://' . uniqid()); })
  60. ->isInstanceOf('mageekguy\atoum\exceptions\runtime')
  61. ->hasMessage('Unable to register ' . $protocol . ' stream')
  62. ;
  63. }
  64. public function testGetSubStream()
  65. {
  66. $this
  67. ->if(testedClass::setAdapter($adapter = new test\adapter()))
  68. ->and($adapter->stream_get_wrappers = array())
  69. ->and($adapter->stream_wrapper_register = true)
  70. ->and($stream = testedClass::get())
  71. ->then
  72. ->string($stream . '\\' . uniqid())->matches('#^' . $stream . preg_quote('\\') . '[^' . preg_quote('\\') . ']+$#')
  73. ->object($subStream = testedClass::getSubStream($stream))->isInstanceOf('mageekguy\atoum\mock\stream\controller')
  74. ->castToString($subStream)->matches('#^' . $stream . preg_quote(DIRECTORY_SEPARATOR) . '[^' . preg_quote(DIRECTORY_SEPARATOR) . ']+$#')
  75. ->object($subStream = testedClass::getSubStream($stream, $basename = uniqid()))->isInstanceOf('mageekguy\atoum\mock\stream\controller')
  76. ->castToString($subStream)->matches('#^' . $stream . preg_quote(DIRECTORY_SEPARATOR) . $basename . '$#')
  77. ;
  78. }
  79. public function testGetProtocol()
  80. {
  81. $this
  82. ->variable(testedClass::getProtocol(uniqid()))->isNull()
  83. ->string(testedClass::getProtocol(($scheme = uniqid()) . '://' . uniqid()))->isEqualTo($scheme)
  84. ;
  85. }
  86. public function testSetDirectorySeparator()
  87. {
  88. $this
  89. ->string(testedClass::setDirectorySeparator('foo/bar', '/'))->isEqualTo('foo/bar')
  90. ->string(testedClass::setDirectorySeparator('foo\bar', '/'))->isEqualTo('foo/bar')
  91. ->string(testedClass::setDirectorySeparator('foo/bar', '\\'))->isEqualTo('foo\bar')
  92. ->string(testedClass::setDirectorySeparator('foo\bar', '\\'))->isEqualTo('foo\bar')
  93. ->string(testedClass::setDirectorySeparator('foo' . DIRECTORY_SEPARATOR . 'bar'))->isEqualTo('foo' . DIRECTORY_SEPARATOR . 'bar')
  94. ->string(testedClass::setDirectorySeparator('foo' . (DIRECTORY_SEPARATOR == '/' ? '\\' : '/') . 'bar'))->isEqualTo('foo' . DIRECTORY_SEPARATOR . 'bar')
  95. ;
  96. }
  97. }