Page MenuHomeDevCentral

No OneTemporary

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..15f50c2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+# PHPUnit
+.phpunit.result.cache
+.phpunit.cache/
diff --git a/workspaces/tests/phpunit.xml b/phpunit.xml
similarity index 54%
copy from workspaces/tests/phpunit.xml
copy to phpunit.xml
index af4512f..9c444d4 100644
--- a/workspaces/tests/phpunit.xml
+++ b/phpunit.xml
@@ -1,26 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
<phpunit
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
-
- forceCoversAnnotation="true"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
+ bootstrap="vendor/autoload.php"
+ displayDetailsOnTestsThatTriggerDeprecations="true"
+ displayDetailsOnPhpunitDeprecations="true"
+ cacheDirectory=".phpunit.cache"
+ stopOnFailure="false"
>
+
+ <testsuites>
+ <testsuite name="Unit tests for waystone/workspaces">
+ <directory suffix="Test.php">workspaces/tests/</directory>
+ </testsuite>
+ </testsuites>
+
+ <source>
+ <include>
+ <directory suffix=".php">workspaces/src/</directory>
+ </include>
+ </source>
+
<php>
<!-- MySQL -->
<const name="UNITTESTING_MYSQL_HOST" value="localhost" />
<const name="UNITTESTING_MYSQL_USERNAME" value="root" />
<const name="UNITTESTING_MYSQL_PASSWORD" value="" />
<const name="UNITTESTING_MYSQL_DATABASE" value="obsidian" />
<const name="UNITTESTING_MYSQL_TABLE" value="collections_unittesting" />
<!-- SQLite -->
<const name="UNITTESTING_SQLITE_FILE" value="/tmp/collections.db" />
<!-- MongoDB -->
<const name="UNITTESTING_MONGODB_HOST" value="localhost" />
<const name="UNITTESTING_MONGODB_PORT" value="27017" />
<const name="UNITTESTING_MONGODB_SSL" value="true" />
<!-- FilesCollection -->
<const name="UNITTESTING_FILESCOLLECTION_PATH" value="/tmp/obsidiancollections" />
</php>
</phpunit>
diff --git a/workspaces/tests/Engines/Collection/BookDocument.php b/workspaces/tests/Engines/Collection/BookDocument.php
new file mode 100644
index 0000000..5b03b20
--- /dev/null
+++ b/workspaces/tests/Engines/Collection/BookDocument.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Waystone\Workspaces\Tests\Engines\Collection;
+
+use CollectionDocument;
+
+require_once(__DIR__ . '/../../../src/includes/autoload.php');
+
+/**
+ * A CollectionDocument class, for our tests.
+ */
+class BookDocument extends CollectionDocument {
+ /**
+ * @var string The book title
+ */
+ public $title;
+
+ /**
+ * @var string The book author
+ */
+ public $author;
+
+ /**
+ * Initializes a new instance of the BookDocument object
+ */
+ public function __construct ($author = null, $title = null) {
+ if ($title !== null) $this->title = $title;
+ if ($author !== null) $this->author = $author;
+ }
+}
diff --git a/workspaces/tests/includes/collection/CRUDTestTrait.php b/workspaces/tests/Engines/Collection/CRUDTestTrait.php
similarity index 80%
rename from workspaces/tests/includes/collection/CRUDTestTrait.php
rename to workspaces/tests/Engines/Collection/CRUDTestTrait.php
index f5eb4ce..10efea4 100644
--- a/workspaces/tests/includes/collection/CRUDTestTrait.php
+++ b/workspaces/tests/Engines/Collection/CRUDTestTrait.php
@@ -1,235 +1,215 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* CRUD features tests for each Collection class.
*
* @package ObsidianWorkspaces
* @subpackage Tests
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
-require_once('../src/includes/autoload.php');
+namespace Waystone\Workspaces\Tests\Engines\Collection;
-/**
- * A CollectionDocument class, for our tests.
- */
-class BookDocument extends CollectionDocument {
- /**
- * @var string The book title
- */
- public $title;
-
- /**
- * @var string The book author
- */
- public $author;
-
- /**
- * Initializes a new instance of the BookDocument object
- */
- public function __construct ($author = null, $title = null) {
- if ($title !== null) $this->title = $title;
- if ($author !== null) $this->author = $author;
- }
-}
+use CollectionDocument;
/**
* The tests for our basic, non storage engine specific CRUD features
*
* For coverage purposes, it requires a coversDefaultClass annotation in the classes using this trait.
*/
trait CRUDTestTrait {
/**
* @var BookDocument The document to test every CRUD feature
*/
protected $redBook;
/**
* @var BookDocument A second document to test Set in two scenarii
*/
protected $blueBook;
/**
* Initializes two documents for the tests
*/
public function initializeDocuments() {
$this->blueBook = new BookDocument('Isaac Asimov', 'Foundation');
$this->redBook = new BookDocument('Iain Banks', 'Matter'); //M. will be added in update test
$this->redBook->id = 'redBook';
}
/**
* Tests if the constructor correctly initializes the id property
*
* @covers ::_construct()
*/
public function testId () {
$this->assertEquals('quux', $this->collection->id, "The collection constructor should have initialized the Collection::id property.");
}
/**
* Tests CRUD methods
*
* @covers ::add
* @covers ::exists
* @covers ::update
* @covers ::get
* @covers ::set
* @covers ::delete
* @covers ::count
* @covers ::getAll
*/
public function testCRUD () {
global $Config;
$Config = static::getConfig();
//::count
$this->assertEquals(
0, $this->collection->count(),
"The test collection isn't empty. Check the last test run cleaned correctly the resources."
);
//::add
$this->collection->add($this->redBook);
$this->assertNotEmpty(
$this->redBook->id,
"After a document has been added, the id has been deleted."
);
$this->assertEquals(
'redBook',
$this->redBook->id,
"After a document has been added, the id has been modified."
);
//:ccount
$this->assertEquals(1, $this->collection->count());
//::exists
$this->assertFalse(
$this->collection->exists($this->blueBook),
"A non added document has been marked existing."
);
$this->assertTrue(
$this->collection->exists($this->redBook),
"An added document hasn't been found as existing."
);
//::update
$this->redBook->author = 'Iain M. Banks';
$this->collection->update($this->redBook);
$this->assertEquals(
'redBook',
$this->redBook->id,
"The document ID has been modified during an update operation. It should stay the same. Old id: redBook. New id: " . $this->redBook->id
);
//::count
$this->assertEquals(1, $this->collection->count());
//::get - when our collection uses the generic CollectionDocument class
$newDocument = $this->collection->get($this->redBook->id);
- $this->assertInstanceOf('CollectionDocument', $newDocument);
- $this->assertNotInstanceOf('BookDocument', $newDocument);
+ $this->assertInstanceOf(CollectionDocument::class, $newDocument);
+ $this->assertNotInstanceOf(BookDocument::class, $newDocument);
//::set - when our collection uses a proper CollectionDocument descendant
- $this->collection->documentType = 'BookDocument';
+ $this->collection->documentType = BookDocument::class;
$newBook = $this->collection->get($this->redBook->id);
- $this->assertInstanceOf('BookDocument', $newBook);
- $this->assertObjectHasAttribute('title', $newBook);
- $this->assertObjectHasAttribute('author', $newBook);
- $this->assertObjectHasAttribute('id', $newBook);
+ $this->assertInstanceOf(BookDocument::class, $newBook);
+ $this->assertObjectHasProperty('title', $newBook);
+ $this->assertObjectHasProperty('author', $newBook);
+ $this->assertObjectHasProperty('id', $newBook);
$this->assertEquals('Matter', $newBook->title);
$this->assertEquals('Iain M. Banks', $newBook->author);
$this->assertEquals('redBook', $newBook->id);
//::set - an existing document as parameter
$previousId = $this->redBook->id;
$this->collection->set($this->redBook);
$this->assertEquals(
$previousId,
$this->redBook->id,
"The document ID has been modified during a set operation on an already added dcoument. It should stay the same. Old id: $previousId. New id: " . $this->redBook->id
);
//::count
$this->assertEquals(1, $this->collection->count());
//::set - a new document as parameter
$this->collection->set($this->blueBook);
$this->assertNotEmpty(
$this->blueBook->id,
"After a document has been added, the expected behavior is the id property is filled with the generated identifiant."
);
$this->assertTrue(
$this->collection->exists($this->blueBook),
"An added document with set hasn't been found as existing."
);
//::count
$this->assertEquals(2, $this->collection->count());
//::getAll
$documents = $this->collection->getAll();
$count = 0;
foreach ($documents as $document) {
switch ($document->id) {
case $this->blueBook->id:
- $this->assertInstanceOf('BookDocument', $document);
- $this->assertObjectHasAttribute('title', $document);
- $this->assertObjectHasAttribute('author', $document);
- $this->assertObjectHasAttribute('id', $document);
+ $this->assertInstanceOf(BookDocument::class, $document);
+ $this->assertObjectHasProperty('title', $document);
+ $this->assertObjectHasProperty('author', $document);
+ $this->assertObjectHasProperty('id', $document);
$this->assertEquals('Foundation', $document->title);
$this->assertEquals('Isaac Asimov', $document->author);
break;
case 'redBook':
- $this->assertInstanceOf('BookDocument', $document);
- $this->assertObjectHasAttribute('title', $document);
- $this->assertObjectHasAttribute('author', $document);
- $this->assertObjectHasAttribute('id', $document);
+ $this->assertInstanceOf(BookDocument::class, $document);
+ $this->assertObjectHasProperty('title', $document);
+ $this->assertObjectHasProperty('author', $document);
+ $this->assertObjectHasProperty('id', $document);
$this->assertEquals('Matter', $document->title);
$this->assertEquals('Iain M. Banks', $document->author);
break;
case '':
$this->fail("An object without id has been returned by the getAll method.");
break;
default:
$this->fail("A document with an id nor 'redBook', nor the blueBook generated id ('{$this->blueBook->id}') has been returned: $document->id.");
}
$count++;
}
$this->assertEquals(2, $count);
//::delete
$this->collection->delete($this->blueBook->id);
$this->assertFalse(
$this->collection->exists($this->blueBook),
"A deleted document has been marked existing."
);
$this->assertTrue(
$this->collection->exists($this->redBook),
"An unexpected document has been deleted."
);
//::count
$this->assertEquals(1, $this->collection->count());
//::delete, ::count
$this->collection->delete($this->redBook->id);
$this->assertEquals(0, $this->collection->count());
//::getAll
$documents = $this->collection->getAll();
- $this->assertCount(0, $documents, "We expected each collection document would have been deleted.");
+ $count = iterator_count($documents);
+ $this->assertEquals(0, $count, "We expected each collection document would have been deleted.");
}
}
diff --git a/workspaces/tests/includes/collection/FilesCollectionTest.php b/workspaces/tests/Engines/Collection/FilesCollectionTest.php
similarity index 66%
rename from workspaces/tests/includes/collection/FilesCollectionTest.php
rename to workspaces/tests/Engines/Collection/FilesCollectionTest.php
index 7dcc849..91752e8 100644
--- a/workspaces/tests/includes/collection/FilesCollectionTest.php
+++ b/workspaces/tests/Engines/Collection/FilesCollectionTest.php
@@ -1,193 +1,170 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Unit testing — FilesCollection class
*
* @package ObsidianWorkspaces
* @subpackage Tests
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
-require_once('CRUDTestTrait.php');
-require('../src/includes/GlobalFunctions.php');
+namespace Waystone\Workspaces\Tests\Engines\Collection;
+
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\TestCase;
+
+use FilesCollection;
+
+use Exception;
+
+require_once(__DIR__ . '/../../../src//includes/GlobalFunctions.php');
/**
* Tests FilesCollection class
- * @coversDefaultClass FilesCollection
*/
-class FilesCollectionTest extends PHPUnit_Framework_TestCase {
+#[CoversClass(FilesCollection::class)]
+class FilesCollectionTest extends TestCase {
+
/**
* @var string Our collection
*/
protected $collection;
/**
* Gets default configuration for this test
*
* @return array The configuration block
*/
protected static function getConfig () {
return [
'DocumentStorage' => [
'Type' => 'Files',
'Path' => UNITTESTING_FILESCOLLECTION_PATH
]
];
}
- /**
- * Initializes a new instance of the PHPUnit_Framework_TestCase class
- *
- * @param string $name The test case name
- */
- public function __construct (string $name = null) {
- parent::__construct($name);
-
+ protected function setUp () : void {
$this->initializeDocuments();
global $Config;
$Config = self::getConfig();
$this->collection = new FilesCollection('quux');
}
-
///
/// Specific tests for this particular Collection class
///
-
- /**
- * @covers FilesCollection::__construct
- */
public function testConstructor () {
global $Config;
$Config = self::getConfig();
$collection = new FilesCollection('quux');
$this->assertEquals('quux', $collection->id);
}
- /**
- * @covers FilesCollection::getCollectionPath
- * @covers FilesCollection::getDocumentPath
- */
public function testPaths () {
global $Config;
$Config = self::getConfig();
$expectedPath = UNITTESTING_FILESCOLLECTION_PATH
- . DIRECTORY_SEPARATOR
- . 'quux';
+ . DIRECTORY_SEPARATOR
+ . 'quux';
$this->assertEquals(
$expectedPath,
FilesCollection::getCollectionPath('quux')
);
$this->assertFileExists($expectedPath);
$expectedPath .= DIRECTORY_SEPARATOR
- . 'foo.json';
+ . 'foo.json';
$this->assertEquals(
$expectedPath,
$this->collection->getDocumentPath('foo')
);
}
- /**
- * @covers FilesCollection::getCollectionPath
- */
public function testConfigurationMissingException () {
//Note: @expectedException isn't allowed in PHPUnit to test the generic Exception class.
global $Config;
$oldConfigDocumentStorage = $Config['DocumentStorage'];
$Config['DocumentStorage'] = []; //Path isn't defined
- try {
- FilesCollection::getCollectionPath('quux');
- } catch (Exception $ex) {
- $Config['DocumentStorage'] = $oldConfigDocumentStorage;
- return;
- }
- $this->fail('An expected exception has not been raised.');
+
+ $this>$this->expectException(Exception::class);
+ FilesCollection::getCollectionPath('quux');
+
+ $Config['DocumentStorage'] = $oldConfigDocumentStorage;
}
- /**
- * @covers FilesCollection::getCollectionPath
- */
public function testCantCreateDirectoryException () {
- //Note: @expectedException isn't allowed in PHPUnit to test the generic Exception class.
-
global $Config;
$oldConfigDocumentStorage = $Config['DocumentStorage'];
$Config['DocumentStorage'] = [
'Type' => 'Files',
'Path' => '/root/obsidiancollections',
];
- try {
- FilesCollection::getCollectionPath('quux');
- } catch (Exception $ex) {
- $Config['DocumentStorage'] = $oldConfigDocumentStorage;
- return;
- }
- $this->fail("An expected exception has not been raised. If you're logged as root, you can safely delete /root/obsidiancollections folder and ignore this test. By the way, are you sure to trust a tests sequence creating and deleting files to run them as root?");
+
+ $this->expectException(Exception::class);
+ FilesCollection::getCollectionPath('quux');
+
+ $Config['DocumentStorage'] = $oldConfigDocumentStorage;
}
///
/// CRUD tests
///
use CRUDTestTrait;
- /**
- * @covers FilesCollection::add
- * @covers FilesCollection::update
- */
public function testFileContent () {
global $Config;
$Config = self::getConfig();
$book = new BookDocument('Iain Banks', 'Excession');
$book->id = 'greenBook';
$this->collection->add($book);
$filename = $this->collection->getDocumentPath('greenBook');
$this->assertJsonFileEqualsJsonFile(
'includes/collection/greenBook1.json',
$filename
);
$book->author = 'Iain M. Banks';
$this->collection->update($book);
$this->assertJsonFileEqualsJsonFile(
'includes/collection/greenBook2.json',
$filename
);
//Cleans up, so CRUD test starts with an empty collection
unlink(UNITTESTING_FILESCOLLECTION_PATH . '/quux/greenBook.json');
}
///
/// Cleanup
///
/**
* Tears down resources when tests are done
*/
- public static function tearDownAfterClass () {
+ public static function tearDownAfterClass () : void {
//Removes created directories
rmdir(UNITTESTING_FILESCOLLECTION_PATH . '/quux');
rmdir(UNITTESTING_FILESCOLLECTION_PATH);
}
}
diff --git a/workspaces/tests/includes/collection/MongoDBCollectionTest.php b/workspaces/tests/Engines/Collection/MongoDBCollectionTest.php
similarity index 75%
rename from workspaces/tests/includes/collection/MongoDBCollectionTest.php
rename to workspaces/tests/Engines/Collection/MongoDBCollectionTest.php
index f9d29e2..9ce8f62 100644
--- a/workspaces/tests/includes/collection/MongoDBCollectionTest.php
+++ b/workspaces/tests/Engines/Collection/MongoDBCollectionTest.php
@@ -1,63 +1,61 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Unit testing — MongoDBCollection class
*
* @package ObsidianWorkspaces
* @subpackage Tests
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
-require_once('CRUDTestTrait.php');
+namespace Waystone\Workspaces\Tests\Engines\Collection;
+
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\TestCase;
+
+use MongoDBCollection;
/**
* Tests MongoDBCollection class
- * @coversDefaultClass MongoDBCollection
*/
-class MongoDBCollectionTest extends PHPUnit_Framework_TestCase {
+#[CoversClass(MongoDBCollection::class)]
+class MongoDBCollectionTest extends TestCase {
/**
* @var string Our collection
*/
protected $collection;
/**
* Gets default configuration for this test
*
* @return array The configuration block
*/
public static function getConfig () {
return [
'DocumentStorage' => [
'Type' => 'MongoDB',
'Host' => UNITTESTING_MONGODB_HOST,
'Port' => UNITTESTING_MONGODB_PORT,
'SSL' => UNITTESTING_MONGODB_SSL ? [] : null
]
];
}
- /**
- * Initializes a new instance of the PHPUnit_Framework_TestCase class
- *
- * @param string $name The test case name
- */
- public function __construct (string $name = null) {
- parent::__construct($name);
-
+ public function setUp () : void {
global $Config;
$Config = static::getConfig();
$this->initializeDocuments();
$this->collection = new MongoDBCollection('quux');
}
use CRUDTestTrait;
}
diff --git a/workspaces/tests/includes/collection/MySQCollectionTest.php b/workspaces/tests/Engines/Collection/MySQCollectionTest.php
similarity index 89%
rename from workspaces/tests/includes/collection/MySQCollectionTest.php
rename to workspaces/tests/Engines/Collection/MySQCollectionTest.php
index 36a6538..742c537 100644
--- a/workspaces/tests/includes/collection/MySQCollectionTest.php
+++ b/workspaces/tests/Engines/Collection/MySQCollectionTest.php
@@ -1,135 +1,133 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Unit testing — MySQLCollection class
*
* @package ObsidianWorkspaces
* @subpackage Tests
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
-require_once('CRUDTestTrait.php');
-require_once('SQLTestTrait.php');
+namespace Waystone\Workspaces\Tests\Engines\Collection;
+
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\TestCase;
+
+use MySQLCollection;
/**
* Tests MySQLCollection class
- * @coversDefaultClass MySQLCollection
*/
-class MySQLCollectionTest extends PHPUnit_Framework_TestCase {
+#[CoversClass(MySQLCollection::class)]
+class MySQLCollectionTest extends TestCase {
///
/// Traits
///
use CRUDTestTrait;
use SQLTestTrait;
///
/// Test properties
///
/**
* @var string Our collection
*/
protected $collection;
///
/// Class initialisation
///
/**
* Gets default configuration for this test
*
* @return array The configuration block
*/
protected static function getConfig () {
return [
'DocumentStorage' => [
'Type' => 'MySQL',
'Table' => UNITTESTING_MYSQL_TABLE
]
];
}
/**
* Initializes the resources needed for thist test.
*/
- public function setUp () {
+ public function setUp () : void {
$db = new MySQLDatabase(
UNITTESTING_MYSQL_HOST,
UNITTESTING_MYSQL_USERNAME,
UNITTESTING_MYSQL_PASSWORD,
UNITTESTING_MYSQL_DATABASE
);
$this->initializeDocuments();
$this->collection = new MySQLCollection('quux', $db, UNITTESTING_MYSQL_TABLE);
}
///
/// Tests specific to MySQLCollection
///
/**
* Tests the property table is correctly set
- *
- * @covers MySQLCollection::construct
*/
public function testTable () {
$this->assertEquals(UNITTESTING_MYSQL_TABLE, $this->collection->table, "The collection constructor should have initialized the MySQLCollection::table property.");
}
/**
* Tests if the ready constant has been correctly defined
- *
- * @covers MySQLCollection::initializeCollectionsTable
*/
public function testReadyConstant () {
$this->assertTrue(
defined('COLLECTIONS_MYSQL_DATABASE_READY'),
"After the client has been initialized, we shall have a 'COLLECTIONS_SQLITE_DATABASE_READY' constant defined."
);
$this->assertSame(
COLLECTIONS_MYSQL_DATABASE_READY,
true,
"COLLECTIONS_MYSQL_DATABASE_READY constant value shall be the boolean true."
);
}
/**
* Tests if strings are correctly escaped
- *
- * @covers MySQLCollection::escape
*/
public function testEscape () {
$toEscapeExpressions = [
"",
"Lorem ipsum dolor",
"L'arbre",
"''",
"cœur"
];
$escapedExpressions = [
"",
"Lorem ipsum dolor",
"L\\'arbre",
"\\'\\'",
"cœur"
];
for ($i = 0 ; $i < count($toEscapeExpressions) ; $i++) {
$this->assertEquals(
$escapedExpressions[$i],
$this->collection->escape($toEscapeExpressions[$i]),
"The following string isn't properly escaped: '$toEscapeExpressions[$i]'"
);
}
}
}
diff --git a/workspaces/tests/includes/collection/SQLTestTrait.php b/workspaces/tests/Engines/Collection/SQLTestTrait.php
similarity index 95%
rename from workspaces/tests/includes/collection/SQLTestTrait.php
rename to workspaces/tests/Engines/Collection/SQLTestTrait.php
index ac20fc2..9a0ade8 100644
--- a/workspaces/tests/includes/collection/SQLTestTrait.php
+++ b/workspaces/tests/Engines/Collection/SQLTestTrait.php
@@ -1,66 +1,68 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* SQL schema tests for each Collection class.
*
* @package ObsidianWorkspaces
* @subpackage Tests
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
-require_once('../src/includes/autoload.php');
+namespace Waystone\Workspaces\Tests\Engines\Collection;
+
+require_once(__DIR__ . '/../../../src/includes/autoload.php');
/**
* The tests for our SQL storage engines, to ensure the schema is created correctly.
*
* For coverage purposes, it requires a coversDefaultClass annotation in the classes using this trait.
*/
trait SQLTestTrait {
/**
* @covers ::query()
*/
public function testQuery () {
/*
The query method is a complicated aspect of the code, as it returns a different
result according the kind of query:
(1) If the query doesn't return any result, null.
(2) If the query return a row with one field, the scalar value.
(3) Otherwise, an associative array, the fields as keys, the row as values.
*/
$sqlNoQueryResult = "";
$sqlNullResult = "UPDATE {$this->collection->table} SET collection_id = 'lorem' WHERE collection_id = 'lorem'";
$sqlScalarResult = "SELECT 1+1";
$sqlArrayResult = "SELECT 8+2 as sum, 8*2 as product, 8/2 as quotient, 8-2 as difference";
try {
$resultNull = $this->collection->query($sqlNoQueryResult);
} catch (Exception $ex) {
$this->fail("The query() specifications provides: 'If the query doesn't return any result, return null.'. This is also the expected behavior for empty queries. Instead we got an exception.");
}
$this->assertNull($resultNull, "The query() specifications provides: 'If the query doesn't return any result, return null.'. This is also the expected behavior for empty queries.");
$resultNull = $this->collection->query($sqlNullResult);
$this->assertNull($resultNull, "The query() specifications provides: 'If the query doesn't return any result, return null.'. This is expected for $sqlNullResult.");
$resultScalar = $this->collection->query($sqlScalarResult);
$this->assertEquals(2, $resultScalar, "The query() specifications provides: 'If the query return a row with one field, the scalar value.' This is expected for $sqlScalarResult.");
$resultArray = $this->collection->query($sqlArrayResult);
$expectedResultArray = [
'sum' => 10,
'product' => 16,
'quotient' => 4,
'difference' => 6,
];
$this->assertEquals($expectedResultArray, $resultArray, "The query() specifications provides: '[If the query returns a non scalar result, return] an associative array, the fields as keys, the row as values.'. This is expected for $sqlArrayResult.");
}
-}
\ No newline at end of file
+}
diff --git a/workspaces/tests/includes/collection/SQLiteCollectionTest.php b/workspaces/tests/Engines/Collection/SQLiteCollectionTest.php
similarity index 86%
rename from workspaces/tests/includes/collection/SQLiteCollectionTest.php
rename to workspaces/tests/Engines/Collection/SQLiteCollectionTest.php
index e3654b8..e655937 100644
--- a/workspaces/tests/includes/collection/SQLiteCollectionTest.php
+++ b/workspaces/tests/Engines/Collection/SQLiteCollectionTest.php
@@ -1,187 +1,174 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Unit testing — SQLiteCollection class
*
* @package ObsidianWorkspaces
* @subpackage Tests
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
-require_once('CRUDTestTrait.php');
-require_once('SQLTestTrait.php');
+namespace Waystone\Workspaces\Tests\Engines\Collection;
+
+use PHPUnit\Framework\Attributes\CoversClass;
+use PHPUnit\Framework\TestCase;
+
+use SQLiteCollection;
/**
* Tests SQLiteCollection class
- * @coversDefaultClass SQLiteCollection
*/
-class SQLiteCollectionTest extends PHPUnit_Framework_TestCase {
+#[CoversClass(SQLiteCollection::class)]
+class SQLiteCollectionTest extends TestCase {
///
/// Traits
///
use CRUDTestTrait;
use SQLTestTrait;
///
/// Test properties
///
/**
* @var string Our collection
*/
protected $collection;
///
/// Class initizliation
///
/**
* Gets default configuration for this test
*
* @return array The configuration block
*/
public static function getConfig () {
return [
'DocumentStorage' => [
'Type' => 'SQLite',
'File' => UNITTESTING_SQLITE_FILE
]
];
}
- /**
- * Initializes a new instance of the PHPUnit_Framework_TestCase class
- *
- * @param string $name The test case name
- */
- public function __construct (string $name = null) {
- parent::__construct($name);
-
+ public function setUp () : void {
$this->initializeDocuments();
global $Config;
$Config = static::getConfig();
$this->collection = new SQLiteCollection('quux');
}
///
/// Tests specific to SQLiteCollection
///
-
/**
* Tests the client related methods
- *
- * @covers SQLiteCollection::getClient
- * @covers SQLiteCollection::initializeClient
- * @covers SQLiteCollection::initializeCollectionsTable
*/
public function testClient () {
global $Config;
$client1 = SQLiteCollection::getClient();
$client2 = SQLiteCollection::getClient();
$this->assertInstanceOf('SQLite3', $client1);
$this->assertInstanceOf('SQLite3', $client2);
$this->assertSame($client1, $client2, "The collection classes are expected to use a singleton pattern for the client: you should return the same object initialized before instead to create another one.");
$databaseList = $this->collection->query("PRAGMA database_list");
$this->assertEquals(
[
'seq' => 0,
'name' => 'main',
'file' => realpath($Config['DocumentStorage']['File'])
],
$databaseList,
"The query PRAGMA database_list hasn't returned what we expected: one database opened by the client, the file returned by the database matching our configuration file."
);
$this->assertTrue(
defined('COLLECTIONS_SQLITE_DATABASE_READY'),
"After the client has been initialized, we shall have a 'COLLECTIONS_SQLITE_DATABASE_READY' constant defined."
);
$this->assertSame(
COLLECTIONS_SQLITE_DATABASE_READY,
true,
"COLLECTIONS_SQLITE_DATABASE_READY constant value shall be the boolean true."
);
}
/**
* Tests string escapement
- *
- * @covers SQLiteCollection::escape
*/
public function testEscape () {
$toEscapeExpressions = [
"",
"Lorem ipsum dolor",
"L'arbre",
"''",
"cœur"
];
$escapedExpressions = [
"",
"Lorem ipsum dolor",
"L''arbre",
"''''",
"cœur"
];
for ($i = 0 ; $i < count($toEscapeExpressions) ; $i++) {
$this->assertEquals(
$escapedExpressions[$i],
$this->collection->escape($toEscapeExpressions[$i]),
"The following string isn't properly escaped: '$toEscapeExpressions[$i]'"
);
}
}
- /**
- * @covers SQLiteCollection::isStatement
- */
public function testStatements () {
$sqlQueries = [
"SELECT foo FROM bar",
"PRAGMA foo"
];
$sqlStatements = [
"UPDATE bar SET foo = 'quux'",
"DELETE FROM bar WHERE foo = 'quux'",
"INSERT INTO bar (foo) VALUES ('quux')",
"REPLACE INTO bar (foo) VALUES ('quux')",
"INSERT INTO bar SELECT FROM baz"
];
foreach ($sqlQueries as $sql) {
$this->assertFalse(
$this->collection->isStatement($sql),
"The query $sql should be considered as a query, not as a statement, to use SQLite3::query() and not SQLite3::exec()"
);
}
foreach ($sqlStatements as $sql) {
$this->assertTrue(
$this->collection->isStatement($sql),
"The query $sql should be considered as a statement, not as a query, as long as SQLite3 is concerned, to use SQLite3::exec() and not SQLite3::query()"
);
}
}
/**
* Clears resources created for this test
*/
- public static function tearDownAfterClass () {
+ public static function tearDownAfterClass () : void {
unlink(UNITTESTING_SQLITE_FILE);
}
}
diff --git a/workspaces/tests/includes/collection/greenBook1.json b/workspaces/tests/Engines/Collection/greenBook1.json
similarity index 100%
rename from workspaces/tests/includes/collection/greenBook1.json
rename to workspaces/tests/Engines/Collection/greenBook1.json
diff --git a/workspaces/tests/includes/collection/greenBook2.json b/workspaces/tests/Engines/Collection/greenBook2.json
similarity index 100%
rename from workspaces/tests/includes/collection/greenBook2.json
rename to workspaces/tests/Engines/Collection/greenBook2.json
diff --git a/workspaces/tests/bootstrap.php b/workspaces/tests/bootstrap.php
new file mode 100644
index 0000000..4a9bd13
--- /dev/null
+++ b/workspaces/tests/bootstrap.php
@@ -0,0 +1,53 @@
+<?php
+
+/* -------------------------------------------------------------
+ Bootstrap tests for Obsidian Workspaces
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ Project: Nasqueron
+ Product: Obsidian Workspaces
+ License: Trivial work, not eligible to copyright
+ ------------------------------------------------------------- */
+
+/* -------------------------------------------------------------
+ Search relevant vendor/ directory
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+const VENDOR_DIR_CANDIDATES = [
+ # Composer packages have been installed directly in workspaces/
+ __DIR__ . "/../vendor",
+
+ # Composer packages have been installed at monorepo root level
+ __DIR__ . "/../../vendor",
+
+];
+
+function search_vendor_autoload () : string|null {
+ foreach (VENDOR_DIR_CANDIDATES as $dir) {
+ $autoload_path = $dir . "/autoload.php";
+
+ echo "Candidate: $autoload_path\n";
+
+ if (file_exists($autoload_path)) {
+ return $autoload_path;
+ }
+ }
+
+ return null;
+}
+
+/* -------------------------------------------------------------
+ Entry point for tests
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+function run() : void {
+ $vendor_autoload = search_vendor_autoload();
+
+ if ($vendor_autoload === null) {
+ fwrite(STDERR, "Your first need to install dependencies. Run `composer install` before running tests.");
+ die;
+ }
+
+ require($vendor_autoload);
+}
+
+run();
diff --git a/workspaces/tests/includes/database/DatabaseExceptionTest.php b/workspaces/tests/includes/database/DatabaseExceptionTest.php
index b6ce741..4efd7bc 100644
--- a/workspaces/tests/includes/database/DatabaseExceptionTest.php
+++ b/workspaces/tests/includes/database/DatabaseExceptionTest.php
@@ -1,50 +1,48 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Unit testing — FilesCollection class
*
* @package ObsidianWorkspaces
* @subpackage Tests
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
-require '../src/includes/database/DatabaseException.php';
+use PHPUnit\Framework\TestCase;
+
+require_once(__DIR__ . '/../../../src/includes/database/DatabaseException.php');
/**
* Tests DatabaseException class
*/
-class DatabaseExceptionTest extends PHPUnit_Framework_TestCase {
- /**
- * @covers DatabaseException::__construct
- * @covers DatabaseException::getQuery
- */
+class DatabaseExceptionTest extends TestCase {
public function testGetQuery () {
$sql = 'SELECT 1+';
$ex = new DatabaseException($sql, 'Syntax error', 1064);
$this->assertEquals(
$sql,
$ex->getQuery(),
""
);
$ex = new DatabaseException();
$this->assertNull(
$ex->getQuery(),
"If the query isn't specified during the constructor call, getQuery shall return null."
);
$ex = new DatabaseException('');
$this->assertEquals(
'',
$ex->getQuery(),
"If the query isn't specified during the constructor call, getQuery shall not return null but must return an empty string too."
);
}
}
diff --git a/workspaces/tests/includes/database/MySQLDatabaseTest.php b/workspaces/tests/includes/database/MySQLDatabaseTest.php
index 09d4788..8046430 100644
--- a/workspaces/tests/includes/database/MySQLDatabaseTest.php
+++ b/workspaces/tests/includes/database/MySQLDatabaseTest.php
@@ -1,70 +1,70 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Unit testing — MysqlDatabaseTest class
*
* @package ObsidianWorkspaces
* @subpackage Tests
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
+use PHPUnit\Framework\TestCase;
+
//require '../src/includes/database/MysqlDatabase.php';
/**
* Tests DatabaseTest
*/
-class MySQLDatabaseTest extends PHPUnit_Framework_TestCase {
+class MySQLDatabaseTest extends TestCase {
/**
* @var MysqlDatabase
*/
private $db;
/**
* Creates the objects against which we will test.
*/
- public function setUp () {
+ public function setUp () : void {
$this->db = new MySQLDatabase(
UNITTESTING_MYSQL_HOST,
UNITTESTING_MYSQL_USERNAME,
UNITTESTING_MYSQL_PASSWORD,
UNITTESTING_MYSQL_DATABASE
);
}
/**
* Tests string escape
- *
- * @covers SQLiteCollection::escape
*/
public function testEscape () {
$toEscapeExpressions = [
"",
"Lorem ipsum dolor",
"L'arbre",
"''",
"cœur"
];
$escapedExpressions = [
"",
"Lorem ipsum dolor",
"L\\'arbre",
"\\'\\'",
"cœur"
];
for ($i = 0 ; $i < count($toEscapeExpressions) ; $i++) {
$this->assertEquals(
$escapedExpressions[$i],
$this->db->escape($toEscapeExpressions[$i]),
"The following string isn't properly escaped: '$toEscapeExpressions[$i]'"
);
}
}
}
diff --git a/workspaces/tests/phpunit.xml b/workspaces/tests/phpunit.xml
index af4512f..07bd49a 100644
--- a/workspaces/tests/phpunit.xml
+++ b/workspaces/tests/phpunit.xml
@@ -1,26 +1,28 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
+ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
+ bootstrap="bootstrap.php"
- forceCoversAnnotation="true"
+ displayDetailsOnTestsThatTriggerDeprecations="true"
+ displayDetailsOnPhpunitDeprecations="true"
>
<php>
<!-- MySQL -->
<const name="UNITTESTING_MYSQL_HOST" value="localhost" />
<const name="UNITTESTING_MYSQL_USERNAME" value="root" />
<const name="UNITTESTING_MYSQL_PASSWORD" value="" />
<const name="UNITTESTING_MYSQL_DATABASE" value="obsidian" />
<const name="UNITTESTING_MYSQL_TABLE" value="collections_unittesting" />
<!-- SQLite -->
<const name="UNITTESTING_SQLITE_FILE" value="/tmp/collections.db" />
<!-- MongoDB -->
<const name="UNITTESTING_MONGODB_HOST" value="localhost" />
<const name="UNITTESTING_MONGODB_PORT" value="27017" />
<const name="UNITTESTING_MONGODB_SSL" value="true" />
<!-- FilesCollection -->
<const name="UNITTESTING_FILESCOLLECTION_PATH" value="/tmp/obsidiancollections" />
</php>
</phpunit>

File Metadata

Mime Type
text/x-diff
Expires
Wed, Mar 18, 12:40 (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3539675
Default Alt Text
(43 KB)

Event Timeline