]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/ProfilerTest.php
Add checks & realpath() usage
[friendica.git] / tests / src / Util / ProfilerTest.php
index f242fd43c5ebc6354dc011fd207f0f941638174a..0790bc30ac3aad45da8de12d45be1bdd68e4bf0e 100644 (file)
@@ -1,7 +1,9 @@
 <?php
 
-namespace src\Util;
+namespace Friendica\Test\src\Util;
 
+use Friendica\Core\Config\Cache\ConfigCache;
+use Friendica\Core\Config\Configuration;
 use Friendica\Test\MockedTest;
 use Friendica\Util\Profiler;
 use Mockery\MockInterface;
@@ -18,7 +20,7 @@ class ProfilerTest extends MockedTest
        {
                parent::setUp();
 
-               $this->logger = \Mockery::mock('Psr\Log\LoggerInterface');
+               $this->logger = \Mockery::mock(LoggerInterface::class);
        }
 
        /**
@@ -26,7 +28,12 @@ class ProfilerTest extends MockedTest
         */
        public function testSetUp()
        {
-               $profiler = new Profiler(true, true);
+               $configCache = \Mockery::mock(ConfigCache::class);
+               $configCache->shouldReceive('get')
+                           ->withAnyArgs()
+                           ->andReturn(true)
+                           ->twice();
+               $profiler = new Profiler($configCache);
        }
 
        /**
@@ -96,7 +103,13 @@ class ProfilerTest extends MockedTest
         */
        public function testSaveTimestamp($timestamp, $name, array $functions)
        {
-               $profiler = new Profiler(true, true);
+               $configCache = \Mockery::mock(ConfigCache::class);
+               $configCache->shouldReceive('get')
+                           ->withAnyArgs()
+                           ->andReturn(true)
+                           ->twice();
+
+               $profiler = new Profiler($configCache);
 
                foreach ($functions as $function) {
                        $profiler->saveTimestamp($timestamp, $name, $function);
@@ -111,7 +124,13 @@ class ProfilerTest extends MockedTest
         */
        public function testReset($timestamp, $name, array $functions)
        {
-               $profiler = new Profiler(true, true);
+               $configCache = \Mockery::mock(ConfigCache::class);
+               $configCache->shouldReceive('get')
+                           ->withAnyArgs()
+                           ->andReturn(true)
+                           ->twice();
+
+               $profiler = new Profiler($configCache);
 
                $profiler->saveTimestamp($timestamp, $name);
                $profiler->reset();
@@ -168,7 +187,13 @@ class ProfilerTest extends MockedTest
                        ->shouldReceive('info')
                        ->once();
 
-               $profiler = new Profiler(true, true);
+               $configCache = \Mockery::mock(ConfigCache::class);
+               $configCache->shouldReceive('get')
+                           ->withAnyArgs()
+                           ->andReturn(true)
+                           ->twice();
+
+               $profiler = new Profiler($configCache);
 
                foreach ($data as $perf => $items) {
                        foreach ($items['functions'] as $function) {
@@ -177,5 +202,70 @@ class ProfilerTest extends MockedTest
                }
 
                $profiler->saveLog($this->logger, 'test');
+
+               $output = $profiler->getRendertimeString();
+
+               foreach ($data as $perf => $items) {
+                       foreach ($items['functions'] as $function) {
+                               // assert that the output contains the functions
+                               $this->assertRegExp('/' . $function . ': \d+/', $output);
+                       }
+               }
+       }
+
+       /**
+        * Test different enable and disable states of the profiler
+        */
+       public function testEnableDisable()
+       {
+               $configCache = \Mockery::mock(ConfigCache::class);
+               $configCache->shouldReceive('get')
+                           ->with('system', 'profiler')
+                           ->andReturn(true)
+                           ->once();
+               $configCache->shouldReceive('get')
+                           ->with('rendertime', 'callstack')
+                           ->andReturn(false)
+                           ->once();
+
+               $profiler = new Profiler($configCache);
+
+               $this->assertFalse($profiler->isRendertime());
+               $this->assertEmpty($profiler->getRendertimeString());
+
+               $profiler->saveTimestamp(time(), 'network', 'test1');
+
+               $config = \Mockery::mock(Configuration::class);
+               $config->shouldReceive('get')
+                           ->with('system', 'profiler')
+                           ->andReturn(false)
+                           ->once();
+               $config->shouldReceive('get')
+                           ->with('rendertime', 'callstack')
+                           ->andReturn(false)
+                           ->once();
+
+               $profiler->update($config);
+
+               $this->assertFalse($profiler->isRendertime());
+               $this->assertEmpty($profiler->getRendertimeString());
+
+               $config->shouldReceive('get')
+                      ->with('system', 'profiler')
+                      ->andReturn(true)
+                      ->once();
+               $config->shouldReceive('get')
+                      ->with('rendertime', 'callstack')
+                      ->andReturn(true)
+                      ->once();
+
+               $profiler->update($config);
+
+               $profiler->saveTimestamp(time(), 'database', 'test2');
+
+               $this->assertTrue($profiler->isRendertime());
+               $output = $profiler->getRendertimeString();
+               $this->assertRegExp('/test1: \d+/', $output);
+               $this->assertRegExp('/test2: \d+/', $output);
        }
 }