Skip to content

Commit

Permalink
5.next: unmockify the testsuite for form, http and mailer tests (cake…
Browse files Browse the repository at this point in the history
…php#17878)

unmockify the testsuite for form, http and mailer tests
  • Loading branch information
LordSimal authored Aug 31, 2024
1 parent 1d87350 commit 5c0d095
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 115 deletions.
13 changes: 8 additions & 5 deletions tests/TestCase/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
Expand Down
20 changes: 9 additions & 11 deletions tests/TestCase/Http/Client/Auth/DigestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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';
}
};
}

/**
Expand Down
79 changes: 60 additions & 19 deletions tests/TestCase/Http/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand Down
33 changes: 14 additions & 19 deletions tests/TestCase/Mailer/MailerDefaultProfileRestorationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 14 additions & 18 deletions tests/TestCase/Mailer/MailerSendActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 19 additions & 19 deletions tests/TestCase/Mailer/MailerSendFailsEmailIsReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 5c0d095

Please sign in to comment.