/dmCorePlugin/lib/test/dmCoreFunctionalCoverageTest.php

https://github.com/h16bit/diem · PHP · 191 lines · 134 code · 42 blank · 15 comment · 6 complexity · 917093d58d6a54f0e382a3dd439cb777 MD5 · raw file

  1. <?php
  2. abstract class dmCoreFunctionalCoverageTest
  3. {
  4. protected
  5. $context,
  6. $options,
  7. $browser,
  8. $stats,
  9. $counter;
  10. protected static $defaults = array(
  11. 'env' => 'test',
  12. 'debug' => false,
  13. 'login' => false,
  14. 'maxRedirections' => 5,
  15. 'validate' => true
  16. );
  17. public function __construct(array $options)
  18. {
  19. $this->options = array_merge(self::$defaults, $options);
  20. $this->configure();
  21. }
  22. protected function configure()
  23. {
  24. }
  25. abstract protected function execute();
  26. public function run()
  27. {
  28. $this->boot();
  29. $this->initBrowser();
  30. $this->initStats();
  31. if ($this->options['login'])
  32. {
  33. $this->login();
  34. }
  35. /*
  36. * Preload cache to ensure stats consistency
  37. */
  38. $this->browser->get('');
  39. $this->execute();
  40. $this->showStats();
  41. }
  42. protected function boot()
  43. {
  44. $configuration = ProjectConfiguration::getApplicationConfiguration($this->options['app'], $this->options['env'], $this->options['debug']);
  45. $this->context = dm::createContext($configuration);
  46. sfConfig::set('sf_logging_enabled', false);
  47. // remove all cache
  48. sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
  49. }
  50. protected function initBrowser()
  51. {
  52. $this->browser = $this->context->get('test_functional');
  53. if($this->options['debug'])
  54. {
  55. $this->browser->info('If allowed memory size is exhausted, try setting debug to off');
  56. }
  57. }
  58. protected function initStats()
  59. {
  60. $this->stats = array(
  61. 'nbUrls' => 0,
  62. 'maxTime' => array('url' => '?', 'value' => 0),
  63. 'maxMem' => array('url' => '?', 'value' => 0),
  64. 'totalTime' => 0,
  65. 'totalMem' => 0
  66. );
  67. }
  68. protected function login()
  69. {
  70. $user = dmDb::table('DmUser')->findOneByUsername($this->options['username']);
  71. if(!$user instanceof DmUser)
  72. {
  73. throw new dmException(sprintf(
  74. 'The "%s" user does not exist. Please provide a valid username in your functional test, or disable login',
  75. $this->options['username']
  76. ));
  77. }
  78. $this->browser->getContext()->getUser()->signin($user);
  79. // make the login persistent
  80. $this->browser->getContext()->getUser()->shutdown();
  81. $this->browser->getContext()->getStorage()->shutdown();
  82. $this->browser->with('user')->begin()->isAuthenticated()->end();
  83. }
  84. protected function testUrl($url, $expectedStatusCode = 200)
  85. {
  86. $nbRedirects = 0;
  87. $this->startCounter($url);
  88. dm::resetStartTime();
  89. $this->browser->get($url);
  90. while(in_array($this->browser->getResponse()->getStatusCode(), array(301, 302)))
  91. {
  92. $this->browser->with('response')->begin()->isRedirected()->end()->followRedirect();
  93. $nbRedirects++;
  94. if ($nbRedirects > $this->options['maxRedirections'])
  95. {
  96. $this->browser->info('Too many redirections');
  97. break;
  98. }
  99. }
  100. $this->stopCounter();
  101. $this->browser->with('response')->begin()
  102. ->isStatusCode($expectedStatusCode)
  103. ->end();
  104. if ($this->options['validate'])
  105. {
  106. $this->browser->with('response')->begin()->isValid()->end();
  107. }
  108. }
  109. protected function startCounter($url)
  110. {
  111. $this->counter = array(
  112. 'url' => $url,
  113. 'mem' => memory_get_usage(),
  114. 'time' => microtime(true)
  115. );
  116. }
  117. protected function stopCounter()
  118. {
  119. $time = sprintf('%01.3f', 1000 * (microtime(true) - $this->counter['time']));
  120. // $mem = sprintf('%01.3f', (memory_get_usage() - $this->counter['mem'])/1024);
  121. // $this->browser->info(round($time).' ms | '.round($mem).' Ko');
  122. $this->browser->info(round($time).' ms');
  123. if ($time > $this->stats['maxTime']['value'])
  124. {
  125. $this->stats['maxTime'] = array('url' => $this->counter['url'], 'value' => $time);
  126. }
  127. // if ($mem > $this->stats['maxMem']['value'])
  128. // {
  129. // $this->stats['maxMem'] = array('url' => $this->counter['url'], 'value' => $mem);
  130. // }
  131. $this->stats['totalTime'] += $time;
  132. // $this->stats['totalMem'] += $mem;
  133. $this->stats['nbUrls'] += 1;
  134. }
  135. protected function showStats()
  136. {
  137. $averageTime = $this->stats['totalTime'] / $this->stats['nbUrls'];
  138. // $averageMem = $this->stats['totalMem'] / $this->stats['nbUrls'];
  139. $this->browser->info('------------------------------------------------');
  140. $this->browser->info(sprintf('Average time : %01.3f ms', $averageTime));
  141. // $this->browser->info(sprintf('Average memory : %01.3f Ko', $averageMem));
  142. $this->browser->info(sprintf('Max time : %01.3f ms on %s', $this->stats['maxTime']['value'], $this->stats['maxTime']['url']));
  143. // $this->browser->info(sprintf('Max memory : %01.3f Ko on %s', $this->stats['maxMem']['value'], $this->stats['maxMem']['url']));
  144. }
  145. protected function willRunOutOfMemory()
  146. {
  147. return (dmString::convertBytes(ini_get('memory_limit')) - memory_get_usage()) < (5 * 1024 * 1024);
  148. }
  149. }