Skip to content

Commit

Permalink
Remove TableRegistry calls from tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Sep 21, 2017
1 parent 527855a commit afb01a0
Show file tree
Hide file tree
Showing 37 changed files with 744 additions and 781 deletions.
5 changes: 2 additions & 3 deletions tests/TestCase/Auth/BasicAuthenticateTest.php
Expand Up @@ -20,7 +20,6 @@
use Cake\Http\ServerRequest;
use Cake\I18n\Time;
use Cake\Network\Exception\UnauthorizedException;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
Expand Down Expand Up @@ -52,7 +51,7 @@ public function setUp()
]);

$password = password_hash('password', PASSWORD_BCRYPT);
$User = TableRegistry::get('Users');
$User = $this->getTableLocator()->get('Users');
$User->updateAll(['password' => $password], []);
$this->response = $this->getMockBuilder(Response::class)->getMock();
}
Expand Down Expand Up @@ -144,7 +143,7 @@ public function testAuthenticateInjection()
*/
public function testAuthenticateUsernameZero()
{
$User = TableRegistry::get('Users');
$User = $this->getTableLocator()->get('Users');
$User->updateAll(['username' => '0'], ['username' => 'mariano']);

$request = new ServerRequest('posts/index');
Expand Down
5 changes: 2 additions & 3 deletions tests/TestCase/Auth/DigestAuthenticateTest.php
Expand Up @@ -24,7 +24,6 @@
use Cake\I18n\Time;
use Cake\Network\Exception\UnauthorizedException;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
Expand Down Expand Up @@ -65,7 +64,7 @@ public function setUp()
]);

$password = DigestAuthenticate::password('mariano', 'cake', 'localhost');
$User = TableRegistry::get('Users');
$User = $this->getTableLocator()->get('Users');
$User->updateAll(['password' => $password], []);

