Skip to content

Commit 50e6306

Browse files
committed
Merge branch '3.x'
2 parents e03978a + 2faf742 commit 50e6306

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

config/bootstrap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
46
*
@@ -42,11 +44,11 @@
4244
* This function returns the same variable that was passed.
4345
* Only runs if debug mode is enabled.
4446
*
45-
* @param Query $query Query to show SQL statements for.
47+
* @param \Cake\Database\Query $query Query to show SQL statements for.
4648
* @param bool $showValues Renders the SQL statement with bound variables.
4749
* @param bool|null $showHtml If set to true, the method prints the debug
4850
* data in a browser-friendly way.
49-
* @return Query
51+
* @return \Cake\Database\Query
5052
*/
5153
function sql(Query $query, $showValues = true, $showHtml = null)
5254
{
@@ -61,7 +63,7 @@ function sql(Query $query, $showValues = true, $showHtml = null)
6163
* Only runs if debug mode is enabled.
6264
* It will otherwise just continue code execution and ignore this function.
6365
*
64-
* @param Query $query Query to show SQL statements for.
66+
* @param \Cake\Database\Query $query Query to show SQL statements for.
6567
* @param bool $showValues Renders the SQL statement with bound variables.
6668
* @param bool|null $showHtml If set to true, the method prints the debug
6769
* data in a browser-friendly way.

src/Model/Table/RequestsTable.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public function findRecent(Query $query, array $options)
7676
->limit(10);
7777
}
7878

79+
/**
80+
* Check if garbage collection should be run
81+
*
82+
* @return bool
83+
*/
84+
protected function shouldGc()
85+
{
86+
return time() % 100 === 0;
87+
}
88+
7989
/**
8090
* Garbage collect old request data.
8191
*
@@ -87,13 +97,16 @@ public function findRecent(Query $query, array $options)
8797
*/
8898
public function gc()
8999
{
90-
if (time() % 100 !== 0) {
100+
if (!$this->shouldGc()) {
91101
return;
92102
}
93103
$noPurge = $this->find()
94104
->select(['id'])
105+
->enableHydration(false)
95106
->order(['requested_at' => 'desc'])
96-
->limit(Configure::read('DebugKit.requestCount') ?: 20);
107+
->limit(Configure::read('DebugKit.requestCount') ?: 20)
108+
->extract('id')
109+
->toArray();
97110

98111
$query = $this->Panels->query()
99112
->delete()

tests/TestCase/Model/Table/RequestTableTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
namespace DebugKit\Test\TestCase\Model\Table;
1717

18+
use Cake\Core\Configure;
1819
use Cake\Database\Driver\Sqlite;
1920
use Cake\Datasource\ConnectionManager;
2021
use Cake\ORM\TableRegistry;
@@ -73,4 +74,35 @@ public function testFindRecent()
7374
$this->assertSame(10, $query->clause('limit'));
7475
$this->assertNotEmpty($query->clause('order'));
7576
}
77+
78+
/**
79+
* Test the garbage collect.
80+
*
81+
* @return void
82+
*/
83+
public function testGc()
84+
{
85+
/** @var \PHPUnit\Framework\MockObject\MockObject&\DebugKit\Model\Table\RequestsTable $requestsTableMock */
86+
$requestsTableMock = $this->getMockForModel('DebugKit.Requests', ['shouldGc']);
87+
$requestsTableMock->method('shouldGc')
88+
->will($this->returnValue(true));
89+
90+
$data = array_fill(0, 10, [
91+
'url' => '/tasks/add',
92+
'content_type' => 'text/html',
93+
'status_code' => 200,
94+
'requested_at' => '2014-08-21 7:41:12',
95+
]);
96+
$requests = $requestsTableMock->newEntities($data);
97+
$this->assertNotFalse($requestsTableMock->saveMany($requests));
98+
99+
$count = $requestsTableMock->find()->count();
100+
$this->assertGreaterThanOrEqual(10, $count);
101+
102+
Configure::write('DebugKit.requestCount', 5);
103+
$requestsTableMock->gc();
104+
105+
$count = $requestsTableMock->find()->count();
106+
$this->assertSame(5, $count);
107+
}
76108
}

0 commit comments

Comments
 (0)