From 5c0d095441062d4a37561975ba11681e30946076 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sat, 31 Aug 2024 20:06:50 +0200 Subject: [PATCH] 5.next: unmockify the testsuite for form, http and mailer tests (#17878) unmockify the testsuite for form, http and mailer tests --- tests/TestCase/Form/FormTest.php | 13 +-- .../TestCase/Http/Client/Auth/DigestTest.php | 20 +++-- tests/TestCase/Http/ServerTest.php | 79 ++++++++++++++----- .../MailerDefaultProfileRestorationTest.php | 33 ++++---- .../TestCase/Mailer/MailerSendActionTest.php | 32 ++++---- .../Mailer/MailerSendFailsEmailIsReset.php | 38 ++++----- ...hUnsetTemplateDefaultsToActionNameTest.php | 37 ++++----- tests/TestCase/Mailer/MailerTest.php | 18 +++-- 8 files changed, 155 insertions(+), 115 deletions(-) diff --git a/tests/TestCase/Form/FormTest.php b/tests/TestCase/Form/FormTest.php index 99ec85a7e17..55c8a0cd817 100644 --- a/tests/TestCase/Form/FormTest.php +++ b/tests/TestCase/Form/FormTest.php @@ -20,6 +20,7 @@ use Cake\Form\Schema; use Cake\TestSuite\TestCase; use Cake\Validation\Validator; +use Exception; use TestApp\Form\AppForm; use TestApp\Form\FormSchema; @@ -164,16 +165,18 @@ public function testSetErrors(): void */ public function testExecuteInvalid(): void { - $form = $this->getMockBuilder(Form::class) - ->onlyMethods(['_execute']) - ->getMock(); + $form = new class extends Form { + // phpcs:ignore CakePHP.NamingConventions.ValidFunctionName.PublicWithUnderscore + public function _execute(array $data): bool + { + throw new Exception('Should not be called'); + } + }; $form->getValidator() ->add('email', 'format', ['rule' => 'email']); $data = [ 'email' => 'rong', ]; - $form->expects($this->never()) - ->method('_execute'); $this->assertFalse($form->execute($data)); } diff --git a/tests/TestCase/Http/Client/Auth/DigestTest.php b/tests/TestCase/Http/Client/Auth/DigestTest.php index 05df2361cc4..5185b4e6d99 100644 --- a/tests/TestCase/Http/Client/Auth/DigestTest.php +++ b/tests/TestCase/Http/Client/Auth/DigestTest.php @@ -34,7 +34,7 @@ class DigestTest extends TestCase protected $client; /** - * @var \PHPUnit\Framework\MockObject\MockObject|\Cake\Http\Client\Auth\Digest + * @var \Cake\Http\Client\Auth\Digest */ protected $auth; @@ -50,19 +50,17 @@ public function setUp(): void } /** - * @return Digest|\PHPUnit\Framework\MockObject\MockObject + * @return Digest */ protected function getDigestMock() { - $digest = $this->getMockBuilder(Digest::class) - ->onlyMethods(['generateCnonce']) - ->setConstructorArgs([$this->client]) - ->getMock(); - $digest->expects($this->any()) - ->method('generateCnonce') - ->willReturn('cnonce'); - - return $digest; + return new class ($this->client) extends Digest + { + public function generateCnonce(): string + { + return 'cnonce'; + } + }; } /** diff --git a/tests/TestCase/Http/ServerTest.php b/tests/TestCase/Http/ServerTest.php index fb0ed3e9695..dc15b8498ea 100644 --- a/tests/TestCase/Http/ServerTest.php +++ b/tests/TestCase/Http/ServerTest.php @@ -33,6 +33,7 @@ use Laminas\Diactoros\ServerRequest as LaminasServerRequest; use Mockery; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; use TestApp\Http\MiddlewareApplication; require_once __DIR__ . '/server_mocks.php'; @@ -84,14 +85,14 @@ public function tearDown(): void */ public function testAppGetSet(): void { - /** @var \Cake\Http\BaseApplication|\PHPUnit\Framework\MockObject\MockObject $app */ - $app = $this->getMockBuilder(BaseApplication::class) - ->setConstructorArgs([$this->config]) - ->getMock(); - - $manager = new EventManager(); - $app->method('getEventManager') - ->willReturn($manager); + $eventManager = new EventManager(); + $app = new class ($this->config, $eventManager) extends BaseApplication + { + public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue + { + return $middlewareQueue; + } + }; $server = new Server($app); $this->assertSame($app, $server->getApp()); @@ -319,8 +320,21 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue */ public function testGetEventManagerNonEventedApplication(): void { - /** @var \Cake\Core\HttpApplicationInterface|\PHPUnit\Framework\MockObject\MockObject $app */ - $app = $this->createMock(HttpApplicationInterface::class); + $app = new class implements HttpApplicationInterface { + public function bootstrap(): void + { + } + + public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue + { + return $middlewareQueue; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + return new Response(); + } + }; $server = new Server($app); $this->assertSame(EventManager::instance(), $server->getEventManager()); @@ -331,8 +345,21 @@ public function testGetEventManagerNonEventedApplication(): void */ public function testSetEventManagerNonEventedApplication(): void { - /** @var \Cake\Core\HttpApplicationInterface|\PHPUnit\Framework\MockObject\MockObject $app */ - $app = $this->createMock(HttpApplicationInterface::class); + $app = new class implements HttpApplicationInterface { + public function bootstrap(): void + { + } + + public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue + { + return $middlewareQueue; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + return new Response(); + } + }; $events = new EventManager(); $server = new Server($app); @@ -347,8 +374,21 @@ public function testSetEventManagerNonEventedApplication(): void */ public function testAppWithoutContainerApplicationInterface(): void { - /** @var \Cake\Core\HttpApplicationInterface|\PHPUnit\Framework\MockObject\MockObject $app */ - $app = $this->createMock(HttpApplicationInterface::class); + $app = new class implements HttpApplicationInterface { + public function bootstrap(): void + { + } + + public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue + { + return $middlewareQueue; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + return new Response(); + } + }; $server = new Server($app); $request = new ServerRequest(); @@ -372,11 +412,12 @@ function ($event, $request, $response) use (&$triggered): void { } ); - $emitter = $this->getMockBuilder(ResponseEmitter::class)->getMock(); - $emitter->expects($this->once()) - ->method('emit') - ->willReturn(true); - + $emitter = new class extends ResponseEmitter { + public function emit(ResponseInterface $response, $stream = null): bool + { + return true; + } + }; $server->emit(new Response(), $emitter); $this->assertTrue($triggered); diff --git a/tests/TestCase/Mailer/MailerDefaultProfileRestorationTest.php b/tests/TestCase/Mailer/MailerDefaultProfileRestorationTest.php index 347e0b7f6a3..55c871d2d09 100644 --- a/tests/TestCase/Mailer/MailerDefaultProfileRestorationTest.php +++ b/tests/TestCase/Mailer/MailerDefaultProfileRestorationTest.php @@ -21,27 +21,22 @@ class MailerDefaultProfileRestorationTest extends TestCase { public function testSendAction(): void { - $mailer = $this->getMockBuilder(DefaultProfileRestorationMailer::class) - ->onlyMethods(['deliver', 'test']) - ->setConstructorArgs([['template' => 'cakephp']]) - ->getMock(); - $mailer->expects($this->once()) - ->method('test') - ->with('foo', 'bar'); - $mailer->expects($this->once()) - ->method('deliver') - ->willReturn([]); + $mailer = new class (['template' => 'cakephp']) extends Mailer { + public bool $testIsCalled = false; + + public function test($to, $subject) + { + $this->testIsCalled = true; + } + + public function deliver(string $content = ''): array + { + return []; + } + }; $mailer->send('test', ['foo', 'bar']); $this->assertSame('cakephp', $mailer->viewBuilder()->getTemplate()); + $this->assertTrue($mailer->testIsCalled); } } - -// phpcs:disable -class DefaultProfileRestorationMailer extends Mailer -{ - public function test($to, $subject) - { - } -} -// phpcs:enable diff --git a/tests/TestCase/Mailer/MailerSendActionTest.php b/tests/TestCase/Mailer/MailerSendActionTest.php index ef64e304806..1f8f6a36db3 100644 --- a/tests/TestCase/Mailer/MailerSendActionTest.php +++ b/tests/TestCase/Mailer/MailerSendActionTest.php @@ -21,27 +21,23 @@ class MailerSendActionTest extends TestCase { public function testSendAction(): void { - $mailer = $this->getMockBuilder(SendActionMailer::class) - ->onlyMethods(['deliver', 'test']) - ->getMock(); - $mailer->expects($this->once()) - ->method('test') - ->with('foo', 'bar'); - $mailer->expects($this->any()) - ->method('deliver') - ->willReturn([]); + $mailer = new class extends Mailer { + public bool $testIsCalled = false; + + public function test($to, $subject) + { + $this->testIsCalled = true; + } + + public function deliver(string $content = ''): array + { + return []; + } + }; $mailer->send('test', ['foo', 'bar']); $this->assertNull($mailer->viewBuilder()->getTemplate()); + $this->assertTrue($mailer->testIsCalled); } } - -// phpcs:disable -class SendActionMailer extends Mailer -{ - public function test($to, $subject) - { - } -} -// phpcs:enable diff --git a/tests/TestCase/Mailer/MailerSendFailsEmailIsReset.php b/tests/TestCase/Mailer/MailerSendFailsEmailIsReset.php index eca801ef459..2430f6c018a 100644 --- a/tests/TestCase/Mailer/MailerSendFailsEmailIsReset.php +++ b/tests/TestCase/Mailer/MailerSendFailsEmailIsReset.php @@ -22,31 +22,31 @@ class MailerSendFailsEmailIsReset extends TestCase { public function testSendAction(): void { - $mailer = $this->getMockBuilder(SendFailsEmailIsResetMailer::class) - ->onlyMethods(['restore', 'deliver', 'welcome']) - ->getMock(); + $mailer = new class extends Mailer { + public bool $restoreIsCalled = false; - $mailer->expects($this->once()) - ->method('deliver') - ->will($this->throwException(new RuntimeException('kaboom'))); - // Mailer should be reset even if sending fails. - $mailer->expects($this->once()) - ->method('restore'); + public function welcome() + { + } + + public function deliver(string $content = ''): array + { + throw new RuntimeException('kaboom'); + } + + protected function restore() + { + $this->restoreIsCalled = true; + + return $this; + } + }; try { $mailer->send('welcome', ['foo', 'bar']); $this->fail('Exception should bubble up.'); } catch (RuntimeException) { - $this->assertTrue(true, 'Exception was raised'); + $this->assertTrue($mailer->restoreIsCalled, 'Exception was raised'); } } } - -// phpcs:disable -class SendFailsEmailIsResetMailer extends Mailer -{ - public function welcome() - { - } -} -// phpcs:enable diff --git a/tests/TestCase/Mailer/MailerSendWithUnsetTemplateDefaultsToActionNameTest.php b/tests/TestCase/Mailer/MailerSendWithUnsetTemplateDefaultsToActionNameTest.php index 5346dc52f16..7011e24203f 100644 --- a/tests/TestCase/Mailer/MailerSendWithUnsetTemplateDefaultsToActionNameTest.php +++ b/tests/TestCase/Mailer/MailerSendWithUnsetTemplateDefaultsToActionNameTest.php @@ -21,26 +21,27 @@ class MailerSendWithUnsetTemplateDefaultsToActionNameTest extends TestCase { public function testSendAction(): void { - $mailer = $this->getMockBuilder(SendWithUnsetTemplateDefaultsToActionNameMailer::class) - ->onlyMethods(['deliver', 'restore', 'test']) - ->getMock(); - $mailer->expects($this->once()) - ->method('test') - ->with('foo', 'bar'); - $mailer->expects($this->any()) - ->method('deliver') - ->willReturn([]); + $mailer = new class extends Mailer { + public bool $testIsCalled = false; + + public function test($to, $subject) + { + $this->testIsCalled = true; + } + + public function deliver(string $content = ''): array + { + return []; + } + + protected function restore() + { + return $this; + } + }; $mailer->send('test', ['foo', 'bar']); $this->assertSame('test', $mailer->viewBuilder()->getTemplate()); + $this->assertTrue($mailer->testIsCalled); } } - -// phpcs:disable -class SendWithUnsetTemplateDefaultsToActionNameMailer extends Mailer -{ - public function test($to, $subject) - { - } -} -// phpcs:enable diff --git a/tests/TestCase/Mailer/MailerTest.php b/tests/TestCase/Mailer/MailerTest.php index 1a2f55f8684..421055e7f18 100644 --- a/tests/TestCase/Mailer/MailerTest.php +++ b/tests/TestCase/Mailer/MailerTest.php @@ -88,7 +88,8 @@ public function testTransport(): void $result = $this->mailer->getTransport(); $this->assertInstanceOf(DebugTransport::class, $result); - $instance = $this->getMockBuilder(DebugTransport::class)->getMock(); + $instance = new class extends DebugTransport { + }; $this->mailer->setTransport($instance); $this->assertSame($instance, $this->mailer->getTransport()); } @@ -311,21 +312,26 @@ public function testUseConfigString(): void } /** - * CakeEmailTest::testMockTransport() + * CakeEmailTest::testDefaultTransport() */ - public function testMockTransport(): void + public function testDefaultTransport(): void { TransportFactory::drop('default'); - $mock = $this->getMockBuilder(AbstractTransport::class)->getMock(); + $instance = new class extends AbstractTransport { + public function send(Message $message): array + { + return []; + } + }; $config = ['from' => 'tester@example.org', 'transport' => 'default']; Mailer::setConfig('default', $config); - TransportFactory::setConfig('default', $mock); + TransportFactory::setConfig('default', $instance); $em = new Mailer('default'); - $this->assertSame($mock, $em->getTransport()); + $this->assertSame($instance, $em->getTransport()); TransportFactory::drop('default'); }