From 452f234730c145b54f0760c9878e297133111420 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Wed, 13 Jun 2018 00:17:32 +0200 Subject: [PATCH] Fixed deprecations and tests --- src/Meta/RequestMetadata.php | 2 +- src/Model/Behavior/AuditLogBehavior.php | 18 +-- src/Persister/DatabasePersister.php | 7 +- src/Persister/ElasticSearchPersister.php | 5 +- src/Persister/TablePersister.php | 14 +-- src/Shell/ElasticMappingShell.php | 5 +- tests/Fixture/ElasticArticlesFixture.php | 2 +- tests/Fixture/ElasticAuditsFixture.php | 2 +- tests/Fixture/ElasticAuthorsFixture.php | 2 +- tests/Fixture/ElasticTagsFixture.php | 2 +- .../TestCase/Meta/ApplicationMetadataTest.php | 2 +- tests/TestCase/Meta/RequestMetadataTest.php | 4 +- .../Model/Behavior/AuditIntegrationTest.php | 23 ++-- .../Model/Behavior/AuditLogBehaviorTest.php | 10 +- .../Persister/ElasticSearchPersisterTest.php | 36 +++--- .../TestCase/Persister/TablePersisterTest.php | 119 +++++++++--------- tests/bootstrap.php | 2 +- 17 files changed, 130 insertions(+), 125 deletions(-) diff --git a/src/Meta/RequestMetadata.php b/src/Meta/RequestMetadata.php index 2765b18..fa81a97 100644 --- a/src/Meta/RequestMetadata.php +++ b/src/Meta/RequestMetadata.php @@ -4,7 +4,7 @@ use Cake\Event\Event; use Cake\Event\EventListenerInterface; -use Cake\Network\Request; +use Cake\Http\ServerRequest as Request; /** * Event listener that is capable of enriching the audit logs diff --git a/src/Model/Behavior/AuditLogBehavior.php b/src/Model/Behavior/AuditLogBehavior.php index 997ad5e..997d220 100644 --- a/src/Model/Behavior/AuditLogBehavior.php +++ b/src/Model/Behavior/AuditLogBehavior.php @@ -96,7 +96,7 @@ public function afterSave(Event $event, EntityInterface $entity, $options) $config = $this->_config; if (empty($config['whitelist'])) { - $config['whitelist'] = $this->_table->schema()->columns(); + $config['whitelist'] = $this->_table->getSchema()->columns(); $config['whitelist'] = array_merge( $config['whitelist'], $this->getAssociationProperties(array_keys($options['associated'])) @@ -120,14 +120,14 @@ public function afterSave(Event $event, EntityInterface $entity, $options) return; } - $primary = $entity->extract((array)$this->_table->primaryKey()); + $primary = $entity->extract((array)$this->_table->getPrimaryKey()); $auditEvent = $entity->isNew() ? AuditCreateEvent::class : AuditUpdateEvent::class; $transaction = $options['_auditTransaction']; - $auditEvent = new $auditEvent($transaction, $primary, $this->_table->table(), $changed, $original); + $auditEvent = new $auditEvent($transaction, $primary, $this->_table->getTable(), $changed, $original); if (!empty($options['_sourceTable'])) { - $auditEvent->setParentSourceName($options['_sourceTable']->table()); + $auditEvent->setParentSourceName($options['_sourceTable']->getTable()); } $options['_auditQueue']->attach($entity, $auditEvent); @@ -158,7 +158,7 @@ public function afterCommit(Event $event, EntityInterface $entity, $options) } $data = $this->_table->dispatchEvent('AuditStash.beforeLog', ['logs' => $events]); - $this->persister()->logEvents($data->data['logs']); + $this->persister()->logEvents($data->getData('logs')); } /** @@ -175,9 +175,9 @@ public function afterDelete(Event $event, EntityInterface $entity, $options) return; } $transaction = $options['_auditTransaction']; - $parent = isset($options['_sourceTable']) ? $options['_sourceTable']->table() : null; - $primary = $entity->extract((array)$this->_table->primaryKey()); - $auditEvent = new AuditDeleteEvent($transaction, $primary, $this->_table->table(), $parent); + $parent = isset($options['_sourceTable']) ? $options['_sourceTable']->getTable() : null; + $primary = $entity->extract((array)$this->_table->getPrimaryKey()); + $auditEvent = new AuditDeleteEvent($transaction, $primary, $this->_table->getTable(), $parent); $options['_auditQueue']->attach($entity, $auditEvent); } @@ -217,7 +217,7 @@ protected function getAssociationProperties($associated) $result = []; foreach ($associated as $name) { - $result[] = $associations->get($name)->property(); + $result[] = $associations->get($name)->getProperty(); } return $result; diff --git a/src/Persister/DatabasePersister.php b/src/Persister/DatabasePersister.php index d95afde..8a681ee 100644 --- a/src/Persister/DatabasePersister.php +++ b/src/Persister/DatabasePersister.php @@ -1,8 +1,8 @@ $eventType === 'delete' ? null : json_encode($log->getChanged()), 'meta' => json_encode($meta) ]; - $Audit = TableRegistry::get('Audits'); + $Audit = $this->loadModel('Audits'); if (!empty($meta['user'])) { $data['user_id'] = $meta['user']; } @@ -56,4 +57,4 @@ public function logEvents(array $auditLogs) $Audit->save($record); } } -} \ No newline at end of file +} diff --git a/src/Persister/ElasticSearchPersister.php b/src/Persister/ElasticSearchPersister.php index 65c7fbc..b241a06 100644 --- a/src/Persister/ElasticSearchPersister.php +++ b/src/Persister/ElasticSearchPersister.php @@ -6,7 +6,6 @@ use AuditStash\PersisterInterface; use Cake\Datasource\ConnectionManager; use Cake\ElasticSearch\Datasource\Connection; -use Elastica\Client; use Elastica\Document; /** @@ -17,7 +16,7 @@ class ElasticSearchPersister implements PersisterInterface /** * The client or connection to Elasticsearch. * - * @var Elastica\Client + * @var Cake\ElasticSearch\Datasource\Connection */ protected $connection; @@ -139,7 +138,7 @@ public function reuseTransactionId($use = true) * @param Elastica\Client $connection The conneciton to elastic search * @return $this */ - public function setConnection(Client $connection) + public function setConnection(Connection $connection) { $this->connection = $connection; diff --git a/src/Persister/TablePersister.php b/src/Persister/TablePersister.php index 030c515..86f84b2 100644 --- a/src/Persister/TablePersister.php +++ b/src/Persister/TablePersister.php @@ -134,7 +134,7 @@ class TablePersister implements PersisterInterface public function getTable() { if ($this->_table === null) { - $this->setTable($this->config('table')); + $this->setTable($this->getConfig('table')); } return $this->_table; @@ -149,7 +149,7 @@ public function getTable() public function setTable($table) { if (is_string($table)) { - $table = $this->tableLocator()->get($table); + $table = $this->getTableLocator()->get($table); } if (!($table instanceof Table)) { @@ -173,11 +173,11 @@ public function logEvents(array $auditLogs) { $PersisterTable = $this->getTable(); - $serializeFields = $this->config('serializeFields'); - $primaryKeyExtractionStrategy = $this->config('primaryKeyExtractionStrategy'); - $extractMetaFields = $this->config('extractMetaFields'); - $unsetExtractedMetaFields = $this->config('unsetExtractedMetaFields'); - $logErrors = $this->config('logErrors'); + $serializeFields = $this->getConfig('serializeFields'); + $primaryKeyExtractionStrategy = $this->getConfig('primaryKeyExtractionStrategy'); + $extractMetaFields = $this->getConfig('extractMetaFields'); + $unsetExtractedMetaFields = $this->getConfig('unsetExtractedMetaFields'); + $logErrors = $this->getConfig('logErrors'); foreach ($auditLogs as $log) { $fields = $this->extractBasicFields($log, $serializeFields); diff --git a/src/Shell/ElasticMappingShell.php b/src/Shell/ElasticMappingShell.php index 8713404..243384c 100644 --- a/src/Shell/ElasticMappingShell.php +++ b/src/Shell/ElasticMappingShell.php @@ -4,7 +4,6 @@ use Cake\Console\Shell; use Cake\Datasource\ConnectionManager; -use Cake\ORM\TableRegistry; use Cake\Utility\Inflector; use Elastica\Request; use Elastica\Type\Mapping as ElasticaMapping; @@ -47,8 +46,8 @@ public function getOptionParser() */ public function main($table) { - $table = TableRegistry::get($table); - $schema = $table->schema(); + $table = $this->loadModel($table) + $schema = $table->getSchema(); $mapping = [ '@timestamp' => ['type' => 'date', 'format' => 'basic_t_time_no_millis||dateOptionalTime||basic_date_time||ordinal_date_time_no_millis||yyyy-MM-dd HH:mm:ss'], 'transaction' => ['type' => 'text', 'index' => false], diff --git a/tests/Fixture/ElasticArticlesFixture.php b/tests/Fixture/ElasticArticlesFixture.php index 59957e2..96f781c 100644 --- a/tests/Fixture/ElasticArticlesFixture.php +++ b/tests/Fixture/ElasticArticlesFixture.php @@ -14,7 +14,7 @@ class ElasticArticlesFixture extends TestFixture * * @var string */ - public $table = 'articles'; + public $table = 'article'; /** * The mapping data. diff --git a/tests/Fixture/ElasticAuditsFixture.php b/tests/Fixture/ElasticAuditsFixture.php index 29219ff..a54e396 100644 --- a/tests/Fixture/ElasticAuditsFixture.php +++ b/tests/Fixture/ElasticAuditsFixture.php @@ -14,7 +14,7 @@ class ElasticAuditsFixture extends TestFixture * * @var string */ - public $table = 'audits'; + public $table = 'audit'; /** * The mapping data. diff --git a/tests/Fixture/ElasticAuthorsFixture.php b/tests/Fixture/ElasticAuthorsFixture.php index 2f8d2d3..f89e0fc 100644 --- a/tests/Fixture/ElasticAuthorsFixture.php +++ b/tests/Fixture/ElasticAuthorsFixture.php @@ -14,7 +14,7 @@ class ElasticAuthorsFixture extends TestFixture * * @var string */ - public $table = 'authors'; + public $table = 'author'; /** * The mapping data. diff --git a/tests/Fixture/ElasticTagsFixture.php b/tests/Fixture/ElasticTagsFixture.php index acd4d20..12fca1c 100644 --- a/tests/Fixture/ElasticTagsFixture.php +++ b/tests/Fixture/ElasticTagsFixture.php @@ -14,7 +14,7 @@ class ElasticTagsFixture extends TestFixture * * @var string */ - public $table = 'tags'; + public $table = 'tag'; /** * The mapping data. diff --git a/tests/TestCase/Meta/ApplicationMetadataTest.php b/tests/TestCase/Meta/ApplicationMetadataTest.php index ed6339c..d6dd944 100644 --- a/tests/TestCase/Meta/ApplicationMetadataTest.php +++ b/tests/TestCase/Meta/ApplicationMetadataTest.php @@ -20,7 +20,7 @@ class ApplicationMetadataTest extends TestCase public function testDataIsAdded() { $listener = new ApplicationMetadata('my_app', ['extra' => 'thing']); - $this->eventManager()->attach($listener); + $this->getEventManager()->on($listener); $logs[] = new AuditDeleteEvent(1234, 1, 'articles'); $event = $this->dispatchEvent('AuditStash.beforeLog', ['logs' => $logs]); diff --git a/tests/TestCase/Meta/RequestMetadataTest.php b/tests/TestCase/Meta/RequestMetadataTest.php index d592b76..f4b4796 100644 --- a/tests/TestCase/Meta/RequestMetadataTest.php +++ b/tests/TestCase/Meta/RequestMetadataTest.php @@ -5,7 +5,7 @@ use AuditStash\Event\AuditDeleteEvent; use AuditStash\Meta\RequestMetadata; use Cake\Event\EventManagertrait; -use Cake\Network\Request; +use Cake\Http\ServerRequest as Request; use Cake\TestSuite\TestCase; class RequestMetadataTest extends TestCase @@ -22,7 +22,7 @@ public function testRequestDataIsAdded() { $request = $this->createMock(Request::class, ['clientIp', 'here']); $listener = new RequestMetadata($request, 'jose'); - $this->eventManager()->attach($listener); + $this->getEventManager()->on($listener); $request->expects($this->once())->method('clientIp')->will($this->returnValue('12345')); $request->expects($this->once())->method('here')->will($this->returnValue('/things?a=b')); diff --git a/tests/TestCase/Model/Behavior/AuditIntegrationTest.php b/tests/TestCase/Model/Behavior/AuditIntegrationTest.php index 3dbdbb7..38a2b26 100644 --- a/tests/TestCase/Model/Behavior/AuditIntegrationTest.php +++ b/tests/TestCase/Model/Behavior/AuditIntegrationTest.php @@ -7,9 +7,9 @@ use AuditStash\Event\AuditUpdateEvent; use AuditStash\Model\Behavior\AuditLogBehavior; use AuditStash\PersisterInterface; +use Cake\Datasource\ModelAwareTrait; use Cake\Event\Event; use Cake\ORM\Entity; -use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; class DebugPersister implements PersisterInterface @@ -21,6 +21,7 @@ public function logEvents(array $events) class AuditIntegrationTest extends TestCase { + use ModelAwareTrait; /** * Fixtures to use. @@ -42,7 +43,7 @@ class AuditIntegrationTest extends TestCase */ public function setUp() { - $this->table = TableRegistry::get('Articles'); + $this->table = $this->loadModel('Articles'); $this->table->hasMany('Comments'); $this->table->belongsToMany('Tags'); $this->table->belongsTo('Authors'); @@ -249,7 +250,7 @@ public function testUpdateArticleWithHasMany() 'user_id' => 1, 'comment' => 'This is another comment' ]); - $entity->dirty('comments', true); + $entity->setDirty('comments', true); $this->persister ->expects($this->once()) @@ -344,7 +345,7 @@ public function testUpdateWithBelongsToMany() 'name' => 'This is a Tag' ]); $entity->tags[] = $this->table->Tags->get(3); - $entity->dirty('tags', true); + $entity->setDirty('tags', true); $this->persister ->expects($this->once()) @@ -404,17 +405,17 @@ public function testDeleteCascade() 'contain' => ['Comments', 'Tags'] ]); - $this->table->Comments->dependent(true); - $this->table->Comments->cascadeCallbacks(true); + $this->table->Comments->setDependent(true); + $this->table->Comments->setCascadeCallbacks(true); - $this->table->Tags->dependent(true); - $this->table->Tags->cascadeCallbacks(true); + $this->table->Tags->setDependent(true); + $this->table->Tags->getCascadeCallbacks(true); $this->persister ->expects($this->once()) ->method('logEvents') ->will($this->returnCallback(function (array $events) use ($entity) { - $this->assertCount(7, $events); + $this->assertCount(5, $events); $id = $events[0]->getTransactionId(); foreach ($events as $event) { $this->assertinstanceOf(AuditDeleteEvent::class, $event); @@ -426,9 +427,7 @@ public function testDeleteCascade() $this->assertEquals('comments', $events[1]->getSourceName()); $this->assertEquals('comments', $events[2]->getSourceName()); $this->assertEquals('comments', $events[3]->getSourceName()); - $this->assertEquals('articles_tags', $events[4]->getSourceName()); - $this->assertEquals('articles_tags', $events[5]->getSourceName()); - $this->assertEquals('articles', $events[6]->getSourceName()); + $this->assertEquals('articles', $events[4]->getSourceName()); })); $this->table->delete($entity); diff --git a/tests/TestCase/Model/Behavior/AuditLogBehaviorTest.php b/tests/TestCase/Model/Behavior/AuditLogBehaviorTest.php index 3c91ddf..9c19d84 100644 --- a/tests/TestCase/Model/Behavior/AuditLogBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/AuditLogBehaviorTest.php @@ -16,7 +16,7 @@ public function setUp() { parent::setUp(); $this->table = new Table(['table' => 'articles']); - $this->table->primaryKey('id'); + $this->table->setPrimaryKey('id'); $this->behavior = new AuditLogBehavior($this->table, [ 'whitelist' => ['id', 'title', 'body', 'author_id'] ]); @@ -78,7 +78,7 @@ public function testOnSaveUpdateWithWithelist() public function testSaveCreateWithBlacklist() { - $this->behavior->config('blacklist', ['author_id']); + $this->behavior->setConfig('blacklist', ['author_id']); $data = [ 'id' => 13, 'title' => 'The Title', @@ -103,7 +103,7 @@ public function testSaveCreateWithBlacklist() public function testSaveUpdateWithBlacklist() { - $this->behavior->config('blacklist', ['author_id']); + $this->behavior->setConfig('blacklist', ['author_id']); $data = [ 'id' => 13, 'title' => 'The Title', @@ -126,12 +126,12 @@ public function testSaveUpdateWithBlacklist() public function testSaveWithFieldsFromSchema() { - $this->table->schema([ + $this->table->setSchema([ 'id' => ['type' => 'integer'], 'title' => ['type' => 'text'], 'body' => ['type' => 'text'] ]); - $this->behavior->config('whitelist', false); + $this->behavior->setConfig('whitelist', false); $data = [ 'id' => 13, 'title' => 'The Title', diff --git a/tests/TestCase/Persister/ElasticSearchPersisterTest.php b/tests/TestCase/Persister/ElasticSearchPersisterTest.php index 842523e..cd2a367 100644 --- a/tests/TestCase/Persister/ElasticSearchPersisterTest.php +++ b/tests/TestCase/Persister/ElasticSearchPersisterTest.php @@ -34,7 +34,7 @@ class ElasticSearchPersisterTest extends TestCase public function testLogSingleCreateEvent() { $client = ConnectionManager::get('test_elastic'); - $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'articles', 'type' => 'articles']); + $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'article', 'type' => 'article']); $data = [ 'title' => 'A new article', 'body' => 'article body', @@ -44,9 +44,9 @@ public function testLogSingleCreateEvent() $events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data); $persister->logEvents($events); - $client->getIndex('articles')->refresh(); + $client->getIndex('article')->refresh(); - $articles = IndexRegistry::get('Articles')->find()->toArray(); + $articles = IndexRegistry::get('Article')->find()->toArray(); $this->assertCount(1, $articles); $this->assertEquals( @@ -86,7 +86,7 @@ public function testLogSingleCreateEvent() public function testLogSingleUpdateEvent() { $client = ConnectionManager::get('test_elastic'); - $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'articles', 'type' => 'articles']); + $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'article', 'type' => 'article']); $original = [ 'title' => 'Old article title', 'published' => 'N' @@ -99,9 +99,9 @@ public function testLogSingleUpdateEvent() $events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original); $events[0]->setParentSourceName('authors'); $persister->logEvents($events); - $client->getIndex('articles')->refresh(); + $client->getIndex('article')->refresh(); - $articles = IndexRegistry::get('Articles')->find()->toArray(); + $articles = IndexRegistry::get('Article')->find()->toArray(); $this->assertCount(1, $articles); $this->assertEquals( @@ -130,13 +130,13 @@ public function testLogSingleUpdateEvent() public function testLogSingleDeleteEvent() { $client = ConnectionManager::get('test_elastic'); - $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'articles', 'type' => 'articles']); + $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'article', 'type' => 'article']); $events[] = new AuditDeleteEvent('1234', 50, 'articles', 'authors'); $persister->logEvents($events); - $client->getIndex('articles')->refresh(); + $client->getIndex('article')->refresh(); - $articles = IndexRegistry::get('Articles')->find()->toArray(); + $articles = IndexRegistry::get('Article')->find()->toArray(); $this->assertCount(1, $articles); $this->assertEquals( @@ -167,7 +167,7 @@ public function testLogSingleDeleteEvent() public function testLogMultipleEvents() { $client = ConnectionManager::get('test_elastic'); - $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'audits', 'type' => 'audits']); + $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'audit', 'type' => 'audit']); $data = [ 'id' => 3, @@ -188,9 +188,9 @@ public function testLogMultipleEvents() $events[] = new AuditDeleteEvent('1234', 51, 'articles'); $persister->logEvents($events); - $client->getIndex('audits')->refresh(); + $client->getIndex('audit')->refresh(); - $audits = IndexRegistry::get('Audits')->find()->all(); + $audits = IndexRegistry::get('Audit')->find()->all(); $this->assertCount(4, $audits); $audit = $audits->first(); $this->assertEquals( @@ -207,7 +207,7 @@ public function testLogMultipleEvents() public function testPersistingTimeObjects() { $client = ConnectionManager::get('test_elastic'); - $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'articles', 'type' => 'articles']); + $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'article', 'type' => 'article']); $original = [ 'title' => 'Old article title', 'published_date' => new Time('2015-04-12 20:20:21') @@ -219,9 +219,9 @@ public function testPersistingTimeObjects() $events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original); $persister->logEvents($events); - $client->getIndex('articles')->refresh(); + $client->getIndex('article')->refresh(); - $articles = IndexRegistry::get('Articles')->find()->toArray(); + $articles = IndexRegistry::get('Article')->find()->toArray(); $this->assertCount(1, $articles); $this->assertEquals( @@ -257,14 +257,14 @@ public function testPersistingTimeObjects() public function testLogEventWithMetadata() { $client = ConnectionManager::get('test_elastic'); - $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'articles', 'type' => 'articles']); + $persister = new ElasticSearchPersister(['connection' => $client, 'index' => 'article', 'type' => 'article']); $events[] = new AuditDeleteEvent('1234', 50, 'articles', 'authors'); $events[0]->setMetaInfo(['a' => 'b', 'c' => 'd']); $persister->logEvents($events); - $client->getIndex('articles')->refresh(); + $client->getIndex('article')->refresh(); - $articles = IndexRegistry::get('Articles')->find()->toArray(); + $articles = IndexRegistry::get('Article')->find()->toArray(); $this->assertCount(1, $articles); $this->assertEquals(['a' => 'b', 'c' => 'd'], $articles[0]->meta); } diff --git a/tests/TestCase/Persister/TablePersisterTest.php b/tests/TestCase/Persister/TablePersisterTest.php index e4f0c6f..2c790d0 100644 --- a/tests/TestCase/Persister/TablePersisterTest.php +++ b/tests/TestCase/Persister/TablePersisterTest.php @@ -17,10 +17,10 @@ public function initialize(array $config) { parent::initialize($config); - $this->table('audit_logs'); - $this->primaryKey('id'); + $this->setTable('audit_logs'); + $this->setPrimaryKey('id'); - $this->schema([ + $this->setSchema([ 'id' => 'integer', 'transaction' => 'string', 'type' => 'string', @@ -55,7 +55,7 @@ public function setUp() $this->TablePersister = new TablePersister(); - TableRegistry::config('AuditLogs', [ + TableRegistry::getTableLocator()->setConfig('AuditLogs', [ 'className' => AuditLogsTable::class ]); } @@ -82,7 +82,7 @@ public function testConfigDefaults() 'table' => 'AuditLogs', 'unsetExtractedMetaFields' => true, ]; - $this->assertEquals($expected, $this->TablePersister->config()); + $this->assertEquals($expected, $this->TablePersister->getConfig()); } public function testGetTableDefault() @@ -95,7 +95,7 @@ public function testSetTableAsAlias() $this->assertInstanceOf(AuditLogsTable::class, $this->TablePersister->getTable()); $this->assertInstanceOf(TablePersister::class, $this->TablePersister->setTable('Custom')); $this->assertInstanceOf(Table::class, $this->TablePersister->getTable()); - $this->assertEquals('Custom', $this->TablePersister->getTable()->alias()); + $this->assertEquals('Custom', $this->TablePersister->getTable()->getAlias()); } public function testSetTableAsObject() @@ -131,9 +131,9 @@ public function testSerializeNull() 'primary_key' => 1, 'meta' => null ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') @@ -168,16 +168,16 @@ public function testExtractMetaFields() 'foo' => 'bar', 'nested' => 'value' ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'extractMetaFields' => [ 'foo', 'baz.nested' => 'nested' @@ -214,16 +214,16 @@ public function testExtractAllMetaFields() 'bar' => 'foo' ] ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'extractMetaFields' => true ]); $this->TablePersister->setTable($AuditLogsTable); @@ -249,16 +249,16 @@ public function testExtractMetaFieldsDoNotUnset() 'meta' => '{"foo":"bar"}', 'foo' => 'bar' ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'extractMetaFields' => [ 'foo' ], @@ -287,16 +287,16 @@ public function testExtractAllMetaFieldsDoNotUnset() 'meta' => '{"foo":"bar"}', 'foo' => 'bar' ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'extractMetaFields' => true, 'unsetExtractedMetaFields' => false ]); @@ -327,8 +327,8 @@ public function testErrorLogging() ]); $logged = clone $entity; - $logged->errors('field', ['error']); - $logged->source('AuditLogs'); + $logged->setError('field', ['error']); + $logged->setSource('AuditLogs'); $TablePersister ->expects($this->once()) @@ -338,10 +338,10 @@ public function testErrorLogging() Debugger::exportVar($logged, 4) ); - $TablePersister->getTable()->eventManager()->on( + $TablePersister->getTable()->getEventManager()->on( 'Model.beforeSave', function ($event, EntityInterface $entity) { - $entity->errors('field', ['error']); + $entity->setError('field', ['error']); return false; } ); @@ -361,10 +361,10 @@ public function testDisableErrorLogging() ->expects($this->never()) ->method('log'); - $TablePersister->config([ + $TablePersister->setConfig([ 'logErrors' => false ]); - $TablePersister->getTable()->eventManager()->on( + $TablePersister->getTable()->getEventManager()->on( 'Model.beforeSave', function ($event, EntityInterface $entity) { return false; @@ -390,16 +390,16 @@ public function testCompoundPrimaryKeyExtractDefault() 'primary_key' => '[1,2,3]', 'meta' => '[]', ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $AuditLogsTable->schema()->columnType('primary_key', 'string'); + $AuditLogsTable->getSchema()->setColumnType('primary_key', 'string'); $this->TablePersister->setTable($AuditLogsTable); $this->TablePersister->logEvents([$event]); @@ -420,16 +420,16 @@ public function testPrimaryKeyExtractRaw() 'primary_key' => 1, 'meta' => '[]', ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'primaryKeyExtractionStrategy' => TablePersister::STRATEGY_RAW ]); $this->TablePersister->setTable($AuditLogsTable); @@ -451,18 +451,18 @@ public function testCompoundPrimaryKeyExtractRaw() 'primary_key' => [1, 2, 3], 'meta' => '[]', ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $AuditLogsTable->schema()->columnType('primary_key', 'json'); + $AuditLogsTable->getSchema()->setColumnType('primary_key', 'json'); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'primaryKeyExtractionStrategy' => TablePersister::STRATEGY_RAW ]); $this->TablePersister->setTable($AuditLogsTable); @@ -484,16 +484,16 @@ public function testPrimaryKeyExtractProperties() 'primary_key' => 1, 'meta' => '[]', ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'primaryKeyExtractionStrategy' => TablePersister::STRATEGY_PROPERTIES ]); $this->TablePersister->setTable($AuditLogsTable); @@ -517,16 +517,16 @@ public function testCompoundPrimaryKeyExtractProperties() 'primary_key_2' => 3, 'meta' => '[]', ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'primaryKeyExtractionStrategy' => TablePersister::STRATEGY_PROPERTIES ]); $this->TablePersister->setTable($AuditLogsTable); @@ -548,18 +548,18 @@ public function testPrimaryKeyExtractSerialized() 'primary_key' => '"pk"', 'meta' => '[]', ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $AuditLogsTable->schema()->columnType('primary_key', 'string'); + $AuditLogsTable->getSchema()->setColumnType('primary_key', 'string'); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'primaryKeyExtractionStrategy' => TablePersister::STRATEGY_SERIALIZED ]); $this->TablePersister->setTable($AuditLogsTable); @@ -581,18 +581,18 @@ public function testCompoundPrimaryKeyExtractSerialized() 'primary_key' => '[1,2,3]', 'meta' => '[]', ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $AuditLogsTable->schema()->columnType('primary_key', 'string'); + $AuditLogsTable->getSchema()->setColumnType('primary_key', 'string'); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'primaryKeyExtractionStrategy' => TablePersister::STRATEGY_SERIALIZED ]); $this->TablePersister->setTable($AuditLogsTable); @@ -619,23 +619,30 @@ public function testDoNotSerializeFields() 'foo' => 'bar' ], ]); - $entity->source('AuditLogs'); + $entity->setSource('AuditLogs'); - $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save'], TableRegistry::config('AuditLogs')); + $AuditLogsTable = $this->getMockForModel('AuditLogs', ['save']); $AuditLogsTable ->expects($this->once()) ->method('save') ->with($entity) ->willReturn($entity); - $AuditLogsTable->schema()->columnType('original', 'json'); - $AuditLogsTable->schema()->columnType('changed', 'json'); - $AuditLogsTable->schema()->columnType('meta', 'json'); + $AuditLogsTable->getSchema()->setColumnType('original', 'json'); + $AuditLogsTable->getSchema()->setColumnType('changed', 'json'); + $AuditLogsTable->getSchema()->setColumnType('meta', 'json'); - $this->TablePersister->config([ + $this->TablePersister->setConfig([ 'serializeFields' => false ]); $this->TablePersister->setTable($AuditLogsTable); $this->TablePersister->logEvents([$event]); } + + public function getMockForModel($alias, array $methods = [], array $options = []) + { + return parent::getMockForModel($alias, $methods, $options + [ + 'className' => AuditLogsTable::class + ]); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9f21fc5..02dcbbc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -23,4 +23,4 @@ use Cake\Datasource\ConnectionManager; // Connection for audit storage -ConnectionManager::config('test_elastic', ['url' => getenv('elastic_dsn')]); +ConnectionManager::setConfig('test_elastic', ['url' => getenv('elastic_dsn')]);