$this->response = $this->getMockBuilder(Response::class)->getMock();
Expand Down Expand Up @@ -284,7 +283,7 @@ public function testAuthenticateSuccess()
*/
public function testAuthenticateSuccessHiddenPasswordField()
{
$User = TableRegistry::get('Users');
$User = $this->getTableLocator()->get('Users');
$User->setEntityClass(ProtectedUser::class);

$request = new ServerRequest([
Expand Down
15 changes: 7 additions & 8 deletions tests/TestCase/Auth/FormAuthenticateTest.php
Expand Up @@ -21,7 +21,6 @@
use Cake\Http\ServerRequest;
use Cake\I18n\Time;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\Utility\Security;

Expand Down Expand Up @@ -52,11 +51,11 @@ public function setUp()
]);
$password = password_hash('password', PASSWORD_DEFAULT);

TableRegistry::clear();
$Users = TableRegistry::get('Users');
$this->getTableLocator()->clear();
$Users = $this->getTableLocator()->get('Users');
$Users->updateAll(['password' => $password], []);

$AuthUsers = TableRegistry::get('AuthUsers', [
$AuthUsers = $this->getTableLocator()->get('AuthUsers', [
'className' => 'TestApp\Model\Table\AuthUsersTable'
]);
$AuthUsers->updateAll(['password' => $password], []);
Expand Down Expand Up @@ -225,7 +224,7 @@ public function testAuthenticateSuccess()
*/
public function testAuthenticateIncludesVirtualFields()
{
$users = TableRegistry::get('Users');
$users = $this->getTableLocator()->get('Users');
$users->entityClass('TestApp\Model\Entity\VirtualUser');

$request = new ServerRequest('posts/index');
Expand Down Expand Up @@ -253,7 +252,7 @@ public function testPluginModel()
{
Plugin::load('TestPlugin');

$PluginModel = TableRegistry::get('TestPlugin.AuthUsers');
$PluginModel = $this->getTableLocator()->get('TestPlugin.AuthUsers');
$user['id'] = 1;
$user['username'] = 'gwoo';
$user['password'] = password_hash(Security::salt() . 'cake', PASSWORD_BCRYPT);
Expand Down Expand Up @@ -370,7 +369,7 @@ public function testPasswordHasherSettings()
$this->assertEquals(PASSWORD_BCRYPT, $result['hashType']);

$hash = password_hash('mypass', PASSWORD_BCRYPT);
$User = TableRegistry::get('Users');
$User = $this->getTableLocator()->get('Users');
$User->updateAll(
['password' => $hash],
['username' => 'mariano']
Expand Down Expand Up @@ -437,7 +436,7 @@ public function testAuthenticateRehash()
'passwordHasher' => 'Weak'
]);
$password = $this->auth->passwordHasher()->hash('password');
TableRegistry::get('Users')->updateAll(['password' => $password], []);
$this->getTableLocator()->get('Users')->updateAll(['password' => $password], []);

$request = new ServerRequest('posts/index');
$request->data = [
Expand Down
5 changes: 2 additions & 3 deletions tests/TestCase/Controller/Component/AuthComponentTest.php
Expand Up @@ -22,7 +22,6 @@
use Cake\Event\EventManager;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\Routing\Route\InflectedRoute;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -76,7 +75,7 @@ public function setUp()
$this->Controller = new AuthTestController($request, $response);
$this->Auth = new TestAuthComponent($this->Controller->components());

$Users = TableRegistry::get('AuthUsers');
$Users = $this->getTableLocator()->get('AuthUsers');
$Users->updateAll(['password' => password_hash('cake', PASSWORD_BCRYPT)], []);
}

Expand Down Expand Up @@ -212,7 +211,7 @@ public function testIdentifyArrayAccess()
public function testAuthorizeFalse()
{
$event = new Event('Controller.startup', $this->Controller);
$Users = TableRegistry::get('Users');
$Users = $this->getTableLocator()->get('Users');
$user = $Users->find('all')->hydrate(false)->first();
$this->Controller->Auth->storage()->write($user);
$this->Controller->Auth->config('userModel', 'Users');
Expand Down
21 changes: 10 additions & 11 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -23,7 +23,6 @@
use Cake\Http\ServerRequest;
use Cake\Network\Exception\NotFoundException;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use stdClass;

Expand Down Expand Up @@ -98,7 +97,7 @@ public function setUp()
public function tearDown()
{
parent::tearDown();
TableRegistry::clear();
$this->getTableLocator()->clear();
}

/**
Expand Down Expand Up @@ -209,7 +208,7 @@ public function testPaginateCustomFinderOptions()
'finder' => ['author' => ['author_id' => 1]]
]
];
$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');

$expected = $table
->find('author', [
Expand Down Expand Up @@ -257,9 +256,9 @@ public function testPaginateCustomFinder()
public function testPaginateNestedEagerLoader()
{
$this->loadFixtures('Articles', 'Tags', 'Authors', 'ArticlesTags', 'AuthorsTags');
$articles = TableRegistry::get('Articles');
$articles = $this->getTableLocator()->get('Articles');
$articles->belongsToMany('Tags');
$tags = TableRegistry::get('Tags');
$tags = $this->getTableLocator()->get('Tags');
$tags->belongsToMany('Authors');

$articles->eventManager()->on('Model.beforeFind', function ($event, $query) {
Expand Down Expand Up @@ -719,7 +718,7 @@ public function testOutOfRangePageNumberGetsClamped()
$this->loadFixtures('Posts');
$this->request->query['page'] = 3000;

$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
try {
$this->Paginator->paginate($table);
$this->fail('No exception raised');
Expand Down Expand Up @@ -769,7 +768,7 @@ public function testOutOfVeryBigPageNumberGetsClamped()
'page' => '3000000000000000000000000',
];

$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
$this->Paginator->paginate($table);
}

Expand Down Expand Up @@ -1069,7 +1068,7 @@ public function testCheckLimit($input, $expected)
public function testPaginateMaxLimit()
{
$this->loadFixtures('Posts');
$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');

$settings = [
'maxLimit' => 100,
Expand Down Expand Up @@ -1106,7 +1105,7 @@ public function testPaginateCustomFind()
return $ids;
};

$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
$data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
$result = $table->save(new Entity($data));
$this->assertNotEmpty($result);
Expand Down Expand Up @@ -1161,7 +1160,7 @@ public function testPaginateCustomFind()
public function testPaginateCustomFindFieldsArray()
{
$this->loadFixtures('Posts');
$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
$data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
$table->save(new Entity($data));

Expand Down Expand Up @@ -1261,7 +1260,7 @@ public function testPaginateQueryWithBindValue()
$config = ConnectionManager::config('test');
$this->skipIf(strpos($config['driver'], 'Sqlserver') !== false, 'Test temporarily broken in SQLServer');
$this->loadFixtures('Posts');
$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
$query = $table->find()
->where(['PaginatorPosts.author_id BETWEEN :start AND :end'])
->bind(':start', 1)
Expand Down
5 changes: 2 additions & 3 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -21,7 +21,6 @@
use Cake\Event\Event;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use TestApp\Controller\Admin\PostsController;
Expand Down Expand Up @@ -761,7 +760,7 @@ public function testPaginate()
$this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
$this->assertCount(3, $results);

$results = $Controller->paginate(TableRegistry::get('Posts'));
$results = $Controller->paginate($this->getTableLocator()->get('Posts'));
$this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
$this->assertCount(3, $results);

Expand All @@ -771,7 +770,7 @@ public function testPaginate()
$this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false);
$this->assertNull($Controller->request->params['paging']['Posts']['scope']);

$results = $Controller->paginate(TableRegistry::get('Posts'), ['scope' => 'posts']);
$results = $Controller->paginate($this->getTableLocator()->get('Posts'), ['scope' => 'posts']);
$this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
$this->assertCount(1, $results);

Expand Down
7 changes: 3 additions & 4 deletions tests/TestCase/Database/Schema/TableTest.php
Expand Up @@ -17,7 +17,6 @@
use Cake\Database\Schema\Table;
use Cake\Database\Type;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

/**
Expand Down Expand Up @@ -56,7 +55,7 @@ public function setUp()

public function tearDown()
{
TableRegistry::clear();
$this->getTableLocator()->clear();
Type::clear();
Type::map($this->_map);
parent::tearDown();
Expand Down Expand Up @@ -575,7 +574,7 @@ public function testAddConstraintForeignKey()
*/
public function testConstraintForeignKey()
{
$table = TableRegistry::get('ArticlesTags');
$table = $this->getTableLocator()->get('ArticlesTags');
$compositeConstraint = $table->schema()->getConstraint('tag_id_fk');
$expected = [
'type' => 'foreign',
Expand All @@ -599,7 +598,7 @@ public function testConstraintForeignKey()
*/
public function testConstraintForeignKeyTwoColumns()
{
$table = TableRegistry::get('Orders');
$table = $this->getTableLocator()->get('Orders');
$compositeConstraint = $table->schema()->getConstraint('product_category_fk');
$expected = [
'type' => 'foreign',
Expand Down
21 changes: 10 additions & 11 deletions tests/TestCase/Datasource/PaginatorTest.php
Expand Up @@ -20,7 +20,6 @@
use Cake\Datasource\Exception\PageOutOfBoundsException;
use Cake\Datasource\Paginator;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

class PaginatorTest extends TestCase
Expand Down Expand Up @@ -69,7 +68,7 @@ public function setUp()
public function tearDown()
{
parent::tearDown();
TableRegistry::clear();
$this->getTableLocator()->clear();
}

/**
Expand Down Expand Up @@ -146,7 +145,7 @@ public function testPaginateCustomFinderOptions()
'finder' => ['author' => ['author_id' => 1]]
]
];
$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');

$expected = $table
->find('author', [
Expand Down Expand Up @@ -195,9 +194,9 @@ public function testPaginateCustomFinder()
public function testPaginateNestedEagerLoader()
{
$this->loadFixtures('Articles', 'Tags', 'Authors', 'ArticlesTags', 'AuthorsTags');
$articles = TableRegistry::get('Articles');
$articles = $this->getTableLocator()->get('Articles');
$articles->belongsToMany('Tags');
$tags = TableRegistry::get('Tags');
$tags = $this->getTableLocator()->get('Tags');
$tags->belongsToMany('Authors');
$articles->eventManager()->on('Model.beforeFind', function ($event, $query) {
$query ->matching('Tags', function ($q) {
Expand Down Expand Up @@ -639,7 +638,7 @@ public function testOutOfRangePageNumberGetsClamped()
$this->loadFixtures('Posts');
$params['page'] = 3000;

$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
try {
$this->Paginator->paginate($table, $params);
$this->fail('No exception raised');
Expand Down Expand Up @@ -672,7 +671,7 @@ public function testOutOfVeryBigPageNumberGetsClamped()
'page' => '3000000000000000000000000',
];

$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
$this->Paginator->paginate($table, $params);
}

Expand Down Expand Up @@ -972,7 +971,7 @@ public function testCheckLimit($input, $expected)
public function testPaginateMaxLimit()
{
$this->loadFixtures('Posts');
$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');

$settings = [
'maxLimit' => 100,
Expand Down Expand Up @@ -1011,7 +1010,7 @@ public function testPaginateCustomFind()
return $ids;
};

$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
$data = ['author_id' => 3, 'title' => 'Fourth Post', 'body' => 'Article Body, unpublished', 'published' => 'N'];
$result = $table->save(new Entity($data));
$this->assertNotEmpty($result);
Expand Down Expand Up @@ -1066,7 +1065,7 @@ public function testPaginateCustomFind()
public function testPaginateCustomFindFieldsArray()
{
$this->loadFixtures('Posts');
$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
$data = ['author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'];
$table->save(new Entity($data));

Expand Down Expand Up @@ -1166,7 +1165,7 @@ public function testPaginateQueryWithBindValue()
$config = ConnectionManager::config('test');
$this->skipIf(strpos($config['driver'], 'Sqlserver') !== false, 'Test temporarily broken in SQLServer');
$this->loadFixtures('Posts');
$table = TableRegistry::get('PaginatorPosts');
$table = $this->getTableLocator()->get('PaginatorPosts');
$query = $table->find()
->where(['PaginatorPosts.author_id BETWEEN :start AND :end'])
->bind(':start', 1)
Expand Down
3 changes: 1 addition & 2 deletions tests/TestCase/Mailer/EmailTest.php
Expand Up @@ -19,7 +19,6 @@
use Cake\Log\Log;
use Cake\Mailer\Email;
use Cake\Mailer\Transport\DebugTransport;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\View\Exception\MissingTemplateException;
use Exception;
Expand Down Expand Up @@ -2777,7 +2776,7 @@ public function testJsonSerialize()
->setMessageId('<uuid@server.com>')
->setDomain('foo.bar')
->setViewVars([
'users' => TableRegistry::get('Users')->get(1, ['fields' => ['id', 'username']]),
'users' => $this->getTableLocator()->get('Users')->get(1, ['fields' => ['id', 'username']]),
'xml' => new SimpleXmlElement($xmlstr),
'exception' => new Exception('test')
])
Expand Down

0 comments on commit afb01a0

Please sign in to comment.