Skip to content

Commit

Permalink
Test API using phpunit and Guzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
korelstar committed Mar 12, 2020
1 parent 65387c0 commit 81429c5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- master
- stable*
pull_request:

jobs:
Expand Down Expand Up @@ -66,3 +67,4 @@ jobs:
uses: korelstar/phplint-problem-matcher@master
- name: lint PHP
run: make -k lint-php

34 changes: 34 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,34 @@
name: Lint
on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master
- name: Set up php${{ matrix.php-versions }}
uses: shivammathur/setup-php@v1
- name: Install Dependencies
run: composer install --prefer-dist
- name: Setup Nextcloud
run: |
mysql -u root -proot -e "CREATE DATABASE oc_autotest;"
mysql -u root -proot -e "CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY '';"
mysql -u root -proot -e "GRANT ALL ON oc_autotest.* TO 'oc_autotest'@'localhost';"
cd ..
git clone https://github.com/nextcloud/server.git --recursive --depth 1 -b master server
cp -r notes server/apps/
php server/occ maintenance:install --database-name oc_autotest --database-user oc_autotest --admin-user admin --admin-pass admin --database mysql --database-pass=''
OC_PASS=test php server/occ user:add --password-from-env --display-name="Test" test
- name: Setup Notes
run: |
php ../server/occ app:enable notes
php ../server/occ app:check-code notes
- name: Start Nextcloud server
run: "php -S localhost:8080 -t ../server/ &"
- name: Test API
run: vendor/bin/phpunit --testdox tests/api/

2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -3,6 +3,8 @@
"christophwurst/nextcloud": "^16.0",
"squizlabs/php_codesniffer": "3.*",
"phan/phan": "^2.0",
"phpunit/phpunit": "^9.0",
"guzzlehttp/guzzle": "^6.5",
"staabm/annotate-pull-request-from-checkstyle": "^1.0"
}
}
40 changes: 40 additions & 0 deletions tests/api/APIv02Test.php
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class APIv02Test extends TestCase {
private $http;

protected function setUp() : void {
$this->http = new GuzzleHttp\Client([
'base_uri' => 'http://localhost:8080/index.php/apps/notes/api/v0.2/',
'auth' => ['test', 'test'],
'http_errors' => false,
]);
}

public function testGetNotes() : void {
$response = $this->http->request('GET', 'notes');
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(
"application/json; charset=utf-8",
$response->getHeaders()["Content-Type"][0]
);
// TODO test example notes
// $userAgent = json_decode($response->getBody())->{"..."};
}

public function testCreateNote() : void {
// TODO
}

public function testGetNonExistingNoteFails() : void {
$response = $this->http->request('GET', 'notes/1');
$this->assertEquals(404, $response->getStatusCode());
}

protected function tearDown() : void {
$this->http = null;
}
}

0 comments on commit 81429c5

Please sign in to comment.