Skip to content

Commit 2815435

Browse files
authored
Merge pull request #350 from bakewizard/fix-deprecation
Fixed deprecation warning in CakePHP 5.2
1 parent 6d6ad24 commit 2815435

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

src/Controller/Component/SearchComponent.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace Search\Controller\Component;
55

66
use Cake\Controller\Component;
7-
use Cake\Http\Response;
7+
use Cake\Event\EventInterface;
88
use Cake\Utility\Hash;
99
use Closure;
1010
use UnexpectedValueException;
@@ -65,12 +65,13 @@ public function implementedEvents(): array
6565
* Checks if the current request has posted data and redirects the users
6666
* to the same action after converting the post data into GET params
6767
*
68-
* @return \Cake\Http\Response|null
68+
* @param \Cake\Event\EventInterface $event Event instance
69+
* @return void
6970
*/
70-
public function startup(): ?Response
71+
public function startup(EventInterface $event): void
7172
{
7273
if (!$this->getController()->getRequest()->is('post') || !$this->_isSearchAction()) {
73-
return null;
74+
return;
7475
}
7576

7677
$url = $this->getController()->getRequest()->getPath();
@@ -81,7 +82,7 @@ public function startup(): ?Response
8182
$url .= '?' . http_build_query($params);
8283
}
8384

84-
return $this->_registry->getController()->redirect($url);
85+
$event->setResult($this->_registry->getController()->redirect($url));
8586
}
8687

