Skip to content

Commit

Permalink
Update use of deprecated MockBuilder::setMethods().
Browse files Browse the repository at this point in the history
As a result TestCase::getMockModel() no longer accepts null or non existent methods
for it's $methods argument.
  • Loading branch information
ADmad committed Aug 2, 2019
1 parent c7c8cc9 commit 2b47b1f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/TestSuite/TestCase.php
Expand Up @@ -753,12 +753,12 @@ protected function skipUnless($condition, $message = '')
* Mock a model, maintain fixtures and table association
*
* @param string $alias The model to get a mock for.
* @param array|null $methods The list of methods to mock
* @param array $methods The list of methods to mock
* @param array $options The config data for the mock's constructor.
* @throws \Cake\ORM\Exception\MissingTableClassException
* @return \Cake\ORM\Table|\PHPUnit\Framework\MockObject\MockObject
*/
public function getMockForModel(string $alias, ?array $methods = [], array $options = [])

This comment has been minimized.

Copy link
@mirko-pagliai

mirko-pagliai Aug 7, 2019

Contributor

Please, this was something we had already discussed #12720

This comment has been minimized.

Copy link
@ADmad

ADmad Aug 7, 2019

Author Member

This change is forced by change in phpunit's API. setMethods() is deprecated and new method doesn't take null as argument. Instead the new onlyMethods() behaves similarly when passing it an empty array.

This comment has been minimized.

Copy link
@mirko-pagliai

mirko-pagliai Aug 7, 2019

Contributor

ok

public function getMockForModel(string $alias, array $methods = [], array $options = [])
{
$className = $this->_getTableClassName($alias, $options);
$connectionName = $className::defaultConnectionName();
Expand All @@ -772,7 +772,7 @@ public function getMockForModel(string $alias, ?array $methods = [], array $opti

/** @var \Cake\ORM\Table|\PHPUnit\Framework\MockObject\MockObject $mock */
$mock = $this->getMockBuilder($className)
->setMethods($methods)
->onlyMethods($methods)
->setConstructorArgs([$options])
->getMock();

Expand Down
21 changes: 4 additions & 17 deletions tests/TestCase/TestSuite/TestCaseTest.php
Expand Up @@ -359,12 +359,10 @@ public function testGetMockForModel()
->will($this->returnValue('mocked'));
$this->assertSame('mocked', $Posts->save($entity));
$this->assertSame('Cake\ORM\Entity', $Posts->getEntityClass());

$Posts = $this->getMockForModel('Posts', ['doSomething']);
$this->assertInstanceOf('Cake\Database\Connection', $Posts->getConnection());
$this->assertSame('test', $Posts->getConnection()->configName());

$Tags = $this->getMockForModel('Tags', ['doSomething']);
$Tags = $this->getMockForModel('Tags', ['save']);
$this->assertSame('TestApp\Model\Entity\Tag', $Tags->getEntityClass());
}

Expand Down Expand Up @@ -411,7 +409,7 @@ public function testGetMockForModelWithPlugin()
$this->assertTrue($TestPluginComment->save($entity));
$this->assertFalse($TestPluginComment->save($entity));

$TestPluginAuthors = $this->getMockForModel('TestPlugin.Authors', ['doSomething']);
$TestPluginAuthors = $this->getMockForModel('TestPlugin.Authors', ['save']);
$this->assertInstanceOf('TestPlugin\Model\Table\AuthorsTable', $TestPluginAuthors);
$this->assertSame('TestPlugin\Model\Entity\Author', $TestPluginAuthors->getEntityClass());
$this->clearPlugins();
Expand Down Expand Up @@ -453,17 +451,6 @@ public function testGetMockForModelTable()
$result = $this->getTableLocator()->get('Comments');
$this->assertInstanceOf(Table::class, $result);
$this->assertEmpty([], $allMethodsStubs->getAlias());

$allMethodsMocks = $this->getMockForModel(
'Table',
null,
['alias' => 'Comments', 'className' => Table::class]
);
$result = $this->getTableLocator()->get('Comments');
$this->assertInstanceOf(Table::class, $result);
$this->assertSame('Comments', $allMethodsMocks->getAlias());

$this->assertNotEquals($allMethodsStubs, $allMethodsMocks);
}

/**
Expand All @@ -475,10 +462,10 @@ public function testGetMockForModelSetTable()
{
static::setAppNamespace();

$I18n = $this->getMockForModel('I18n', ['doSomething']);
$I18n = $this->getMockForModel('I18n', ['save']);
$this->assertSame('custom_i18n_table', $I18n->getTable());

$Tags = $this->getMockForModel('Tags', ['doSomething']);
$Tags = $this->getMockForModel('Tags', ['save']);
$this->assertSame('tags', $Tags->getTable());
}
}

0 comments on commit 2b47b1f

Please sign in to comment.