public function testAggregate() { $paging = $this->getMockBuilder('CM_Paging_Log_Abstract')->disableOriginalConstructor()->setMethods(array('getType'))->getMockForAbstractClass(); $paging->expects($this->any())->method('getType')->will($this->returnValue(1)); /** @var CM_Paging_Log_Abstract $paging */ $paging->__construct(); CMTest_TH::callProtectedMethod($paging, '_add', ['haha']); CMTest_TH::callProtectedMethod($paging, '_add', ['huhu']); CMTest_TH::timeDaysForward(1); CMTest_TH::timeDaysForward(1); CMTest_TH::callProtectedMethod($paging, '_add', ['haha']); CMTest_TH::callProtectedMethod($paging, '_add', ['haha', array('id' => 123123, 'ip' => 1234123)]); CMTest_TH::timeDaysForward(1); CMTest_TH::callProtectedMethod($paging, '_add', ['ha']); CMTest_TH::callProtectedMethod($paging, '_add', ['ha']); CMTest_TH::callProtectedMethod($paging, '_add', ['ha']); $paging->__construct(true, 2 * 86400); $this->assertEquals(2, $paging->getCount()); $warning1 = $paging->getItem(0); $warning2 = $paging->getItem(1); $this->assertEquals(3, $warning1['count']); $this->assertEquals(2, $warning2['count']); $this->assertEquals('haha', $warning2['msg']); $this->assertEquals('ha', $warning1['msg']); }
public function test_encodeRecord() { /** @var CM_Log_Handler_Fluentd|\Mocka\AbstractClassTrait $mock */ $mock = $this->mockClass('CM_Log_Handler_Fluentd')->newInstanceWithoutConstructor(); $this->assertSame([], CMTest_TH::callProtectedMethod($mock, '_encodeRecord', [[]])); $record = ['foo' => ['id' => 123, 'bar' => (object) ['baz'], 'baz' => new DateTime('01-01-2001'), 'bax' => new CM_Model_Mock_Fluentd()]]; $this->assertSame(['foo' => ['id' => '123', 'bar' => ['class' => 'stdClass'], 'baz' => '2001-01-01T00:00:00+00:00', 'bax' => ['class' => 'CM_Model_Mock_Fluentd', 'id' => '42']]], CMTest_TH::callProtectedMethod($mock, '_encodeRecord', [$record])); }
public function testGetScriptList() { $script1 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor(); $script2 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor(); $script2->mockMethod('getRunLevel')->set(10); $script3 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor(); $script3->mockMethod('getRunLevel')->set(1); $loader = new CM_Provision_Loader(); $loader->registerScript($script1); $loader->registerScript($script2); $loader->registerScript($script3); $scriptList = CMTest_TH::callProtectedMethod($loader, '_getScriptList'); $expected = [$script3, $script1, $script2]; $this->assertSame($expected, $scriptList); }
public function testFlushVariationCache() { $test = CM_Model_Splittest::create('foo', ['v1', 'v2']); $variation1 = new CM_Model_SplittestVariation(CM_Db_Db::select('cm_splittestVariation', 'id', ['name' => 'v1'])->fetchColumn()); $variation2 = new CM_Model_SplittestVariation(CM_Db_Db::select('cm_splittestVariation', 'id', ['name' => 'v2'])->fetchColumn()); $variation2->setEnabled(false); $fixture = $this->mockClass('CM_Splittest_Fixture')->newInstanceWithoutConstructor(); $fixture->mockMethod('getId')->set(1); $fixture->mockMethod('getFixtureType')->set(1); CMTest_TH::timeForward(1); $variation = CMTest_TH::callProtectedMethod($test, '_getVariationFixture', [$fixture]); $this->assertSame('v1', $variation); $test->flush(); $variation2->setEnabled(true); $variation1->setEnabled(false); $variation = CMTest_TH::callProtectedMethod($test, '_getVariationFixture', [$fixture]); $this->assertSame('v2', $variation); }
public function testProcessExceptionCatching() { CM_Config::get()->CM_Http_Response_View_Abstract->catchPublicExceptions = true; CM_Config::get()->CM_Http_Response_View_Abstract->exceptionsToCatch = ['CM_Exception_Nonexistent' => []]; $response = $this->mockClass('CM_Http_Response_View_Abstract')->newInstanceWithoutConstructor(); $response->mockMethod('_processView')->set(function () { throw new CM_Exception_Invalid('foo', null, null, ['messagePublic' => new CM_I18n_Phrase('bar')]); }); $response->mockMethod('getRender')->set(new CM_Frontend_Render()); /** @var CM_Http_Response_View_Abstract $response */ CMTest_TH::callProtectedMethod($response, '_process'); $this->assertViewResponseError($response, 'CM_Exception_Invalid', 'bar', true); $response->mockMethod('_processView')->set(function () { throw new CM_Exception_Nonexistent('foo'); }); CMTest_TH::callProtectedMethod($response, '_process'); $this->assertViewResponseError($response, 'CM_Exception_Nonexistent', 'Internal server error', false); }
public function testListFiles() { $filesystem = $this->_mockFilesystem(); $this->_fillFilesystemWithRandomFiles($filesystem); /** @var \Mocka\AbstractClassTrait $filesystem */ $listByPrefixMethod = $filesystem->mockMethod('listByPrefix')->set(function ($path, $noRecursion) use($filesystem) { return $filesystem->callOriginalMethod('listByPrefix', [$path, $noRecursion]); }); /** @var CM_File_Filesystem $filesystem */ $backupManager = $this->mockClass('S3Export_BackupManager')->newInstanceWithoutConstructor(); /** @var S3Export_BackupManager $backupManager */ $this->assertSame(0, $listByPrefixMethod->getCallCount()); $this->assertCount(15, CMTest_TH::callProtectedMethod($backupManager, '_getRandomFiles', [$filesystem, 15, 45])); // 6 times, because it needs to list root folder and then list 5 additional folders (10 files each) to get 45 files $this->assertSame(6, $listByPrefixMethod->getCallCount()); $this->assertCount(200, CMTest_TH::callProtectedMethod($backupManager, '_getRandomFiles', [$filesystem, 201, 500])); $this->assertSame(6 + 21, $listByPrefixMethod->getCallCount()); }
public function testRunNonBlocking() { $process = $this->mockClass('CM_Process')->newInstanceWithoutConstructor(); $forkMock = $process->mockMethod('fork'); $forkMock->set(function () use($forkMock) { $forkHandler = $this->mockClass('CM_Process_ForkHandler')->newInstanceWithoutConstructor(); $forkHandler->mockMethod('getIdentifier')->set($forkMock->getCallCount()); return $forkHandler; }); $manager = $this->mockObject('CM_Clockwork_Manager'); $manager->mockMethod('_shouldRun')->set(true); $manager->mockMethod('_getProcess')->set($process); /** @var CM_Clockwork_Manager $manager */ $event1 = new CM_Clockwork_Event('1', '1 second'); $manager->registerEvent($event1); $event2 = new CM_Clockwork_Event('2', '1 second'); $manager->registerEvent($event2); $process->mockMethod('listenForChildren')->set([]); // no events finish $this->assertFalse(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event1])); $this->assertFalse(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event2])); $manager->runEvents(); $this->assertSame(2, $forkMock->getCallCount()); $this->assertTrue(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event1])); $this->assertTrue(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event2])); // event 2 finishes $process->mockMethod('listenForChildren')->set([2 => new CM_Process_WorkloadResult()]); $manager->runEvents(); $this->assertSame(2, $forkMock->getCallCount()); $this->assertTrue(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event1])); $this->assertFalse(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event2])); // no events finish, event 2 starts $process->mockMethod('listenForChildren')->set([]); $manager->runEvents(); $this->assertSame(3, $forkMock->getCallCount()); $this->assertTrue(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event1])); $this->assertTrue(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event2])); // both events finish, event 2 finishes with an error $process->mockMethod('listenForChildren')->set([1 => new CM_Process_WorkloadResult(), 3 => (new CM_Process_WorkloadResult())->setException(new CM_Exception())]); $manager->runEvents(); $this->assertFalse(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event1])); $this->assertFalse(CMTest_TH::callProtectedMethod($manager, '_isRunning', [$event2])); }
public function testProcessExceptionCatching() { CM_Config::get()->CM_Http_Response_View_Abstract->catchPublicExceptions = true; CM_Config::get()->CM_Http_Response_View_Abstract->exceptionsToCatch = ['CM_Exception_Nonexistent' => []]; $response = $this->mockClass('CM_Http_Response_View_Abstract')->newInstanceWithoutConstructor(); $response->mockMethod('_processView')->set(function () { throw new CM_Exception_Invalid('foo', null, ['messagePublic' => 'bar']); }); $response->mockMethod('getRender')->set(new CM_Frontend_Render()); /** @var CM_Http_Response_View_Abstract $response */ CMTest_TH::callProtectedMethod($response, '_process'); $responseData = CM_Params::jsonDecode($response->getContent()); $this->assertSame(['error' => ['type' => 'CM_Exception_Invalid', 'msg' => 'bar', 'isPublic' => true]], $responseData); $response->mockMethod('_processView')->set(function () { throw new CM_Exception_Nonexistent('foo'); }); CMTest_TH::callProtectedMethod($response, '_process'); $responseData = CM_Params::jsonDecode($response->getContent()); $this->assertSame(['error' => ['type' => 'CM_Exception_Nonexistent', 'msg' => 'Internal server error', 'isPublic' => false]], $responseData); }
public function test_instantiateJobSetServiceManager() { /** @var CM_Jobdistribution_Job_Abstract|\Mocka\AbstractClassTrait|CM_Service_ManagerAwareInterface $job */ $job = $this->mockClass('CM_Jobdistribution_Job_Abstract', ['CM_Service_ManagerAwareInterface'], ['CM_Service_ManagerAwareTrait'])->newInstance(); $queue = new CM_Jobdistribution_DelayedQueue($this->getServiceManager()); $job = CMTest_TH::callProtectedMethod($queue, '_instantiateJob', [get_class($job)]); $this->assertEquals($this->getServiceManager(), $job->getServiceManager()); }
public function testIncreaseUpdateCountNewDeploy() { $now = time(); $languageKey = $this->mockObject('CM_Model_LanguageKey'); $languageKey->mockMethod('_getDeployVersion')->set($now + 1); /** @var CM_Model_LanguageKey $languageKey */ $languageKey->_set(['updateCountResetVersion' => $now, 'updateCount' => 5]); CMTest_TH::callProtectedMethod($languageKey, '_increaseUpdateCount'); $this->assertSame($now + 1, $languageKey->_get('updateCountResetVersion')); $this->assertSame(1, $languageKey->_get('updateCount')); }
public function testRunWithCatching() { $response = $this->mockClass('CM_Http_Response_Abstract')->newInstanceWithoutConstructor(); $className = get_class($response); // test logging and errorCallback-execution CM_Config::get()->{$className} = new stdClass(); CM_Config::get()->{$className}->exceptionsToCatch = ['CM_Exception_Nonexistent' => ['log' => 'CM_Paging_Log_NotFound', 'foo' => 'bar'], 'CM_Exception_InvalidParam' => ['log' => null]]; $exceptionCodeExecutionCounter = 0; $errorCode = function (CM_Exception_Nonexistent $ex, $errorOptions) use(&$exceptionCodeExecutionCounter) { $this->assertSame('bar', $errorOptions['foo']); $exceptionCodeExecutionCounter++; }; $this->assertSame(0, $exceptionCodeExecutionCounter); $this->assertCount(0, new CM_Paging_Log_NotFound()); CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { }, $errorCode]); $this->assertSame(0, $exceptionCodeExecutionCounter); $this->assertCount(0, new CM_Paging_Log_NotFound()); CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Nonexistent(); }, $errorCode]); $this->assertSame(1, $exceptionCodeExecutionCounter); $this->assertCount(1, new CM_Paging_Log_NotFound()); $errorCode = function (CM_Exception_InvalidParam $ex, $errorOptions) use(&$exceptionCodeExecutionCounter) { $exceptionCodeExecutionCounter++; }; CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_InvalidParam(); }, $errorCode]); $this->assertSame(2, $exceptionCodeExecutionCounter); $this->assertCount(1, new CM_Paging_Log_NotFound()); // test public/non-public exceptions not marked for catching // public exception, no public exception catching CM_Config::get()->{$className}->exceptionsToCatch = []; $errorCode = function (CM_Exception_Nonexistent $ex, $errorOptions) use(&$exceptionCodeExecutionCounter) { $exceptionCodeExecutionCounter++; $this->assertTrue($ex->isPublic()); }; try { CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Nonexistent('foo', null, ['messagePublic' => 'bar']); }, $errorCode]); $this->fail('Caught public exception with public exception catching disabled'); } catch (CM_Exception_Nonexistent $ex) { $this->assertTrue($ex->isPublic()); $this->assertSame('foo', $ex->getMessage()); } $this->assertSame(2, $exceptionCodeExecutionCounter); // non-public exception, public exception catching CM_Config::get()->{$className}->catchPublicExceptions = true; try { CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Nonexistent('foo'); }, $errorCode]); $this->fail('Caught non-public exception that was not configured to be caught'); } catch (CM_Exception_Nonexistent $ex) { $this->assertFalse($ex->isPublic()); $this->assertSame('foo', $ex->getMessage()); } $this->assertSame(2, $exceptionCodeExecutionCounter); // public exception, public exception catching try { $returnValue = CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Nonexistent('foo', null, ['messagePublic' => 'bar']); }, $errorCode]); $this->assertNull($returnValue); } catch (CM_Exception_Nonexistent $ex) { $this->fail('Caught non-public exception'); } $this->assertSame(3, $exceptionCodeExecutionCounter); }
/** * @param mixed $object * @param string $methodName * @param array|null $arguments * @return mixed */ public function callProtectedMethod($object, $methodName, array $arguments = null) { return CMTest_TH::callProtectedMethod($object, $methodName, $arguments); }
public function test_checkResultForErrors() { $mongoDb = CM_Service_Manager::getInstance()->getMongoDb(); CMTest_TH::callProtectedMethod($mongoDb, '_checkResultForErrors', [true]); CMTest_TH::callProtectedMethod($mongoDb, '_checkResultForErrors', [['ok' => 1]]); try { CMTest_TH::callProtectedMethod($mongoDb, '_checkResultForErrors', [false]); } catch (CM_MongoDb_Exception $ex) { $this->assertSame(['result' => false], $ex->getMetaInfo()); } try { CMTest_TH::callProtectedMethod($mongoDb, '_checkResultForErrors', [['ok' => 0, 'errmsg' => 'foo']]); } catch (CM_MongoDb_Exception $ex) { $this->assertSame(['result' => ['ok' => 0, 'errmsg' => 'foo']], $ex->getMetaInfo()); } }
/** * @expectedException CM_Cli_Exception_Internal * @expectedExceptionMessage Module `foo` must exist! */ public function testCreateNamespaceNoModule() { $app = $this->_mockAppInstallation(); $cli = $this->_mockGeneratorCli($app); CMTest_TH::callProtectedMethod($cli, '_createNamespace', ['foo', 'bar']); }
public function testRunWithCatching() { /** @var CM_Http_Response_Abstract|\Mocka\AbstractClassTrait $response */ $response = $this->mockClass('CM_Http_Response_Abstract')->newInstanceWithoutConstructor(); $response->mockMethod('getRequest')->set(new CM_Http_Request_Get('/foo/bar/')); $response->setServiceManager($this->getServiceManager()); $className = get_class($response); // test logging and errorCallback-execution CM_Config::get()->{$className} = new stdClass(); CM_Config::get()->{$className}->exceptionsToCatch = ['CM_Exception_Nonexistent' => ['log' => true, 'level' => CM_Log_Logger::INFO, 'foo' => 'bar'], 'CM_Exception_InvalidParam' => []]; $exceptionCodeExecutionCounter = 0; $errorCode = function (CM_Exception_Nonexistent $ex, $errorOptions) use(&$exceptionCodeExecutionCounter) { $this->assertSame('bar', $errorOptions['foo']); $exceptionCodeExecutionCounter++; }; $this->assertSame(0, $exceptionCodeExecutionCounter); $this->assertCount(0, new CM_Paging_Log([CM_Log_Logger::INFO])); CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { }, $errorCode]); $this->assertSame(0, $exceptionCodeExecutionCounter); $this->assertCount(0, new CM_Paging_Log([CM_Log_Logger::INFO])); CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Nonexistent(); }, $errorCode]); $this->assertSame(1, $exceptionCodeExecutionCounter); $this->assertCount(1, new CM_Paging_Log([CM_Log_Logger::INFO])); $errorCode = function (CM_Exception_InvalidParam $ex, $errorOptions) use(&$exceptionCodeExecutionCounter) { $exceptionCodeExecutionCounter++; }; CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_InvalidParam(); }, $errorCode]); $this->assertSame(2, $exceptionCodeExecutionCounter); $this->assertCount(1, new CM_Paging_Log([CM_Log_Logger::INFO])); // test public/non-public exceptions not marked for catching $exceptionCodeExecutionCounter = 0; // public exception, no public exception catching CM_Config::get()->{$className}->exceptionsToCatch = []; $errorCode = function (CM_Exception_Nonexistent $ex, $errorOptions) use(&$exceptionCodeExecutionCounter) { $exceptionCodeExecutionCounter++; $this->assertTrue($ex->isPublic()); }; try { CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Nonexistent('foo', null, null, ['messagePublic' => new CM_I18n_Phrase('bar')]); }, $errorCode]); $this->fail('Caught public exception with public exception catching disabled'); } catch (CM_Exception_Nonexistent $ex) { $this->assertTrue($ex->isPublic()); $this->assertSame('foo', $ex->getMessage()); } $this->assertSame(0, $exceptionCodeExecutionCounter); // non-public exception, public exception catching CM_Config::get()->{$className}->catchPublicExceptions = true; try { CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Nonexistent('foo'); }, $errorCode]); $this->fail('Caught non-public exception that was not configured to be caught'); } catch (CM_Exception_Nonexistent $ex) { $this->assertFalse($ex->isPublic()); $this->assertSame('foo', $ex->getMessage()); } $this->assertSame(0, $exceptionCodeExecutionCounter); // public exception, public exception catching try { $returnValue = CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Nonexistent('foo', null, null, ['messagePublic' => new CM_I18n_Phrase('bar')]); }, $errorCode]); $this->assertNull($returnValue); } catch (CM_Exception_Nonexistent $ex) { $this->fail('Caught non-public exception'); } $this->assertSame(1, $exceptionCodeExecutionCounter); // test child exception catching $response = $this->mockClass('CM_Http_Response_Abstract')->newInstanceWithoutConstructor(); $className = get_class($response); CM_Config::get()->{$className} = new stdClass(); CM_Config::get()->{$className}->exceptionsToCatch = ['CM_Exception' => []]; $exceptionCodeExecutionCounter = 0; $errorCode = function (CM_Exception $ex, $errorOptions) use(&$exceptionCodeExecutionCounter) { $exceptionCodeExecutionCounter++; }; try { $returnValue = CMTest_TH::callProtectedMethod($response, '_runWithCatching', [function () { throw new CM_Exception_Invalid('foo'); }, $errorCode]); $this->assertNull($returnValue); } catch (CM_Exception_Nonexistent $ex) { $this->fail("Didn't catch the child of an exception that was configured to be caught"); } $this->assertSame(1, $exceptionCodeExecutionCounter); }
public function testBulkAddDelete() { $cmClient = $this->_getCmClient(); $indexName = 'index1'; $typeName = 'typeName'; $cmClient->createIndex($indexName, $typeName, [], [], false); $documentList = [new CM_Elasticsearch_Document('1', ['name' => 'foo']), new CM_Elasticsearch_Document('2', ['name' => 'bar']), new CM_Elasticsearch_Document('3', ['name' => 'baz']), new CM_Elasticsearch_Document('4', ['name' => 'quux'])]; $cmClient->bulkAddDocuments($documentList, $indexName, $typeName); $cmClient->refreshIndex($indexName); $this->assertSame(4, $cmClient->count($indexName, $typeName)); $cmClient->bulkDeleteDocuments(['1', '2'], $indexName, $typeName); $cmClient->refreshIndex($indexName); $this->assertSame(2, $cmClient->count($indexName, $typeName)); $cmClient->putIndexSettings($indexName, ['blocks.write' => 1]); $exception = $this->catchException(function () use($cmClient, $indexName, $typeName) { $cmClient->bulkDeleteDocuments(['11', '222', '3333', '4444', '555'], $indexName, $typeName); }); $this->assertInstanceOf('CM_Exception_Invalid', $exception); /** @var CM_Exception_Invalid $exception */ $this->assertSame('Error(s) in bulk request action(s)', $exception->getMessage()); $this->assertSame(['errorsCount' => 5, 'message' => 'Operator `delete` ClusterBlockException[blocked by: [FORBIDDEN/8/index write (api)];]' . PHP_EOL . 'Operator `delete` ClusterBlockException[blocked by: [FORBIDDEN/8/index write (api)];]' . PHP_EOL . 'Operator `delete` ClusterBlockException[blocked by: [FORBIDDEN/8/index write (api)];]' . PHP_EOL], $exception->getMetaInfo()); $exception = $this->catchException(function () use($cmClient, $indexName, $typeName) { $cmClient->bulkAddDocuments([new CM_Elasticsearch_Document('434', ['name' => 'bar'])], $indexName, $typeName); }); $this->assertInstanceOf('CM_Exception_Invalid', $exception); /** @var CM_Exception_Invalid $exception */ $this->assertSame('Error(s) in bulk request action(s)', $exception->getMessage()); $this->assertContains('Operator `index` ClusterBlockException[blocked by: [FORBIDDEN/8/index write (api)];]', $exception->getMetaInfo()['message']); $exception = $this->catchException(function () use($cmClient) { CMTest_TH::callProtectedMethod($cmClient, '_processBulkResponse', [['errors' => 1]]); }); $this->assertInstanceOf('CM_Exception_Invalid', $exception); /** @var CM_Exception_Invalid $exception */ $this->assertSame('Unknown error in one or more bulk request actions', $exception->getMessage()); $cmClient->deleteIndex($indexName); }