Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
This commit is contained in:
Julien Veyssier 2022-12-13 21:32:37 +01:00
Родитель b90a9cda4a
Коммит 6d25282f91
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4141FEE162030638
5 изменённых файлов: 173 добавлений и 2 удалений

74
.github/workflows/phpunit.yml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,74 @@
name: PHPUnit
on:
pull_request:
paths:
- .github/workflows/phpunit.yml
- appinfo/**
- composer.*
- lib/**
- templates/**
- tests/**
push:
branches:
- master
- stable*
- test
paths:
- .github/workflows/phpunit.yml
- appinfo/**
- composer.*
- lib/**
- templates/**
- tests/**
env:
APP_NAME: integration_github
jobs:
php:
runs-on: ubuntu-latest
strategy:
# do not stop on another job's failure
fail-fast: false
matrix:
php-versions: ['7.4', '8.0', '8.1']
databases: ['sqlite']
server-versions: ['master']
name: php${{ matrix.php-versions }}-${{ matrix.databases }}-${{ matrix.server-versions }}
steps:
- name: Checkout server
uses: actions/checkout@v2
with:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}
submodules: true
- name: Checkout app
uses: actions/checkout@v2
with:
path: apps/${{ env.APP_NAME }}
- name: Set up php ${{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: phpunit
extensions: mbstring, iconv, fileinfo, intl, sqlite, pdo_sqlite, gd, zip
- name: Set up PHPUnit
working-directory: apps/${{ env.APP_NAME }}
run: composer i
- name: Set up Nextcloud
run: |
mkdir data
./occ maintenance:install --verbose --database=sqlite --admin-user admin --admin-pass admin
./occ app:enable --force ${{ env.APP_NAME }}
- name: PHPUnit
working-directory: apps/${{ env.APP_NAME }}
run: phpunit -c tests/phpunit.xml

Просмотреть файл

@ -222,7 +222,7 @@ class GithubAPIService {
* @param int $limit
* @return array [perPage, page, leftPadding]
*/
private function getGitHubPaginationValues(int $offset = 0, int $limit = 5): array {
public static function getGitHubPaginationValues(int $offset = 0, int $limit = 5): array {
// compute pagination values
// indexes offset => offset + limit
if (($offset % $limit) === 0) {
@ -255,7 +255,7 @@ class GithubAPIService {
* @return array request result
*/
public function searchIssues(string $userId, string $query, int $offset = 0, int $limit = 5): array {
[$perPage, $page, $leftPadding] = $this->getGitHubPaginationValues($offset, $limit);
[$perPage, $page, $leftPadding] = self::getGitHubPaginationValues($offset, $limit);
$params = [
'q' => $query,
'order' => 'desc',

6
tests/bootstrap.php Normal file
Просмотреть файл

@ -0,0 +1,6 @@
<?php
require_once __DIR__ . '/../../../tests/bootstrap.php';
\OC_App::loadApp(OCA\Github\AppInfo\Application::APP_ID);
OC_Hook::clear();

24
tests/phpunit.xml Normal file
Просмотреть файл

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="bootstrap.php"
verbose="true"
convertDeprecationsToExceptions="true"
timeoutForSmallTests="900"
timeoutForMediumTests="900"
timeoutForLargeTests="900"
>
<testsuite name='GitHub integration App Tests'>
<directory suffix='Test.php'>.</directory>
</testsuite>
<!-- filters for code coverage -->
<filter>
<whitelist>
<directory suffix=".php">../appinfo</directory>
<directory suffix=".php">../lib</directory>
</whitelist>
</filter>
<logging>
<!-- and this is where your report will be written -->
<log type="coverage-clover" target="./clover.xml"/>
</logging>
</phpunit>

Просмотреть файл

@ -0,0 +1,67 @@
<?php
namespace OCA\Github\Tests;
use OCA\Github\AppInfo\Application;
use OCA\Github\Service\GithubAPIService;
class GithubAPIServiceTest extends \PHPUnit\Framework\TestCase {
public function testDummy() {
$app = new Application();
$this->assertEquals('integration_github', $app::APP_ID);
}
public function testPaginationConversion() {
$expecteds = [
// offset, limit => perPage, page, leftPadding
[[0, 5], [5, 1, 0]],
[[0, 10], [10, 1, 0]],
[[2, 5], [7, 1, 2]],
[[2, 10], [12, 1, 2]],
[[12, 2], [2, 7, 0]],
[[12, 3], [3, 5, 0]],
[[12, 4], [4, 4, 0]],
[[12, 5], [6, 3, 0]],
[[12, 6], [6, 3, 0]],
[[12, 7], [10, 2, 2]],
];
foreach ($expecteds as $expected) {
$params = $expected[0];
$offset = $params[0];
$limit = $params[1];
$expectedResults = $expected[1];
$results = GithubAPIService::getGitHubPaginationValues($offset, $limit);
$this->assertEquals($expectedResults, $results);
}
foreach (range(0, 100) as $offset) {
foreach (range(1, 100) as $limit) {
[$perPage, $page, $leftPadding] = GithubAPIService::getGitHubPaginationValues($offset, $limit);
// what do we want?
$firstRequestedIndex = $offset;
$lastRequestedIndex = $offset + $limit - 1;
// what do we get?
// page number start at 1
$firstObtainedIndex = ($page - 1) * $perPage;
$lastObtainedIndex = $firstObtainedIndex + ($perPage - 1);
// check requested indexes are in the obtained page
$this->assertTrue($firstRequestedIndex >= $firstObtainedIndex);
$this->assertTrue($lastRequestedIndex <= $lastObtainedIndex);
// page size can't be smaller than the number of elements we want
$this->assertTrue($perPage >= $limit);
// no negative padding
$this->assertTrue($leftPadding >= 0);
// padding can't be bigger than the number of extra elements we got
$this->assertTrue($leftPadding <= $perPage - $limit);
}
}
}
}