Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.x: Convert addMethods() to anonymous or test classes or Mockery #17246

Merged
merged 7 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"require-dev": {
"cakephp/cakephp-codesniffer": "^5.0",
"mikey179/vfsstream": "^1.6.10",
"mockery/mockery": "^1.6",
"paragonie/csp-builder": "^2.3",
"phpunit/phpunit": "^10.1.0"
},
Expand Down
2 changes: 2 additions & 0 deletions src/TestSuite/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Closure;
use Exception;
use LogicException;
use Mockery;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase as BaseTestCase;
use ReflectionClass;
Expand Down Expand Up @@ -225,6 +226,7 @@ protected function tearDown(): void
$this->getTableLocator()->clear();
$this->_configure = [];
$this->_tableLocator = null;
Mockery::close();
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/TestCase/Collection/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2606,11 +2606,17 @@ public function testLazy(): void
{
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = (new Collection($items))->lazy();
$callable = $this->getMockBuilder(stdClass::class)
->addMethods(['__invoke'])
$callable = $this->getMockBuilder(stdMock::class)
->getMock();

$callable->expects($this->never())->method('__invoke');
$collection->filter($callable)->filter($callable);
}
}

// phpcs:disable
class stdMock extends stdClass
{
public function __invoke() {}
}
// phpcs:enable
6 changes: 2 additions & 4 deletions tests/TestCase/Command/PluginAssetsCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Cake\Core\Configure;
use Cake\TestSuite\TestCase;
use Cake\Utility\Filesystem;
use Mockery;
use SplFileInfo;

/**
Expand Down Expand Up @@ -111,10 +112,7 @@ public function testSymlinkWhenTargetAlreadyExits(): void
$this->loadPlugins(['TestTheme']);

$output = new StubConsoleOutput();
$io = $this->getMockBuilder(ConsoleIo::class)
->setConstructorArgs([$output, $output, null, null])
->addMethods(['in'])
->getMock();
$io = Mockery::mock(ConsoleIo::class, [$output, $output, null, null])->makePartial();
$parser = new ConsoleOptionParser('cake example');
$parser->addArgument('name', ['required' => false]);
$parser->addOption('overwrite', ['default' => false, 'boolean' => true]);
Expand Down
10 changes: 4 additions & 6 deletions tests/TestCase/Console/CommandRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use Mockery;
use stdClass;
use TestApp\Command\AbortCommand;
use TestApp\Command\DemoCommand;
Expand Down Expand Up @@ -531,11 +532,8 @@ protected function makeAppWithCommands(array $commands): BaseApplication

protected function getMockIo(StubConsoleOutput $output): ConsoleIo
{
$io = $this->getMockBuilder(ConsoleIo::class)
->setConstructorArgs([$output, $output, null, null])
->addMethods(['in'])
->getMock();

return $io;
return Mockery::mock(ConsoleIo::class, [$output, $output, null, null])
->shouldAllowMockingMethod('in')
->makePartial();
}
}
10 changes: 3 additions & 7 deletions tests/TestCase/Console/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Cake\ORM\Locator\TableLocator;
use Cake\ORM\Table;
use Cake\TestSuite\TestCase;
use Mockery;
use TestApp\Command\AbortCommand;
use TestApp\Command\AutoLoadModelCommand;
use TestApp\Command\DemoCommand;
Expand Down Expand Up @@ -305,15 +306,10 @@ public function testExecuteCommandNonInteractive(): void

/**
* @param \Cake\Console\ConsoleOutput $output
* @return \Cake\Console\ConsoleIo|\PHPUnit\Framework\MockObject\MockObject
* @return \Cake\Console\ConsoleIo|\Mockery\MockInterface
*/
protected function getMockIo($output)
{
$io = $this->getMockBuilder(ConsoleIo::class)
->setConstructorArgs([$output, $output, null, null])
->addMethods(['in'])
->getMock();

return $io;
return Mockery::mock(ConsoleIo::class, [$output, $output, null, null])->makePartial();
}
}
6 changes: 3 additions & 3 deletions tests/TestCase/Database/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1087,10 +1087,10 @@ public function testAutomaticReconnect2(): void

$newDriver->expects($this->exactly(2))
->method('execute')
->will($this->onConsecutiveCalls(
->willReturnOnConsecutiveCalls(
$this->throwException(new Exception('server gone away')),
$this->returnValue($statement)
));
$statement
);

$res = $conn->execute('SELECT 1');
$this->assertInstanceOf(StatementInterface::class, $res);
Expand Down
14 changes: 6 additions & 8 deletions tests/TestCase/Database/Driver/SqliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Cake\Database\DriverFeatureEnum;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use Mockery;
use PDO;

