public function testExecuteReplacesBaseClassesAndLinkedDataMergeInFiles() { $inputMock = $this->getMock(ArrayInput::class, ['getArgument'], [], '', false); $inputMock->expects($this->exactly(3))->method('getArgument')->will($this->returnValueMap([['querybase', 'Project/PlanBundle/WidgetQuery'], ['peerbase', 'Project/PlanBundle/WidgetPeer'], ['modelbase', 'Project/PlanBundle/Widget']])); $commandUtilityMock = $this->getMock(CommandUtility::class, ['exec']); // list directories command $commandUtilityMock->expects($this->at(0))->method('exec')->with($this->equalTo('find ./src -type d |grep -v "SOA/SOABundle" |grep "Model/om$"'))->will($this->returnCallback(function ($command, &$output, &$returnVar = 0) { $output[] = 'src/Project/PlanBundle/Model/om'; })); // list peers command $commandUtilityMock->expects($this->at(1))->method('exec')->with($this->equalTo('find src/Project/PlanBundle/Model/om -maxdepth 1 -type f |grep "Peer.php$"'))->will($this->returnCallback(function ($command, &$output, &$returnVar = 0) { $output[] = 'src/Project/PlanBundle/Model/om/WidgetPeer.php'; })); // replace in peers $commandUtilityMock->expects($this->at(2))->method('exec'); // list queries command $commandUtilityMock->expects($this->at(3))->method('exec')->with($this->equalTo('find src/Project/PlanBundle/Model/om -maxdepth 1 -type f |grep "Query.php$"'))->will($this->returnCallback(function ($command, &$output, &$returnVar = 0) { $output[] = 'src/Project/PlanBundle/Model/om/WidgetQuery.php'; })); // replace in queries $commandUtilityMock->expects($this->at(4))->method('exec'); // list models command $commandUtilityMock->expects($this->at(5))->method('exec')->with($this->equalTo('find src/Project/PlanBundle/Model/om -maxdepth 1 -type f |grep -v "Query.php$" |grep -v "Peer.php$"'))->will($this->returnCallback(function ($command, &$output, &$returnVar = 0) { $output[] = 'src/Project/PlanBundle/Model/om/Widget.php'; })); // replace in models $commandUtilityMock->expects($this->at(6))->method('exec'); Factory::injectObject(CommandUtility::class, $commandUtilityMock); $command = new SoafyModelsCommandMock(); $command->publicExecute($inputMock, new NullOutput()); }
public function testIsAuthorizedReturnsTrueWhenClientIdIsNull() { $clientPeerMock = $this->getClientPeerMock(null); Factory::injectObject(ClientPeer::class, $clientPeerMock); $course = new Course(); $this->assertTrue($course->isAuthorized()); }
/** * This method will add the data pulled in by our join to an array representation of a model. * * @param PropelSOAModel $model * @param array $data */ public function addJoinedDataToResultArray(PropelSOAModel $model, array &$data) { $classInfo = Factory::createNewObject(SymfonyClassInfo::class); $classInfo->parseClassPath(get_class($model)); $peerClass = $classInfo->getClassPath('peer'); $peer = Factory::createNewObject($peerClass); $getMethod = $peer->linkedData[$this->relation]; $data[$this->relation] = $model->{$getMethod}(); }
/** * This method will filter our query results to a particular billing client ID. * * @param int $clientId * @return QuestionQuery */ public function filterByClientId($clientId) { $userIds = []; $users = Factory::createNewQueryObject(UserQuery::class)->findByBillingClientId($clientId); foreach ($users as $user) { $userIds[] = $user->getUserId(); } return $this->filterByAuthUserId($userIds); }
/** * This method will download one script file and save it to the proper destination. * * @param InputInterface $input * @param OutputInterface $output * @param $controllerFilePath * @param $baseUrl */ protected function generateScript(InputInterface $input, OutputInterface $output, $controllerFilePath, $baseUrl) { $pieces = explode('/', $controllerFilePath); $srcIndex = array_search('src', $pieces); if ($srcIndex === false) { throw new Exception('Could not find the src directory'); } $params = []; $params['namespace'] = $pieces[$srcIndex + 1]; $params['bundle'] = str_replace('Bundle', '', $pieces[$srcIndex + 2]); $lastIndex = count($pieces) - 1; $className = str_replace('.php', '', $pieces[$lastIndex]); $params['entity'] = str_replace('Controller', '', $className); // check to see if this controller is really a service resource $classInfo = $this->classInfo = Factory::createNewObject(SymfonyClassInfo::class); $classInfo->namespace = $params['namespace']; $classInfo->bundle = $params['bundle']; $classInfo->entity = $params['entity']; $routesToGenerate = []; $controllerClass = $classInfo->getClassPath('controller'); $reflectionObject = Factory::createNewObject(ReflectionClass::class, [$controllerClass]); // if the controller is abstract, skip this entirely, don't implement any functionality for // the controller in JavaScript if ($reflectionObject->isAbstract()) { return; } // based on reflection, we will determine which types of objects should get standard class // definitions in the javascript. note that objects that do not get standard definitions // can get custom object definitions where the developer can make the javascript do similar // tasks but in a non-standard way. if ($reflectionObject->isSubclassOf(ModelBasedServiceController::class)) { // model-based controllers get query and object definitions generated. this is because // we can predict the data they should contain by inspecting the model, and we know // generally how to query for this data in a standard way through Propel $routesToGenerate['query'] = 'propelsoa_generatequery_route'; $routesToGenerate['object'] = 'propelsoa_generateobject_route'; } if ($reflectionObject->implementsInterface(Collectionable::class)) { // a controller signals that it wants us to build a standard collection object for it // by implementing the Collectionable interface $routesToGenerate['collection'] = 'propelsoa_generatecollection_route'; } // for a lot of objects, the basic generated object is not enough in javascript. we want our // javascript objects to be strong and contain all of their related logic and data. what // this means is that the generated object is a good starting point, but not the final // result. we can indicate with an interface that we want the generated object to not be // the final object loaded by getNewObject() in propelsoa. if ($reflectionObject->implementsInterface(ClientExtendable::class)) { $routesToGenerate['object'] = 'propelsoa_generatepartialobject_route'; } $router = $this->getContainer()->get('router'); foreach ($routesToGenerate as $routeKey) { $url = $baseUrl . $router->generate($routeKey, $params); $this->code .= $this->commandUtility->fileGetContents($url); } }
/** * This action will send an email to the correct Builder Professional team. * * @param Request $request */ public function postAction(Request $request) { $rawData = $request->getContent(); $post = json_decode($rawData, true); $contactUsValidator = Factory::createNewObject(Validator::class); if (!$contactUsValidator->validateWithRulesKey($post, 'contactus')) { return $contactUsValidator->getHttpErrorResponse(); } $contactUsData = [$post['Name'], $post['Email'], $post['Phone'], $post['Time'], $post['Question']]; $contactUsNotification = Factory::createNewObject(ContactUs::class, $contactUsData); $contactUsNotification->send(); return new PropelSOASuccessResponse(['sent' => true]); }
public function testFilterByClientIdLimitsByRelatedUserIds() { $clientId = 8828; $userId1 = 299382; $userId2 = 111883; $userMock1 = $this->getUserMock($userId1); $userMock2 = $this->getUserMock($userId2); $userCollection = new PropelCollection(); $userCollection->setData([$userMock1, $userMock2]); $userQueryMock = $this->getMock(UserQuery::class, ['findByBillingClientId']); $userQueryMock->expects($this->any())->method('findByBillingClientId')->with($this->equalTo($clientId))->willReturn($userCollection); Factory::injectQueryObject(UserQuery::class, $userQueryMock); $query = $this->getMock(QuestionQuery::class, ['filterByAuthUserId']); $query->expects($this->once())->method('filterByAuthUserId')->with($this->equalTo([$userId1, $userId2])); $query->filterByClientId($clientId); }
/** * This method will determine if the current client or user has access to view our materials. * * @return int */ public function isAuthorized() { $clientPeer = Factory::createNewObject(ClientPeer::class); $clientId = $clientPeer->getWorkingClientId(); if ($clientId === null) { return true; } $client = Factory::createNewQueryObject(ClientQuery::class)->findOneByClientId($clientId); foreach ($client->getClientProducts() as $clientProduct) { if ($clientProduct->getProductId() == $this->getBillingProductId()) { // Our related product is in the list of products the client has access to, so they should be able to view this. return 1; } if ($clientProduct->getProduct()->getCode() == 'NEVER_STOP_LEARNING') { // This client is on the all-access plan, and should always have access return 1; } } return 0; }
public function testAddJoinedDataToResultArrayChecksPeerAndAddsResultOfMappedMethod() { $peerClass = 'MyPeerClass'; $relation = 'Widget'; $widgetValue = 'value'; $peer = new PropelSOAPeer(); $peer->linkedData = ['Widget' => 'getMyWidgets']; $modelMock = $this->getMock(PropelSOAModel::class, ['getMyWidgets']); $modelMock->expects($this->once())->method('getMyWidgets')->willReturn($widgetValue); $classInfoMock = $this->getMock(SymfonyClassInfo::class, ['parseClassPath', 'getClassPath']); $classInfoMock->expects($this->once())->method('parseClassPath')->with($this->equalTo(get_class($modelMock))); $classInfoMock->expects($this->once())->method('getClassPath')->with($this->equalTo('peer'))->willReturn($peerClass); Factory::injectObject($peerClass, $peer); Factory::injectObject(SymfonyClassInfo::class, $classInfoMock); $data = []; $linkedData = new LinkedData(); $linkedData->relation = $relation; $linkedData->addJoinedDataToResultArray($modelMock, $data); $this->assertEquals($widgetValue, $data[$relation]); }
/** * This method will execute the main trunk of execution for this command including * finding model system files throughout the application. * * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $this->input = $input; $this->output = $output; $this->commandUtility = Factory::createNewObject(CommandUtility::class); $this->determineBaseClasses(); // find all model directories in the project $directoryList = []; $listDirectoryCommand = 'find ./src -type d |grep -v "SOA/SOABundle" |grep "Model/om$"'; $this->commandUtility->exec($listDirectoryCommand, $directoryList); $listPeerCommand = 'find %s -maxdepth 1 -type f |grep "Peer.php$"'; $listQueryCommand = 'find %s -maxdepth 1 -type f |grep "Query.php$"'; $listModelCommand = 'find %s -maxdepth 1 -type f |grep -v "Query.php$" |grep -v "Peer.php$"'; // this is how many duplicated slashes it takes to get through all of the condensation, why can't // namespace paths be with pipes or something instead?? $separator = '\\\\\\\\'; foreach ($directoryList as $directory) { $this->replaceInFiles(sprintf($listPeerCommand, $directory), 'abstract class \\(.*\\)Peer', '& extends ' . $separator . implode($separator, $this->peerBaseParts)); $this->replaceInFiles(sprintf($listQueryCommand, $directory), 'extends ModelCriteria', 'extends ' . $separator . implode($separator, $this->queryBaseParts)); $this->replaceInFiles(sprintf($listModelCommand, $directory), 'extends BaseObject', 'extends ' . $separator . implode($separator, $this->modelBaseParts)); } }
public function testAddJoinedDataToResultArrayAddsArrayForPluralRelation() { $data = []; $relation = 'Widget'; $relationType = RelationMap::ONE_TO_MANY; $testUtility = new TestUtility(); $widgetData = ['WidgetId' => 3392]; $widgetMock = $this->getMock(PropelSOAModel::class, ['toArray']); $widgetMock->expects($this->once())->method('toArray')->with($this->equalTo(BasePeer::TYPE_PHPNAME), $this->equalTo(false))->willReturn($widgetData); $modelMock = $this->getMock(PropelSOAModel::class, ['getWidgets']); $modelMock->expects($this->once())->method('getWidgets')->willReturn($testUtility->convertToCollection([$widgetMock])); $relationMapMock = $this->getMock(RelationMap::class, ['getType'], [], '', false); $relationMapMock->expects($this->any())->method('getType')->willReturn($relationType); $peer = new PropelSOAPeer(); $peer->oneToOneRelations = []; $classInfoMock = $this->getMock(SymfonyClassInfo::class, ['parseClassPath', 'getClassPath']); $classInfoMock->expects($this->any())->method('parseClassPath')->with($this->equalTo(get_class($modelMock))); $classInfoMock->expects($this->any())->method('getClassPath')->with($this->equalTo('peer'))->willReturn(PropelSOAPeer::class); Factory::injectObject(PropelSOAPeer::class, $peer); Factory::injectObject(SymfonyClassInfo::class, $classInfoMock); $join = new Join(); $join->joinList = []; $join->relation = $relation; $testUtility->setProtectedProperty($join, 'relationMap', $relationMapMock); $join->addJoinedDataToResultArray($modelMock, $data); $this->assertEquals($data, ['Widgets' => [$widgetData]]); }
public function testCreateNewObjectProperlySuppliesConstructorParametersWhenNotMocked() { $propertyValue = 'Hello World!'; $helperObject = Factory::createNewObject(HelperObject::class, [$propertyValue]); $this->assertEquals($propertyValue, $helperObject->property); }
/** * This constructor will set us up to store our question text in MongoDB. */ public function __construct() { $questionDocumentProperty = Factory::createNewObject(DocumentProperty::class, ['question', 'getQuestion', 'setQuestion', $this]); $this->documentProperties = [$questionDocumentProperty]; }
/** * This constructor will set up the answer as a document property to be stored in Mongo. */ public function __construct() { $answerDocumentProperty = Factory::createNewObject(DocumentProperty::class, ['answer', 'getAnswer', 'setAnswer', $this]); $this->documentProperties = [$answerDocumentProperty]; }
public function testExecuteAppendsGeneratedCode() { $baseUrl = 'http://site.com'; $controllerClass = 'MyNamespace/ProjectBundle/Controller/DataController'; $inputMock = $this->getMock(ArrayInput::class, ['getArgument'], [], '', false); $inputMock->expects($this->exactly(2))->method('getArgument')->will($this->returnValueMap([['url', $baseUrl], ['directory', 'js/src']])); $commandUtilityMock = $this->getMock(CommandUtility::class, ['exec', 'fileGetContents', 'filePutContents']); $commandUtilityMock->expects($this->at(0))->method('exec')->will($this->returnCallback(function ($command, &$output, &$returnVar = 0) { $output[] = 'src/MyNamespace/ProjectBundle/Controller'; })); $commandUtilityMock->expects($this->at(1))->method('exec')->will($this->returnCallback(function ($command, &$output, &$returnVar = 0) { $output[] = 'src/MyNamespace/ProjectBundle/Controller/DataController.php'; })); $commandUtilityMock->expects($this->exactly(3))->method('fileGetContents')->will($this->returnValueMap([['http://site.com/query', 'query'], ['http://site.com/collection', 'collection'], ['http://site.com/partial', 'partial']])); $commandUtilityMock->expects($this->once())->method('filePutContents')->with($this->equalTo('./js/src/generatedscript.js'), $this->equalTo('querypartialcollection')); $classInfoMock = $this->getMock(SymfonyClassInfo::class, ['getClassPath']); $classInfoMock->expects($this->once())->method('getClassPath')->with($this->equalTo('controller'))->will($this->returnValue($controllerClass)); $reflectionMock = $this->getMock(ReflectionClass::class, ['isAbstract', 'isSubclassOf', 'implementsInterface'], [], '', false); $reflectionMock->expects($this->once())->method('isAbstract')->will($this->returnValue(false)); $reflectionMock->expects($this->once())->method('isSubclassOf')->with($this->equalTo(ModelBasedServiceController::class))->will($this->returnValue(true)); $reflectionMock->expects($this->exactly(2))->method('implementsInterface')->will($this->returnValueMap([[Collectionable::class, true], [ClientExtendable::class, true]])); $routerParams = ['namespace' => 'MyNamespace', 'bundle' => 'Project', 'entity' => 'Data']; $routerMock = $this->getMock(stdClass::class, ['generate']); $routerMock->expects($this->exactly(3))->method('generate')->will($this->returnValueMap([['propelsoa_generatequery_route', $routerParams, '/query'], ['propelsoa_generatecollection_route', $routerParams, '/collection'], ['propelsoa_generatepartialobject_route', $routerParams, '/partial']])); $containerMock = $this->getMock(stdClass::class, ['get']); $containerMock->expects($this->once())->method('get')->with($this->equalTo('router'))->will($this->returnValue($routerMock)); Factory::injectObject(CommandUtility::class, $commandUtilityMock); Factory::injectObject(SymfonyClassInfo::class, $classInfoMock); Factory::injectObject(ReflectionClass::class, $reflectionMock); $command = $this->getMock(GenerateJSClassesCommandMock::class, ['getContainer']); $command->expects($this->once())->method('getContainer')->will($this->returnValue($containerMock)); $command->publicExecute($inputMock, new NullOutput()); }
/** * This method will make sure the actual question text will get sent to Mongo for us to remember. * * @param Request $request * @param Question $question */ protected function afterSave(Question $question) { $request = $this->getRequest(); $post = json_decode($request->getContent()); $question->setQuestion($post->QuestionText); $question->save(); // Let the support team know that a customer has a question $newQuestionNotification = Factory::createNewObject(NewQuestion::class, [$question]); $newQuestionNotification->send(); return $question; }
/** * This method will send the notification out to allow someone to convert from a signup to a * billing client for the training system. * * @param Signup $signup */ protected function notify(Signup $signup) { $signupNotification = Factory::createNewObject(SignupConfirmation::class, [$signup]); $signupNotification->send(); }
/** * This method will get our relation key pluralized. * * @return string */ protected function getPluralRelationName() { if (isset($this->pluralRelationName)) { return $this->pluralRelationName; } $pluralizer = Factory::createNewObject(StandardEnglishPluralizer::class); $this->pluralRelationName = $pluralizer->getPluralForm($this->relation); return $this->pluralRelationName; }