]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/ProfilerTest.php
Merge pull request #13375 from MrPetovan/bug/empty-timeline
[friendica.git] / tests / src / Util / ProfilerTest.php
index f242fd43c5ebc6354dc011fd207f0f941638174a..9ca2e664c0f0045313a747ceabddf94e5330af52 100644 (file)
@@ -1,7 +1,27 @@
 <?php
-
-namespace src\Util;
-
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Test\src\Util;
+
+use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Test\MockedTest;
 use Friendica\Util\Profiler;
 use Mockery\MockInterface;
@@ -14,11 +34,11 @@ class ProfilerTest extends MockedTest
         */
        private $logger;
 
-       protected function setUp()
+       protected function setUp(): void
        {
                parent::setUp();
 
-               $this->logger = \Mockery::mock('Psr\Log\LoggerInterface');
+               $this->logger = \Mockery::mock(LoggerInterface::class);
        }
 
        /**
@@ -26,7 +46,14 @@ class ProfilerTest extends MockedTest
         */
        public function testSetUp()
        {
-               $profiler = new Profiler(true, true);
+               $config = \Mockery::mock(IManageConfigValues::class);
+               $config->shouldReceive('get')
+                           ->withAnyArgs()
+                           ->andReturn(true)
+                           ->twice();
+               $profiler = new Profiler($config);
+
+               self::assertInstanceOf(Profiler::class, $profiler);
        }
 
        /**
@@ -71,14 +98,14 @@ class ProfilerTest extends MockedTest
                                'name' => 'rendering',
                                'functions' => ['test', 'it7'],
                        ],
-                       'parser' => [
+                       'session' => [
                                'timestamp' => time(),
-                               'name' => 'parser',
+                               'name' => 'session',
                                'functions' => ['test', 'it8'],
                        ],
                        'marktime' => [
                                'timestamp' => time(),
-                               'name' => 'parser',
+                               'name' => 'session',
                                'functions' => ['test'],
                        ],
                        // This one isn't set during reset
@@ -96,27 +123,39 @@ class ProfilerTest extends MockedTest
         */
        public function testSaveTimestamp($timestamp, $name, array $functions)
        {
-               $profiler = new Profiler(true, true);
+               $config = \Mockery::mock(IManageConfigValues::class);
+               $config->shouldReceive('get')
+                           ->withAnyArgs()
+                           ->andReturn(true)
+                           ->twice();
+
+               $profiler = new Profiler($config);
 
                foreach ($functions as $function) {
                        $profiler->saveTimestamp($timestamp, $name, $function);
                }
 
-               $this->assertGreaterThanOrEqual(0, $profiler->get($name));
+               self::assertGreaterThanOrEqual(0, $profiler->get($name));
        }
 
        /**
         * Test the Profiler reset
         * @dataProvider dataPerformance
         */
-       public function testReset($timestamp, $name, array $functions)
+       public function testReset($timestamp, $name)
        {
-               $profiler = new Profiler(true, true);
+               $config = \Mockery::mock(IManageConfigValues::class);
+               $config->shouldReceive('get')
+                           ->withAnyArgs()
+                           ->andReturn(true)
+                           ->twice();
+
+               $profiler = new Profiler($config);
 
                $profiler->saveTimestamp($timestamp, $name);
                $profiler->reset();
 
-               $this->assertEquals(0, $profiler->get($name));
+               self::assertEquals(0, $profiler->get($name));
        }
 
        public function dataBig()
@@ -168,7 +207,13 @@ class ProfilerTest extends MockedTest
                        ->shouldReceive('info')
                        ->once();
 
-               $profiler = new Profiler(true, true);
+               $config = \Mockery::mock(IManageConfigValues::class);
+               $config->shouldReceive('get')
+                           ->withAnyArgs()
+                           ->andReturn(true)
+                           ->twice();
+
+               $profiler = new Profiler($config);
 
                foreach ($data as $perf => $items) {
                        foreach ($items['functions'] as $function) {
@@ -177,5 +222,14 @@ 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
+                               self::assertMatchesRegularExpression('/' . $function . ': \d+/', $output);
+                       }
+               }
        }
 }