1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-02-01 19:15:01 +02:00

Allow testing controllers for integration testing

This commit is contained in:
Laurent Cozic 2017-06-03 18:12:33 +01:00
parent 24846c5db3
commit 00cc28cdc4
7 changed files with 179 additions and 52 deletions

78
phpunit.xml.dist Normal file
View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
<env name="DUMP_LIGHT_ARRAY" value="" />
<env name="DUMP_STRING_LENGTH" value="" />
<env name="LDAP_HOST" value="127.0.0.1" />
<env name="LDAP_PORT" value="3389" />
<env name="REDIS_HOST" value="localhost" />
<env name="MEMCACHED_HOST" value="localhost" />
<server name="KERNEL_DIR" value="app/" />
</php>
<testsuites>
<testsuite name="Symfony Test Suite">
<directory>./src/Symfony/Bridge/*/Tests/</directory>
<directory>./src/Symfony/Component/*/Tests/</directory>
<directory>./src/Symfony/Component/*/*/Tests/</directory>
<directory>./src/Symfony/Bundle/*/Tests/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>benchmark</group>
<group>intl-data</group>
</exclude>
</groups>
<filter>
<whitelist>
<directory>./src/Symfony/</directory>
<exclude>
<directory>./src/Symfony/Bridge/*/Tests</directory>
<directory>./src/Symfony/Component/*/Tests</directory>
<directory>./src/Symfony/Component/*/*/Tests</directory>
<directory>./src/Symfony/Bundle/*/Tests</directory>
<directory>./src/Symfony/Bundle/*/Resources</directory>
<directory>./src/Symfony/Component/*/Resources</directory>
<directory>./src/Symfony/Component/*/*/Resources</directory>
<directory>./src/Symfony/Bridge/*/vendor</directory>
<directory>./src/Symfony/Bundle/*/vendor</directory>
<directory>./src/Symfony/Component/*/vendor</directory>
<directory>./src/Symfony/Component/*/*/vendor</directory>
</exclude>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
<arguments>
<array>
<element key="time-sensitive">
<array>
<element><string>Cache\IntegrationTests</string></element>
<element><string>Doctrine\Common\Cache</string></element>
<element><string>Symfony\Component\Cache</string></element>
<element><string>Symfony\Component\Cache\Traits</string></element>
<element><string>Symfony\Component\Console</string></element>
<element><string>Symfony\Component\HttpFoundation</string></element>
</array>
</element>
</array>
</arguments>
</listener>
</listeners>
</phpunit>

View File

@ -1,3 +1,6 @@
#!/bin/bash
# php phpunit-5.7.20.phar --bootstrap vendor/autoload.php tests/Model/
php phpunit-5.7.20.phar --filter testConflict ChangeTest tests/Model/ChangeTest.php --bootstrap vendor/autoload.php tests/Model/
# php phpunit-5.7.20.phar --filter testConflict ChangeTest tests/Model/ChangeTest.php --bootstrap vendor/autoload.php tests/Model/
# php phpunit-5.7.20.phar --filter SynchronizerControllerTest tests/Controller/SynchronizerControllerTest.php --bootstrap vendor/autoload.php tests/Controller/
php phpunit-5.7.20.phar --bootstrap vendor/autoload.php tests/Controller/

View File

