/tests/units/classes/script/configurable.php

https://github.com/agallou/atoum · PHP · 166 lines · 156 code · 10 blank · 0 comment · 0 complexity · 1f8a7086da1c35a13f6c81804717bb6b MD5 · raw file

  1. <?php
  2. namespace mageekguy\atoum\tests\units\script;
  3. use
  4. mageekguy\atoum,
  5. mock\mageekguy\atoum\script\configurable as testedClass
  6. ;
  7. require_once __DIR__ . '/../../runner.php';
  8. class configurable extends atoum\test
  9. {
  10. public function testClass()
  11. {
  12. $this->testedClass
  13. ->isAbstract()
  14. ->extends('mageekguy\atoum\script')
  15. ;
  16. }
  17. public function testClassConstants()
  18. {
  19. $this->string(testedClass::defaultConfigFile)->isEqualTo('.config.php');
  20. }
  21. public function test__construct()
  22. {
  23. $this
  24. ->if($configurable = new testedClass($name = uniqid()))
  25. ->then
  26. ->string($configurable->getName())->isEqualTo($name)
  27. ->object($configurable->getAdapter())->isEqualTo(new atoum\adapter())
  28. ->object($configurable->getIncluder())->isInstanceOf('mageekguy\atoum\includer')
  29. ->array($configurable->getConfigFiles())->isEmpty()
  30. ->array($configurable->getHelp())->isEqualTo(array(
  31. array(
  32. array('-h', '--help'),
  33. null,
  34. 'Display this help'
  35. ),
  36. array(
  37. array('-c', '--configurations'),
  38. '<file>...',
  39. 'Use all configuration files <file>'
  40. )
  41. )
  42. )
  43. ->if($configurable = new testedClass($name = uniqid(), $adapter = new atoum\adapter()))
  44. ->then
  45. ->string($configurable->getName())->isEqualTo($name)
  46. ->object($configurable->getAdapter())->isIdenticalTo($adapter)
  47. ->object($configurable->getIncluder())->isInstanceOf('mageekguy\atoum\includer')
  48. ->array($configurable->getConfigFiles())->isEmpty()
  49. ->array($configurable->getHelp())->isEqualTo(array(
  50. array(
  51. array('-h', '--help'),
  52. null,
  53. 'Display this help'
  54. ),
  55. array(
  56. array('-c', '--configurations'),
  57. '<file>...',
  58. 'Use all configuration files <file>'
  59. )
  60. )
  61. )
  62. ;
  63. }
  64. public function testSetIncluder()
  65. {
  66. $this
  67. ->if($configurable = new testedClass(uniqid()))
  68. ->then
  69. ->object($configurable->setIncluder($includer = new atoum\includer()))->isIdenticalTo($configurable)
  70. ->object($configurable->getIncluder())->isIdenticalTo($includer)
  71. ->object($configurable->setIncluder())->isIdenticalTo($configurable)
  72. ->object($configurable->getIncluder())
  73. ->isEqualTo(new atoum\includer())
  74. ->isNotIdenticalTo($includer)
  75. ;
  76. }
  77. public function testUseConfigFile()
  78. {
  79. $this
  80. ->if($configurable = new testedClass(uniqid()))
  81. ->then
  82. ->exception(function() use ($configurable, & $file) { $configurable->useConfigFile($file = uniqid()); })
  83. ->isInstanceOf('mageekguy\atoum\includer\exception')
  84. ->hasMessage('Unable to find configuration file \'' . $file . '\'')
  85. ->if($includer = new \mock\mageekguy\atoum\includer())
  86. ->and($this->calling($includer)->includePath = function() {})
  87. ->and($configurable->setIncluder($includer))
  88. ->then
  89. ->object($configurable->useConfigFile($file = uniqid()))->isIdenticalTo($configurable)
  90. ->mock($includer)->call('includePath')->withArguments($file)->once()
  91. ->array($configurable->getConfigFiles())->isEqualTo(array($file))
  92. ;
  93. }
  94. public function testUseDefaultConfigFiles()
  95. {
  96. $this
  97. ->if($configurable = new testedClass(uniqid()))
  98. ->and($this->calling($configurable)->useConfigFile = function() {})
  99. ->then
  100. ->object($configurable->useDefaultConfigFiles(atoum\directory))->isIdenticalTo($configurable)
  101. ->mock($configurable)
  102. ->foreach(testedClass::getSubDirectoryPath(atoum\directory), function($mock, $path) {
  103. $mock->call('useConfigFile')->withArguments($path . testedClass::defaultConfigFile)->atLeastOnce();
  104. }
  105. )
  106. ->if($configurable = new testedClass(($directory = uniqid() . DIRECTORY_SEPARATOR . uniqid() . DIRECTORY_SEPARATOR . uniqid()) . DIRECTORY_SEPARATOR . uniqid()))
  107. ->and($this->calling($configurable)->useConfigFile = function() {})
  108. ->and($configurable->setAdapter($adapter = new atoum\test\adapter()))
  109. ->and($adapter->is_dir = true)
  110. ->then
  111. ->object($configurable->useDefaultConfigFiles())->isIdenticalTo($configurable)
  112. ->mock($configurable)
  113. ->foreach(testedClass::getSubDirectoryPath($directory), function($mock, $path) {
  114. $mock->call('useConfigFile')->withArguments($path . testedClass::defaultConfigFile)->atLeastOnce();
  115. }
  116. )
  117. ->if($adapter->is_dir = false)
  118. ->and($adapter->getcwd = $workingDirectory = uniqid() . DIRECTORY_SEPARATOR . uniqid() . DIRECTORY_SEPARATOR . uniqid())
  119. ->then
  120. ->object($configurable->useDefaultConfigFiles())->isIdenticalTo($configurable)
  121. ->mock($configurable)
  122. ->foreach(testedClass::getSubDirectoryPath($workingDirectory), function($mock, $path) {
  123. $mock->call('useConfigFile')->withArguments($path . testedClass::defaultConfigFile)->atLeastOnce();
  124. }
  125. )
  126. ->if($adapter->is_dir = true)
  127. ->and($adapter->getcwd = $otherWorkingDirectory = uniqid() . DIRECTORY_SEPARATOR . uniqid() . DIRECTORY_SEPARATOR . uniqid())
  128. ->and($this->calling($configurable)->useConfigFile->throw = new atoum\includer\exception())
  129. ->then
  130. ->object($configurable->useDefaultConfigFiles(uniqid()))->isIdenticalTo($configurable)
  131. ->mock($configurable)
  132. ->foreach(testedClass::getSubDirectoryPath($otherWorkingDirectory), function($mock, $path) {
  133. $mock->call('useConfigFile')->withArguments($path . testedClass::defaultConfigFile)->atLeastOnce();
  134. }
  135. )
  136. ;
  137. }
  138. public function testGetSubDirectoryPath()
  139. {
  140. $this
  141. ->array(testedClass::getSubDirectoryPath(''))->isEmpty()
  142. ->array(testedClass::getSubDirectoryPath('', '/'))->isEmpty()
  143. ->array(testedClass::getSubDirectoryPath('', '\\'))->isEmpty()
  144. ->array(testedClass::getSubDirectoryPath('/', '/'))->isEqualTo(array('/'))
  145. ->array(testedClass::getSubDirectoryPath('/foo', '/'))->isEqualTo(array('/', '/foo/'))
  146. ->array(testedClass::getSubDirectoryPath('/foo/', '/'))->isEqualTo(array('/', '/foo/'))
  147. ->array(testedClass::getSubDirectoryPath('/foo/bar', '/'))->isEqualTo(array('/', '/foo/', '/foo/bar/'))
  148. ->array(testedClass::getSubDirectoryPath('/foo/bar/', '/'))->isEqualTo(array('/', '/foo/', '/foo/bar/'))
  149. ->array(testedClass::getSubDirectoryPath('c:\\', '\\'))->isEqualTo(array('c:\\'))
  150. ->array(testedClass::getSubDirectoryPath('c:\foo', '\\'))->isEqualTo(array('c:\\', 'c:\foo\\'))
  151. ->array(testedClass::getSubDirectoryPath('c:\foo\\', '\\'))->isEqualTo(array('c:\\', 'c:\foo\\'))
  152. ->array(testedClass::getSubDirectoryPath('c:\foo\bar', '\\'))->isEqualTo(array('c:\\', 'c:\foo\\', 'c:\foo\bar\\'))
  153. ->array(testedClass::getSubDirectoryPath('c:\foo\bar\\', '\\'))->isEqualTo(array('c:\\', 'c:\foo\\', 'c:\foo\bar\\'))
  154. ;
  155. }
  156. }