io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock(); $this->shell = new SchemaCacheShell($this->io); $this->cache = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock(); $this->cache->expects($this->any()) ->method('init') ->will($this->returnValue(true)); Cache::setConfig('orm_cache', $this->cache); $ds = ConnectionManager::get('test'); $ds->cacheMetadata('orm_cache'); } /** * Teardown * * @return void */ public function tearDown() { parent::tearDown(); Cache::drop('orm_cache'); $ds = ConnectionManager::get('test'); $ds->cacheMetadata(false); } /** * Test that clear enables the cache if it was disabled. * * @return void */ public function testClearEnablesMetadataCache() { $ds = ConnectionManager::get('test'); $ds->cacheMetadata(false); $this->shell->params['connection'] = 'test'; $this->shell->clear(); $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->getSchemaCollection()); } /** * Test that build enables the cache if it was disabled. * * @return void */ public function testBuildEnablesMetadataCache() { $ds = ConnectionManager::get('test'); $ds->cacheMetadata(false); $this->shell->params['connection'] = 'test'; $this->shell->build(); $this->assertInstanceOf('Cake\Database\Schema\CachedCollection', $ds->getSchemaCollection()); } /** * Test build() with no args. * * @return void */ public function testBuildNoArgs() { $this->cache->expects($this->any()) ->method('write') ->will($this->returnValue(true)); $this->cache->expects($this->at(3)) ->method('write') ->with('test_articles'); $this->shell->params['connection'] = 'test'; $this->shell->build(); } /** * Test build() with one arg. * * @return void */ public function testBuildNamedModel() { $this->cache->expects($this->once()) ->method('write') ->with('test_articles') ->will($this->returnValue(true)); $this->cache->expects($this->never()) ->method('delete'); $this->shell->params['connection'] = 'test'; $this->shell->build('articles'); } /** * Test build() overwrites cached data. * * @return void */ public function testBuildOverwritesExistingData() { $this->cache->expects($this->once()) ->method('write') ->with('test_articles') ->will($this->returnValue(true)); $this->cache->expects($this->never()) ->method('read'); $this->cache->expects($this->never()) ->method('delete'); $this->shell->params['connection'] = 'test'; $this->shell->build('articles'); } /** * Test build() with a non-existing connection name. * * @return void */ public function testBuildInvalidConnection() { $this->expectException(StopException::class); $this->shell->params['connection'] = 'derpy-derp'; $this->shell->build('articles'); } /** * Test clear() with an invalid connection name. * * @return void */ public function testClearInvalidConnection() { $this->expectException(StopException::class); $this->shell->params['connection'] = 'derpy-derp'; $this->shell->clear('articles'); } /** * Test clear() with no args. * * @return void */ public function testClearNoArgs() { $this->cache->expects($this->at(3)) ->method('delete') ->with('test_articles') ->will($this->returnValue(true)); $this->shell->params['connection'] = 'test'; $this->shell->clear(); } /** * Test clear() with a model name. * * @return void */ public function testClearNamedModel() { $this->cache->expects($this->never()) ->method('write'); $this->cache->expects($this->once()) ->method('delete') ->with('test_articles'); $this->shell->params['connection'] = 'test'; $this->shell->clear('articles'); } }