/** * Set up * * @return void */ public function setUp() { parent::setUp(); $this->tag = $this->getMock('Cake\\ORM\\Table', ['find', 'delete'], [['alias' => 'Tags', 'table' => 'tags']]); $this->tag->schema(['id' => ['type' => 'integer'], 'name' => ['type' => 'string'], '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]]); $this->article = $this->getMock('Cake\\ORM\\Table', ['find', 'delete'], [['alias' => 'Articles', 'table' => 'articles']]); $this->article->schema(['id' => ['type' => 'integer'], 'name' => ['type' => 'string'], '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]]); TableRegistry::set('Articles', $this->article); TableRegistry::get('ArticlesTags', ['table' => 'articles_tags', 'schema' => ['article_id' => ['type' => 'integer'], 'tag_id' => ['type' => 'integer'], '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['article_id', 'tag_id']]]]]); $this->tagsTypeMap = new TypeMap(['Tags.id' => 'integer', 'id' => 'integer', 'Tags.name' => 'string', 'name' => 'string']); $this->articlesTagsTypeMap = new TypeMap(['ArticlesTags.article_id' => 'integer', 'article_id' => 'integer', 'ArticlesTags.tag_id' => 'integer', 'tag_id' => 'integer']); }
/** * @return void * @covers ::run */ public function testRunExecuteJob() { $table = $this->getModel('\\DelayedJobs\\Model\\Table\\DelayedJobsTable', ['get'], 'DelayedJobs', 'delayed_jobs'); TableRegistry::set('DelayedJobs.DelayedJobs', $table); $job_data = Fabricate::attributes_for('DelayedJobs.DelayedJobs')[0]; $job = $this->getMock('\\DelayedJobs\\Model\\Entity\\DelayedJob', ['execute'], [$job_data]); $job_output = 'Test completed'; $table->expects($this->once())->method('get')->willReturn($job); $job->expects($this->once())->method('execute')->willReturn($job_output); $this->get('/delayed_jobs/run/' . $job->id); $this->assertSession('Job Completed: ' . $job_output, 'Flash.flash.message'); $this->assertResponseSuccess(); $this->assertRedirect(['action' => 'index']); }
/** * Test initial setup * * @return void */ public function testUrl() { // mock of entity $filename = 'test.jpg'; $entity = $this->getMock('Cake\\ORM\\Entity', ['source', 'get']); $entity->expects($this->once())->method('source')->will($this->returnValue('TestTables')); $entity->expects($this->once())->method('get')->will($this->returnValue($filename)); // mock of table $table = $this->getMock('Cake\\ORM\\Table', ['getUploadFolder']); $table->expects($this->once())->method('getUploadFolder')->will($this->returnValue('/path/to/folder/')); TableRegistry::set('TestTables', $table); // test $url = $this->Upload->url($entity, 'test_field'); $this->assertContains($filename, $url); }
/** * Test inject search * * @return void */ public function testInjectSearch() { \Cake\Core\Plugin::load('Search', ['path' => ROOT . DS]); $params = ['search' => ['name' => '1st post']]; $request = new \Cake\Network\Request(); $response = new \Cake\Network\Response(); $eventManager = new \Cake\Event\EventManager(); $controller = new \Cake\Controller\Controller($request, $response, 'Search', $eventManager); $tableMock = $this->getMockBuilder('\\Cake\\ORM\\Table')->setMockClassName('SearchTables')->setMethods(['filterParams'])->getMock(); $tableMock->expects($this->once())->method('filterParams')->will($this->returnCallback(function () use($params) { return $params; })); \Cake\ORM\TableRegistry::set('Search', $tableMock); $queryMock = $this->getMockBuilder('\\Cake\\ORM\\Query')->disableOriginalConstructor()->getMock(); $queryMock->expects($this->once())->method('find')->with('search', $params)->will($this->returnValue($queryMock)); $subject = new \Crud\Event\Subject(); $subject->query = $queryMock; $event = new \Cake\Event\Event('Crud.beforeLookup', $subject); $listener = new \Crud\Listener\SearchListener($controller, ['enabled' => ['Crud.beforeLookup']]); $listener->injectSearch($event); }
/** * Mock a model, maintain fixtures and table association * * @param string $alias * @param array $methods * @param array $options * @throws \Cake\ORM\Error\MissingTableClassException * @return Model */ public function getMockForModel($alias, array $methods = array(), array $options = array()) { if (empty($options['className'])) { $class = Inflector::camelize($alias); $className = App::classname($class, 'Model/Table', 'Table'); if (!$className) { throw new \Cake\ORM\Error\MissingTableClassException(array($alias)); } $options['className'] = $className; } list($plugin, $baseClass) = pluginSplit($alias); $options += ['alias' => $baseClass] + TableRegistry::config($alias); $mock = $this->getMock($options['className'], $methods, array($options)); TableRegistry::set($alias, $mock); return $mock; }
/** * Mock a model, maintain fixtures and table association * * @param string $alias The model to get a mock for. * @param mixed $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($alias, array $methods = [], 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; } $connectionName = $options['className']::defaultConnectionName(); $connection = ConnectionManager::get($connectionName); list(, $baseClass) = pluginSplit($alias); $options += ['alias' => $baseClass, 'connection' => $connection]; $options += TableRegistry::config($alias); $mock = $this->getMock($options['className'], $methods, [$options]); if (empty($options['entityClass']) && $mock->entityClass() === '\\Cake\\ORM\\Entity') { $parts = explode('\\', $options['className']); $entityAlias = Inflector::singularize(substr(array_pop($parts), 0, -5)); $entityClass = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $entityAlias; if (class_exists($entityClass)) { $mock->entityClass($entityClass); } } TableRegistry::set($baseClass, $mock); return $mock; }
/** * Test setting an instance with plugin syntax aliases * * @return void */ public function testSetPlugin() { Plugin::load('TestPlugin'); $mock = $this->getMock('TestPlugin\\Model\\Table\\CommentsTable'); $this->assertSame($mock, TableRegistry::set('TestPlugin.Comments', $mock)); $this->assertSame($mock, TableRegistry::get('TestPlugin.Comments')); $this->assertSame($mock, TableRegistry::get('Comments')); }
/** * Mock a model, maintain fixtures and table association * * @param string $alias The model to get a mock for. * @param mixed $methods The list of methods to mock * @param array $options The config data for the mock's constructor. * @throws \Cake\ORM\Exception\MissingTableClassException * @return Model */ public function getMockForModel($alias, array $methods = array(), array $options = array()) { if (empty($options['className'])) { $class = Inflector::camelize($alias); $className = App::className($class, 'Model/Table', 'Table'); if (!$className) { throw new \Cake\ORM\Exception\MissingTableClassException(array($alias)); } $options['className'] = $className; } $connectionName = $options['className']::defaultConnectionName(); $connection = ConnectionManager::get($connectionName); list($plugin, $baseClass) = pluginSplit($alias); $options += ['alias' => $baseClass, 'connection' => $connection]; $options += TableRegistry::config($alias); $mock = $this->getMock($options['className'], $methods, [$options]); TableRegistry::set($alias, $mock); return $mock; }
/** * Test setting an instance. * * @return void */ public function testSet() { $mock = $this->getMock('Cake\\ORM\\Table'); $this->assertSame($mock, TableRegistry::set('Articles', $mock)); $this->assertSame($mock, TableRegistry::get('Articles')); }
/** * Test the get() method. * * @return void */ public function testSet() { $table = $this->getMock('Cake\\ORM\\Table'); $locator = $this->_setMockLocator(); $locator->expects($this->once())->method('set')->with('Test', $table); TableRegistry::set('Test', $table); }