Skip to content

Commit

Permalink
Always use consistent class usage to make comparison and detection/re…
Browse files Browse the repository at this point in the history
…factor possible.
  • Loading branch information
dereuromark committed Feb 20, 2018
1 parent a52a2ac commit 660f964
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/Database/Type.php
Expand Up @@ -59,10 +59,10 @@ class Type implements TypeInterface
* @deprecated 3.1 All types will now use a specific class
*/
protected static $_basicTypes = [
'string' => ['callback' => ['\Cake\Database\Type', 'strval']],
'text' => ['callback' => ['\Cake\Database\Type', 'strval']],
'string' => ['callback' => [Type::class, 'strval']],
'text' => ['callback' => [Type::class, 'strval']],
'boolean' => [
'callback' => ['\Cake\Database\Type', 'boolval'],
'callback' => [Type::class, 'boolval'],
'pdo' => PDO::PARAM_BOOL
],
];
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/BelongsToMany.php
Expand Up @@ -1430,7 +1430,7 @@ protected function _junctionTableName($name = null)
{
if ($name === null) {
if (empty($this->_junctionTableName)) {
$tablesNames = array_map('\Cake\Utility\Inflector::underscore', [
$tablesNames = array_map('Cake\Utility\Inflector::underscore', [
$this->getSource()->getTable(),
$this->getTarget()->getTable()
]);
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/Table.php
Expand Up @@ -763,7 +763,7 @@ public function displayField($key = null)
public function getEntityClass()
{
if (!$this->_entityClass) {
$default = '\Cake\ORM\Entity';
$default = Entity::class;
$self = get_called_class();
$parts = explode('\\', $self);

Expand All @@ -772,7 +772,7 @@ public function getEntityClass()
}

$alias = Inflector::singularize(substr(array_pop($parts), 0, -5));
$name = implode('\\', array_slice($parts, 0, -1)) . '\Entity\\' . $alias;
$name = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $alias;
if (!class_exists($name)) {
return $this->_entityClass = $default;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Error/missing_action.ctp
Expand Up @@ -22,7 +22,7 @@ if (!empty($plugin)) {
}
$prefixNs = '';
if (!empty($prefix)) {
$prefix = array_map('\Cake\Utility\Inflector::camelize', explode('/', $prefix));
$prefix = array_map('Cake\Utility\Inflector::camelize', explode('/', $prefix));
$prefixNs = '\\' . implode('\\', $prefix);
$prefix = implode(DIRECTORY_SEPARATOR, $prefix) . DIRECTORY_SEPARATOR;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Error/missing_controller.ctp
Expand Up @@ -26,7 +26,7 @@ $originalClass = $class;
$class = Inflector::camelize($class);

if (!empty($prefix)) {
$prefix = array_map('\Cake\Utility\Inflector::camelize', explode('/', $prefix));
$prefix = array_map('Cake\Utility\Inflector::camelize', explode('/', $prefix));
$prefixNs = '\\' . implode('\\', $prefix);
$prefixPath = implode(DIRECTORY_SEPARATOR, $prefix) . DIRECTORY_SEPARATOR;
}
Expand Down
48 changes: 32 additions & 16 deletions src/TestSuite/TestCase.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Event\EventManager;
use Cake\ORM\Entity;
use Cake\ORM\Exception\MissingTableClassException;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\Routing\Router;
Expand Down Expand Up @@ -679,34 +680,27 @@ protected function skipUnless($condition, $message = '')
*/
public function getMockForModel($alias, array $methods = [], array $options = [])
{
$locator = $this->getTableLocator();

if (empty($options['className'])) {
$class = Inflector::camelize($alias);
$className = App::className($class, 'Model/Table', 'Table');
if (!$className) {
throw new MissingTableClassException([$alias]);
}
$options['className'] = $className;
}

$connectionName = $options['className']::defaultConnectionName();
/** @var \Cake\ORM\Table $className */
$className = $this->_getTableClassName($alias, $options);
$connectionName = $className::defaultConnectionName();
$connection = ConnectionManager::get($connectionName);

$locator = $this->getTableLocator();

list(, $baseClass) = pluginSplit($alias);
$options += ['alias' => $baseClass, 'connection' => $connection];
$options += $locator->getConfig($alias);

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

if (empty($options['entityClass']) && $mock->getEntityClass() === '\Cake\ORM\Entity') {
$parts = explode('\\', $options['className']);
if (empty($options['entityClass']) && $mock->getEntityClass() === Entity::class) {
$parts = explode('\\', $className);
$entityAlias = Inflector::singularize(substr(array_pop($parts), 0, -5));
$entityClass = implode('\\', array_slice($parts, 0, -1)) . '\Entity\\' . $entityAlias;
$entityClass = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $entityAlias;
if (class_exists($entityClass)) {
$mock->setEntityClass($entityClass);
}
Expand All @@ -722,6 +716,28 @@ public function getMockForModel($alias, array $methods = [], array $options = []
return $mock;
}

/**
* Gets the class name for the table.
*
* @param string $alias The model to get a mock for.
* @param array $options The config data for the mock's constructor.
* @return string
* @throws \Cake\ORM\Exception\MissingTableClassException
*/
protected function _getTableClassName($alias, array $options)
{
if (empty($options['className'])) {
$class = Inflector::camelize($alias);
$className = App::className($class, 'Model/Table', 'Table');
if (!$className) {
throw new MissingTableClassException([$alias]);
}
$options['className'] = $className;
}

return $options['className'];
}

/**
* Set the app namespace
*
Expand Down
6 changes: 3 additions & 3 deletions src/Validation/RulesProvider.php
Expand Up @@ -26,7 +26,7 @@ class RulesProvider
/**
* The class/object to proxy.
*
* @var mixed
* @var string|object
*/
protected $_class;

Expand All @@ -40,9 +40,9 @@ class RulesProvider
/**
* Constructor, sets the default class to use for calling methods
*
* @param string $class the default class to proxy
* @param string|object $class the default class to proxy
*/
public function __construct($class = '\Cake\Validation\Validation')
public function __construct($class = Validation::class)
{
$this->_class = $class;
$this->_reflection = new ReflectionClass($class);
Expand Down
2 changes: 1 addition & 1 deletion src/Validation/ValidatorAwareTrait.php
Expand Up @@ -44,7 +44,7 @@ trait ValidatorAwareTrait
*
* @var string
*/
protected $_validatorClass = '\Cake\Validation\Validator';
protected $_validatorClass = Validator::class;

/**
* A list of validation objects indexed by name
Expand Down
8 changes: 5 additions & 3 deletions tests/TestCase/Event/EventDispatcherTraitTest.php
Expand Up @@ -14,6 +14,8 @@

namespace Cake\Test\TestCase\Event;

use Cake\Event\Event;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventManager;
use Cake\TestSuite\TestCase;

Expand All @@ -23,7 +25,7 @@
class EventDispatcherTraitTest extends TestCase
{
/**
* @var EventDispatcherTrait
* @var \Cake\Event\EventDispatcherTrait
*/
public $subject;

Expand All @@ -36,7 +38,7 @@ public function setUp()
{
parent::setUp();

$this->subject = $this->getObjectForTrait('Cake\Event\EventDispatcherTrait');
$this->subject = $this->getObjectForTrait(EventDispatcherTrait::class);
}

/**
Expand Down Expand Up @@ -98,7 +100,7 @@ public function testDispatchEvent()
{
$event = $this->subject->dispatchEvent('some.event', ['foo' => 'bar']);

$this->assertInstanceOf('Cake\Event\Event', $event);
$this->assertInstanceOf(Event::class, $event);
$this->assertSame($this->subject, $event->getSubject());
$this->assertEquals('some.event', $event->getName());
$this->assertEquals(['foo' => 'bar'], $event->getData());
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -1482,7 +1482,7 @@ public function testFindListWithAssociatedTable()
public function testEntityClassDefault()
{
$table = new Table();
$this->assertEquals('\Cake\ORM\Entity', $table->getEntityClass());
$this->assertEquals('Cake\ORM\Entity', $table->getEntityClass());
}

/**
Expand Down Expand Up @@ -5992,7 +5992,7 @@ public function testDebugInfo()
'registryAlias' => 'Foo.Articles',
'table' => 'articles',
'alias' => 'Articles',
'entityClass' => '\Cake\ORM\Entity',
'entityClass' => 'Cake\ORM\Entity',
'associations' => [],
'behaviors' => [],
'defaultConnection' => 'default',
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/TestSuite/TestCaseTest.php
Expand Up @@ -409,7 +409,7 @@ public function testGetMockForModel()
->method('save')
->will($this->returnValue('mocked'));
$this->assertEquals('mocked', $Posts->save($entity));
$this->assertEquals('\Cake\ORM\Entity', $Posts->getEntityClass());
$this->assertEquals('Cake\ORM\Entity', $Posts->getEntityClass());

$Posts = $this->getMockForModel('Posts', ['doSomething']);
$this->assertInstanceOf('Cake\Database\Connection', $Posts->getConnection());
Expand Down Expand Up @@ -450,7 +450,7 @@ public function testGetMockForModelWithPlugin()
$TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', ['save']);

$this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
$this->assertEquals('\Cake\ORM\Entity', $TestPluginComment->getEntityClass());
$this->assertEquals('Cake\ORM\Entity', $TestPluginComment->getEntityClass());
$TestPluginComment->expects($this->at(0))
->method('save')
->will($this->returnValue(true));
Expand Down

0 comments on commit 660f964

Please sign in to comment.