1<?php 2/* 3This program is free software; you can redistribute it and/or 4modify it under the terms of the GNU General Public License 5version 2 as published by the Free Software Foundation. 6 7This program is distributed in the hope that it will be useful, 8but WITHOUT ANY WARRANTY; without even the implied warranty of 9MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10GNU General Public License for more details. 11 12You should have received a copy of the GNU General Public License along 13with this program; if not, write to the Free Software Foundation, Inc., 1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 15*/ 16 17namespace Fossology\Lib\Application; 18 19function time() 20{ 21 return 1535371200; 22} 23 24/** 25 * @class RepositoryApiTest 26 * @brief Test for RepositoryApi 27 */ 28class RepositoryApiTest extends \PHPUnit\Framework\TestCase 29{ 30 /** @var CurlRequest $mockCurlRequest 31 * CurlRequest object for testing */ 32 private $mockCurlRequest; 33 34 /** 35 * @brief One time setup for test 36 * 37 * Mock the CurlRequest class and set mockCurlRequest variable 38 * @see PHPUnit::Framework::TestCase::setUp() 39 */ 40 protected function setUp() 41 { 42 $this->mockCurlRequest = \Mockery::mock('CurlRequest'); 43 44 $this->mockCurlRequest->shouldReceive('setOptions')->once()->with(array( 45 CURLOPT_HEADER => true, 46 CURLOPT_RETURNTRANSFER => true, 47 CURLOPT_HTTPHEADER => array('User-Agent: fossology'), 48 CURLOPT_TIMEOUT => 2, 49 )); 50 $this->mockCurlRequest->shouldReceive('execute')->once() 51 ->andReturn('HEADER{"key": "value"}'); 52 $this->mockCurlRequest->shouldReceive('getInfo')->once() 53 ->with(CURLINFO_HEADER_SIZE)->andReturn(6); 54 $this->mockCurlRequest->shouldReceive('close')->once(); 55 } 56 57 /** 58 * @brief Tear down mock objects 59 * @see PHPUnit::Framework::TestCase::tearDown() 60 */ 61 public function tearDown() 62 { 63 \Mockery::close(); 64 } 65 66 /** 67 * @brief Test for RepositoryApi::getLatestRelease() 68 * @test 69 * -# Mock CurlRequestService object and pass to RepositoryApi 70 * -# Get the result of RepositoryApi::getLatestRelease() 71 * -# Check if you receive array `(key => value)` 72 */ 73 public function testGetLatestRelease() 74 { 75 $mockCurlRequestServer = \Mockery::mock('CurlRequestService'); 76 $mockCurlRequestServer->shouldReceive('create')->once() 77 ->with('https://api.github.com/repos/fossology/fossology/releases/latest') 78 ->andReturn($this->mockCurlRequest); 79 $repositoryApi = new RepositoryApi($mockCurlRequestServer); 80 81 $this->assertEquals(array('key' => 'value'), $repositoryApi->getLatestRelease()); 82 } 83 84 /** 85 * @brief Test for RepositoryApi::getCommitsOfLastDays() 86 * @test 87 * -# Mock CurlRequestService object and pass to RepositoryApi 88 * -# Get the result of RepositoryApi::getCommitsOfLastDays() 89 * -# Check if you receive array `(key => value)` 90 */ 91 public function testGetCommitsOfLastDays() 92 { 93 $mockCurlRequestServer = \Mockery::mock('CurlRequestServer'); 94 $mockCurlRequestServer->shouldReceive('create')->once() 95 ->with('https://api.github.com/repos/fossology/fossology/commits?since=2018-06-28T12:00:00Z') 96 ->andReturn($this->mockCurlRequest); 97 $repositoryApi = new RepositoryApi($mockCurlRequestServer); 98 99 $this->assertEquals(array('key' => 'value'), $repositoryApi->getCommitsOfLastDays(60)); 100 } 101} 102