Skip to content

Pedantry tests #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,44 +79,44 @@ public function listDatabases()
}

/**
* Select a database.
* Select a collection.
*
* If a write concern or read preference is not specified, the write concern
* or read preference of the Client will be applied, respectively.
*
* @param string $databaseName Name of the database to select
* @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* @return Database
* @return Collection
*/
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{
$namespace = $databaseName . '.' . $collectionName;
// TODO: inherit from Manager options once PHPC-196 is implemented
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;

return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
}

/**
* Select a collection.
* Select a database.
*
* If a write concern or read preference is not specified, the write concern
* or read preference of the Client will be applied, respectively.
*
* @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select
* @param string $databaseName Name of the database to select
* @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreference Default read preference to apply
* @return Collection
* @return Database
*/
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{
$namespace = $databaseName . '.' . $collectionName;
// TODO: inherit from Manager options once PHPC-196 is implemented
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;

return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
}
}
40 changes: 20 additions & 20 deletions src/Model/CollectionInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,53 @@ public function __construct(array $info)
}

/**
* Return the collection name.
* Return the maximum number of documents to keep in the capped collection.
*
* @return string
* @return integer|null
*/
public function getName()
public function getCappedMax()
{
return (string) $this->info['name'];
return isset($this->info['options']['max']) ? (integer) $this->info['options']['max'] : null;
}

/**
* Return the collection options.
* Return the maximum size (in bytes) of the capped collection.
*
* @return array
* @return integer|null
*/
public function getOptions()
public function getCappedSize()
{
return isset($this->info['options']) ? (array) $this->info['options'] : array();
return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null;
}

/**
* Return whether the collection is a capped collection.
* Return the collection name.
*
* @return boolean
* @return string
*/
public function isCapped()
public function getName()
{
return ! empty($this->info['options']['capped']);
return (string) $this->info['name'];
}

/**
* Return the maximum number of documents to keep in the capped collection.
* Return the collection options.
*
* @return integer|null
* @return array
*/
public function getCappedMax()
public function getOptions()
{
return isset($this->info['options']['max']) ? (integer) $this->info['options']['max'] : null;
return isset($this->info['options']) ? (array) $this->info['options'] : array();
}

/**
* Return the maximum size (in bytes) of the capped collection.
* Return whether the collection is a capped collection.
*
* @return integer|null
* @return boolean
*/
public function getCappedSize()
public function isCapped()
{
return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null;
return ! empty($this->info['options']['capped']);
}

/**
Expand Down
42 changes: 21 additions & 21 deletions src/Model/CollectionInfoLegacyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,6 @@ public function __construct(Traversable $iterator)
parent::__construct($iterator);
}

/**
* Return the current element as a CollectionInfo instance.
*
* @see CollectionInfoIterator::current()
* @see http://php.net/iterator.current
* @return CollectionInfo
*/
public function current()
{
$info = parent::current();

// Trim the database prefix up to and including the first dot
$firstDot = strpos($info['name'], '.');

if ($firstDot !== false) {
$info['name'] = (string) substr($info['name'], $firstDot + 1);
}

return new CollectionInfo($info);
}

/**
* Filter out internal or invalid collections.
*
Expand Down Expand Up @@ -92,4 +71,25 @@ public function accept()

return true;
}

/**
* Return the current element as a CollectionInfo instance.
*
* @see CollectionInfoIterator::current()
* @see http://php.net/iterator.current
* @return CollectionInfo
*/
public function current()
{
$info = parent::current();

// Trim the database prefix up to and including the first dot
$firstDot = strpos($info['name'], '.');

if ($firstDot !== false) {
$info['name'] = (string) substr($info['name'], $firstDot + 1);
}

return new CollectionInfo($info);
}
}
22 changes: 11 additions & 11 deletions src/Model/DatabaseInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public function __construct(array $info)
$this->info = $info;
}

/**
* Return the collection info as an array.
*
* @see http://php.net/oop5.magic#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return $this->info;
}

/**
* Return the database name.
*
Expand Down Expand Up @@ -55,15 +66,4 @@ public function isEmpty()
{
return (boolean) $this->info['empty'];
}

/**
* Return the collection info as an array.
*
* @see http://php.net/oop5.magic#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return $this->info;
}
}
22 changes: 11 additions & 11 deletions src/Model/IndexInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public function __construct(array $info)
$this->info = $info;
}

/**
* Return the collection info as an array.
*
* @see http://php.net/oop5.magic#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return $this->info;
}

/**
* Return the index key.
*
Expand Down Expand Up @@ -157,15 +168,4 @@ public function offsetUnset($key)
{
throw new BadMethodCallException('IndexInfo is immutable');
}

/**
* Return the collection info as an array.
*
* @see http://php.net/oop5.magic#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return $this->info;
}
}
18 changes: 9 additions & 9 deletions src/Model/IndexInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ public function __construct(array $index)
}

/**
* Serialize the index information to BSON for index creation.
* Return the index name.
*
* @see MongoDB\Collection::createIndexes()
* @see http://php.net/bson-serializable.bsonserialize
* @param string
*/
public function bsonSerialize()
public function __toString()
{
return $this->index;
return $this->index['name'];
}

/**
* Return the index name.
* Serialize the index information to BSON for index creation.
*
* @param string
* @see MongoDB\Collection::createIndexes()
* @see http://php.net/bson-serializable.bsonserialize
*/
public function __toString()
public function bsonSerialize()
{
return $this->index['name'];
return $this->index;
}

/**
Expand Down
32 changes: 0 additions & 32 deletions tests/CollectionTest.php

This file was deleted.

70 changes: 70 additions & 0 deletions tests/PedantryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace MongoDB\Tests;

use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
use ReflectionMethod;
use RegexIterator;

/**
* Pedantic tests that have nothing to do with functional correctness.
*/
class PedantryTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideProjectClassNames
*/
public function testMethodsAreOrderedAlphabeticallyByVisibility($className)
{
$class = new ReflectionClass($className);
$methods = $class->getMethods();

$methods = array_filter(
$methods,
function(ReflectionMethod $method) use ($class) {
return $method->getDeclaringClass() == $class;
}
);

$getSortValue = function(ReflectionMethod $method) {
if ($method->getModifiers() & ReflectionMethod::IS_PRIVATE) {
return '2' . $method->getName();
}
if ($method->getModifiers() & ReflectionMethod::IS_PROTECTED) {
return '1' . $method->getName();
}
if ($method->getModifiers() & ReflectionMethod::IS_PUBLIC) {
return '0' . $method->getName();
}
};

$sortedMethods = $methods;
usort(
$sortedMethods,
function(ReflectionMethod $a, ReflectionMethod $b) use ($getSortValue) {
return strcasecmp($getSortValue($a), $getSortValue($b));
}
);

$methods = array_map(function(ReflectionMethod $method) { return $method->getName(); }, $methods);
$sortedMethods = array_map(function(ReflectionMethod $method) { return $method->getName(); }, $sortedMethods);

$this->assertEquals($sortedMethods, $methods);
}

public function provideProjectClassNames()
{
$classNames = array();
$srcDir = realpath(__DIR__ . '/../src/');

$files = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcDir)), '/\.php$/i');

foreach ($files as $file) {
$classNames[][] = 'MongoDB\\' . str_replace(DIRECTORY_SEPARATOR, '\\', substr($file->getRealPath(), strlen($srcDir) + 1, -4));
}

return $classNames;
}
}