/**
Expand Down Expand Up @@ -172,14 +173,11 @@ public static function schemaValueProvider(): array
*/
public function testSchemaValue($input, $expected): void
{
$mock = $this->getMockBuilder(PDO::class)
->onlyMethods(['quote'])
->addMethods(['quoteIdentifier'])
->disableOriginalConstructor()
->getMock();
$mock->expects($this->any())
->method('quote')
->willReturnCallback(function ($value) {
$mock = Mockery::mock(PDO::class)
->shouldAllowMockingMethod('quoteIdentifier')
->makePartial();
$mock->shouldReceive('quote')
->andReturnUsing(function ($value) {
return '"' . $value . '"';
});

Expand Down
12 changes: 9 additions & 3 deletions tests/TestCase/Database/Schema/MysqlSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,9 +1409,8 @@ protected function _getMockedDriver(): Driver
{
$this->_needsConnection();

$this->pdo = $this->getMockBuilder(PDO::class)
->onlyMethods(['quote', 'getAttribute'])
->addMethods(['quoteIdentifier'])
$this->pdo = $this->getMockBuilder(PDOMocked::class)
->onlyMethods(['quote', 'getAttribute', 'quoteIdentifier'])
->disableOriginalConstructor()
->getMock();
$this->pdo->expects($this->any())
Expand All @@ -1433,3 +1432,10 @@ protected function _getMockedDriver(): Driver
return $driver;
}
}

// phpcs:disable
class PDOMocked extends PDO
{
public function quoteIdentifier(): void {}
}
// phpcs:enable
7 changes: 3 additions & 4 deletions tests/TestCase/Database/Type/StringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Cake\Database\TypeFactory;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
use Mockery;
use PDO;

/**
Expand Down Expand Up @@ -62,10 +63,8 @@ public function testToPHP(): void
*/
public function testToDatabase(): void
{
$obj = $this->getMockBuilder('StdClass')
->addMethods(['__toString'])
->getMock();
$obj->method('__toString')->willReturn('toString called');
$obj = Mockery::mock('StdClass')->shouldAllowMockingMethod('__toString');
$obj->shouldReceive('__toString')->andReturn('toString called');

$this->assertNull($this->type->toDatabase(null, $this->driver));
$this->assertSame('word', $this->type->toDatabase('word', $this->driver));
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase/Datasource/Paging/PaginatorTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,6 @@ protected function _getMockFindQuery($table = null)
/** @var \Cake\ORM\Query\SelectQuery|\PHPUnit\Framework\MockObject\MockObject $query */
$query = $this->getMockBuilder(SelectQuery::class)
->onlyMethods(['all', 'count', 'applyOptions'])
->addMethods(['total'])
->disableOriginalConstructor()
->getMock();

Expand Down
7 changes: 1 addition & 6 deletions tests/TestCase/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ public function testSetGetSchema(): void
*/
public function testGetValidator(): void
{
$form = $this->getMockBuilder(Form::class)
->addMethods(['buildValidator'])
->getMock();

$form->expects($this->once())
->method('buildValidator');
$form = new Form();

$this->assertInstanceof(Validator::class, $form->getValidator());
}
Expand Down
20 changes: 8 additions & 12 deletions tests/TestCase/Http/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use InvalidArgumentException;
use Laminas\Diactoros\Response as LaminasResponse;
use Laminas\Diactoros\ServerRequest as LaminasServerRequest;
use Mockery;
use Psr\Http\Message\ResponseInterface;
use TestApp\Http\MiddlewareApplication;

Expand Down Expand Up @@ -127,18 +128,13 @@ public function testRunCallingPluginHooks(): void
$request = new ServerRequest();
$request = $request->withHeader('X-pass', 'request header');

/** @var \TestApp\Http\MiddlewareApplication|\PHPUnit\Framework\MockObject\MockObject $app */
$app = $this->getMockBuilder(MiddlewareApplication::class)
->onlyMethods(['pluginBootstrap', 'pluginMiddleware'])
->addMethods(['pluginEvents'])
->setConstructorArgs([$this->config])
->getMock();
$app->expects($this->once())
->method('pluginBootstrap');
$app->expects($this->once())
->method('pluginMiddleware')
->with($this->isInstanceOf(MiddlewareQueue::class))
->willReturnCallback(function ($middleware) {
/** @var \TestApp\Http\MiddlewareApplication|\Mockery\MockInterface $app */
$app = Mockery::mock(MiddlewareApplication::class, [$this->config])
->shouldAllowMockingMethod('pluginEvents')
->makePartial();
$app->shouldReceive('pluginBootstrap', 'pluginMiddleware')
->with(Mockery::type(MiddlewareQueue::class))
->andReturnUsing(function ($middleware) {
return $middleware;
});

Expand Down
47 changes: 47 additions & 0 deletions tests/TestCase/Mailer/MailerDefaultProfileRestorationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 5.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Mailer;

use Cake\Mailer\Mailer;
use Cake\TestSuite\TestCase;

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->send('test', ['foo', 'bar']);
$this->assertSame('cakephp', $mailer->viewBuilder()->getTemplate());
}
}

// phpcs:disable
class DefaultProfileRestorationMailer extends Mailer
{
public function test($to, $subject)
{
}
}
// phpcs:enable
47 changes: 47 additions & 0 deletions tests/TestCase/Mailer/MailerSendActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 5.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Mailer;

use Cake\Mailer\Mailer;
use Cake\TestSuite\TestCase;

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->send('test', ['foo', 'bar']);

$this->assertNull($mailer->viewBuilder()->getTemplate());
}
}

// phpcs:disable
class SendActionMailer extends Mailer
{
public function test($to, $subject)
{
}
}
// phpcs:enable
Loading