/** * Register a new Module * * @return Http\Response * @throws Exception\InvalidDataException * @throws Exception\RepositoryException */ public function addAction() { if (!$this->zfcUserAuthentication()->hasIdentity()) { return $this->redirect()->toRoute('zfcuser/login'); } $request = $this->getRequest(); if (!$request->isPost()) { throw Exception\InvalidDataException::fromInvalidRequest('Something went wrong with the post values of the request...', $this->getRequest()); } $postParams = $request->getPost(); $repo = $postParams->get('repo'); $owner = $postParams->get('owner'); $repository = $this->repositoryRetriever->getUserRepositoryMetadata($owner, $repo); if (!$repository instanceof \stdClass) { throw Exception\RepositoryException::fromNotFoundRepository('Not able to fetch the repository from GitHub due to an unknown error.', $owner, $repo); } if ($repository->fork || !$repository->permissions->push) { throw Exception\RepositoryException::fromInsufficientPermissions('You have no permission to add this module. The reason might be that you are neither the owner nor a collaborator of this repository.', $repository->full_name, ['pushAccess', 'noFork']); } if (!$this->moduleService->isModule($repository)) { throw Exception\RepositoryException::fromNonModuleRepository($repository->name . ' is not a Zend Framework Module', $repository->full_name); } $module = $this->moduleService->register($repository); $this->flashMessenger()->addSuccessMessage($module->getName() . ' has been added to ZF Modules'); return $this->redirect()->toRoute('zfcuser'); }
/** * @dataProvider providerIsModuleReturnsTrueIfResultCountIsGreaterThanZero * * @param bool $isModule * @param array $data */ public function testIsModuleReturnValueDependsOnTotalCountInResponseBody($isModule, $data) { $moduleMapper = $this->getMockBuilder(Mapper\Module::class)->disableOriginalConstructor()->getMock(); $repository = $this->repository(); $response = $this->getMockBuilder(Http\Response::class)->getMock(); $response->expects($this->once())->method('getBody')->willReturn(json_encode($data)); $httpClient = $this->getMockBuilder(HttpClient::class)->getMock(); $httpClient->expects($this->once())->method('request')->willReturn($response); $githubClient = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock(); $githubClient->expects($this->once())->method('getHttpClient')->willReturn($httpClient); $service = new Service\Module($moduleMapper, $githubClient); $this->assertSame($isModule, $service->isModule($repository)); }