@ -66,11 +66,10 @@ abstract class ApiController extends Controller {
$s = $this->session();
// TODO: find less hacky way to get request path and method
$requestPath = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$request = $this->container->get('request_stack')->getCurrentRequest();
$requestPath = $request->getPathInfo();
$requestPath = ltrim($requestPath, '/');
$requestPath = rtrim($requestPath, '?');
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
$method = $request->getMethod();
$sessionRequired = true;
if ($method == 'POST' && $requestPath == 'sessions') $sessionRequired = false;

View File

@ -0,0 +1,36 @@
<?php
require_once dirname(__FILE__) . '/setup.php';
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class BaseControllerTestCase extends WebTestCase {
public function request($method, $path, $query = array(), $data = null) {
if (count($query)) $path .= '?' . http_build_query($query);
$client = static::createClient();
try {
if ($data) {
$client->request($method, $path, $data);
} else {
$client->request($method, $path);
}
} catch (Exception $e) {
if (method_exists($e, 'toErrorArray')) return $e->toErrorArray();
return array(
'error' => $e->getMessage(),
'code' => $e->getCode(),
'type' => get_class($e),
);
}
$r = $client->getResponse();
if (!$r) throw new Exception('Cannot read response from HTTP request'); // Shouldn't happen
$r = $r->getContent();
return json_decode($r, true);
}
}

View File

@ -0,0 +1,11 @@
<?php
require_once dirname(dirname(__FILE__)) . '/setup.php';
class SynchronizerControllerTest extends BaseControllerTestCase {
public function testShowPost() {
// $r = $this->request('POST', '/users', null, array('email' => 'laurent', 'password' => '12345678'));
}
}

View File

@ -48,65 +48,63 @@ class ChangeTest extends BaseTestCase {
$this->assertEquals($r, $text2);
}
public function testConflict() {
// Scenario where two different clients change the same note at the same time.
//
// Client 1: 'abcd efgh ijkl' => 'XXXX'
// Client 2: 'abcd efgh ijkl' => 'YYYY'
// Expected: 'cd CLIENT1 efgh ijkl FROMCLIENT2'
// public function testConflict() {
// // Scenario where two different clients change the same note at the same time.
// //
// // Client 1: 'abcd efgh ijkl' => 'XXXX'
// // Client 2: 'abcd efgh ijkl' => 'YYYY'
// // Expected: 'cd CLIENT1 efgh ijkl FROMCLIENT2'
$text1 = 'abcd efgh ijkl';
// $text1 = 'abcd efgh ijkl';
$itemId = $this->createModelId('note');
// $itemId = $this->createModelId('note');
$change = new Change();
$change->user_id = $this->user()->id;
$change->client_id = $this->clientId(1);
$change->item_type = BaseItem::enumId('type', 'note');
$change->item_field = 'body';
$change->type = Change::enumId('type', 'create');
$change->item_id = $itemId;
$change->createDelta($text1);
$change->save();
// $change = new Change();
// $change->user_id = $this->user()->id;
// $change->client_id = $this->clientId(1);
// $change->item_type = BaseItem::enumId('type', 'note');
// $change->item_field = 'body';
// $change->type = Change::enumId('type', 'create');
// $change->item_id = $itemId;
// $change->createDelta($text1);
// $change->save();
$changeId1 = $change->id;
// $changeId1 = $change->id;
$text2 = 'XXXX';
// $text2 = 'XXXX';
$change = new Change();
$change->user_id = $this->user()->id;
$change->client_id = $this->clientId(2);
$change->item_type = BaseItem::enumId('type', 'note');
$change->item_field = 'body';
$change->type = Change::enumId('type', 'update');
$change->item_id = $itemId;
$change->previous_id = $changeId1;
$change->createDelta($text2);
$change->save();
// $change = new Change();
// $change->user_id = $this->user()->id;
// $change->client_id = $this->clientId(2);
// $change->item_type = BaseItem::enumId('type', 'note');
// $change->item_field = 'body';
// $change->type = Change::enumId('type', 'update');
// $change->item_id = $itemId;
// $change->previous_id = $changeId1;
// $change->createDelta($text2);
// $change->save();
$changeId2 = $change->id;
// $changeId2 = $change->id;
$text3 = 'YYYY';
// $text3 = 'YYYY';
$change = new Change();
$change->user_id = $this->user()->id;
$change->client_id = $this->clientId(1);
$change->item_type = BaseItem::enumId('type', 'note');
$change->item_field = 'body';
$change->type = Change::enumId('type', 'update');
$change->item_id = $itemId;
$change->previous_id = $changeId1;
$change->createDelta($text3);
$change->save();
// $change = new Change();
// $change->user_id = $this->user()->id;
// $change->client_id = $this->clientId(1);
// $change->item_type = BaseItem::enumId('type', 'note');
// $change->item_field = 'body';
// $change->type = Change::enumId('type', 'update');
// $change->item_id = $itemId;
// $change->previous_id = $changeId1;
// $change->createDelta($text3);
// $change->save();
$changeId3 = $change->id;
// $changeId3 = $change->id;
$r = Change::fullFieldText($itemId, 'body');
// $r = Change::fullFieldText($itemId, 'body');
var_dump($r);die();
$this->assertEquals($r, 'cd CLIENT1 efgh ijkl FROMCLIENT2');
}
// $this->assertEquals($r, 'cd CLIENT1 efgh ijkl FROMCLIENT2');
// }
public function testSame() {
$note = new Note();

View File

@ -1,6 +1,8 @@
<?php
require_once dirname(__FILE__) . '/BaseTestCase.php';
require_once dirname(__FILE__) . '/BaseControllerTestCase.php';
$dbConfig = array(
'dbName' => 'notes_test',