8788
/**

tests/TestCase/Controller/Component/SearchComponentTest.php

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace Search\Test\TestCase\Controller\Component;
55

66
use Cake\Controller\Controller;
7+
use Cake\Event\Event;
8+
use Cake\Http\Response;
79
use Cake\Http\ServerRequest;
810
use Cake\ORM\Table;
911
use Cake\Routing\RouteBuilder;
@@ -63,26 +65,36 @@ public function testInitializePost()
6365
->withEnv('REQUEST_METHOD', 'POST');
6466

6567
$this->Controller->setRequest($request);
66-
67-
$response = $this->Search->startup();
68+
$event = new Event('Controller.startup', $this->Controller);
69+
$this->Search->startup($event);
70+
$response = $event->getResult();
71+
$this->assertInstanceOf(Response::class, $response);
6872
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->getHeaderLine('Location'));
6973

7074
$this->Search->setConfig('actions', false);
71-
$response = $this->Search->startup();
72-
$this->assertNull($response);
75+
$event = new Event('Controller.startup', $this->Controller);
76+
$this->Search->startup($event);
77+
$this->assertNull($event->getResult());
7378

7479
$this->Search->setConfig('actions', 'does-not-exist', false);
75-
$response = $this->Search->startup();
76-
$this->assertNull($response);
80+
$event = new Event('Controller.startup', $this->Controller);
81+
$this->Search->startup($event);
82+
$this->assertNull($event->getResult());
7783

7884
$this->Search->setConfig('actions', 'index', false);
7985
$this->Controller->setResponse($this->Controller->getResponse()->withHeader('Location', ''));
80-
$response = $this->Search->startup();
86+
$event = new Event('Controller.startup', $this->Controller);
87+
$this->Search->startup($event);
88+
$response = $event->getResult();
89+
$this->assertInstanceOf(Response::class, $response);
8190
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->getHeaderLine('Location'));
8291

8392
$this->Search->setConfig('actions', ['index', 'does-not-exist'], false);
8493
$this->Controller->setResponse($this->Controller->getResponse()->withHeader('Location', ''));
85-
$response = $this->Search->startup();
94+
$event = new Event('Controller.startup', $this->Controller);
95+
$this->Search->startup($event);
96+
$response = $event->getResult();
97+
$this->assertInstanceOf(Response::class, $response);
8698
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->getHeaderLine('Location'));
8799

88100
$this->Search->setConfig('actions', true);
@@ -93,16 +105,23 @@ public function testInitializePost()
93105
'type' => 'open',
94106
'pass' => ['open'],
95107
])
96-
->withRequestTarget('/users/my-predictions');
108+
->withRequestTarget('/users/my-predictions')
109+
->withData('foo', 'bar');
97110
$this->Controller->setRequest($request);
98111

99112
$this->Controller->setResponse($this->Controller->getResponse()->withHeader('Location', ''));
100-
$response = $this->Search->startup();
113+
$event = new Event('Controller.startup', $this->Controller);
114+
$this->Search->startup($event);
115+
$response = $event->getResult();
116+
$this->assertInstanceOf(Response::class, $response);
101117
$this->assertEquals('http://localhost/users/my-predictions?foo=bar', $response->getHeaderLine('Location'));
102118

103119
$this->Controller->setRequest($this->Controller->getRequest()->withData('foo', ''));
104120
$this->Controller->setResponse($this->Controller->getResponse()->withHeader('Location', ''));
105-
$response = $this->Search->startup();
121+
$event = new Event('Controller.startup', $this->Controller);
122+
$this->Search->startup($event);
123+
$response = $event->getResult();
124+
$this->assertInstanceOf(Response::class, $response);
106125
$this->assertEquals('http://localhost/users/my-predictions', $response->getHeaderLine('Location'));
107126
}
108127

@@ -123,11 +142,15 @@ public function testInitializePostWithEmptyValues()
123142
->withEnv('REQUEST_METHOD', 'POST');
124143

125144
$this->Controller->setRequest($request);
145+
$event = new Event('Controller.startup', $this->Controller);
126146

127147
$this->Search->configShallow('emptyValues', [
128148
'checkbox' => '0',
129149
]);
130-
$response = $this->Search->startup();
150+
$this->Search->startup($event);
151+
$response = $event->getResult();
152+
153+
$this->assertInstanceOf(Response::class, $response);
131154
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->getHeaderLine('Location'));
132155
}
133156

@@ -148,13 +171,17 @@ public function testInitializePostWithEmptyValuesCallable()
148171
->withEnv('REQUEST_METHOD', 'POST');
149172

150173
$this->Controller->setRequest($request);
174+
$event = new Event('Controller.startup', $this->Controller);
151175

152176
$this->Search->configShallow('emptyValues', [
153177
'checkbox' => function ($value, array $params): bool {
154178
return $value === '0';
155179
},
156180
]);
157-
$response = $this->Search->startup();
181+
$this->Search->startup($event);
182+
$response = $event->getResult();
183+
184+
$this->assertInstanceOf(Response::class, $response);
158185
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->getHeaderLine('Location'));
159186
}
160187

@@ -179,8 +206,11 @@ public function testInitializePostWithQueryStringWhitelist()
179206
->withEnv('REQUEST_METHOD', 'POST');
180207

181208
$this->Controller->setRequest($request);
209+
$event = new Event('Controller.startup', $this->Controller);
210+
$this->Search->startup($event);
211+
$response = $event->getResult();
182212

183-
$response = $this->Search->startup();
213+
$this->assertInstanceOf(Response::class, $response);
184214
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar&sort=created&direction=desc', $response->getHeaderLine('Location'));
185215
}
186216

@@ -207,9 +237,13 @@ public function testInitializePostWithNestedQueryString()
207237
->withEnv('REQUEST_METHOD', 'POST');
208238

209239
$this->Controller->setRequest($request);
240+
$event = new Event('Controller.startup', $this->Controller);
210241

211242
$this->Search->configShallow('queryStringWhitelist', ['scope.sort', 'scope.direction']);
212-
$response = $this->Search->startup();
243+
$this->Search->startup($event);
244+
$response = $event->getResult();
245+
246+
$this->assertInstanceOf(Response::class, $response);
213247
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar&scope%5Bsort%5D=created&scope%5Bdirection%5D=desc', $response->getHeaderLine('Location'));
214248
}
215249

@@ -237,7 +271,10 @@ public function testInitializePostWithQueryStringWhitelistEmpty()
237271

238272
// Needed as config() would not do anything here due to internal default behavior of merging here
239273
$this->Search->configShallow('queryStringWhitelist', []);
240-
$response = $this->Search->startup();
274+
$event = new Event('Controller.startup', $this->Controller);
275+
$this->Search->startup($event);
276+
$response = $event->getResult();
277+
$this->assertInstanceOf(Response::class, $response);
241278
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->getHeaderLine('Location'));
242279
}
243280

@@ -257,8 +294,11 @@ public function testConversionWithRedirect()
257294
->withEnv('REQUEST_METHOD', 'POST');
258295

259296
$this->Controller->setRequest($request);
297+
$event = new Event('Controller.startup', $this->Controller);
298+
$this->Search->startup($event);
299+
$response = $event->getResult();
260300

261-
$response = $this->Search->startup();
301+
$this->assertInstanceOf(Response::class, $response);
262302
$this->assertEquals('http://localhost/Posts/index/pass?foo=bar', $response->getHeaderLine('Location'));
263303
}
264304

0 commit comments

Comments
 (0)