TestCase.php 3.36 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB\Tests;

5 6 7
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
8
use ReflectionClass;
9
use stdClass;
10 11 12

abstract class TestCase extends \PHPUnit_Framework_TestCase
{
13 14 15 16 17
    public function provideInvalidDocumentValues()
    {
        return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
    }

18 19 20 21 22
    /**
     * Return the test collection name.
     *
     * @return string
     */
23
    protected function getCollectionName()
24
    {
Jeremy Mikola's avatar
Jeremy Mikola committed
25
        $class = new ReflectionClass($this);
26

Jeremy Mikola's avatar
Jeremy Mikola committed
27
        return sprintf('%s.%s', $class->getShortName(), hash('crc32b', $this->getName()));
28 29 30 31 32 33 34
    }

    /**
     * Return the test database name.
     *
     * @return string
     */
35
    protected function getDatabaseName()
36 37 38 39
    {
        return getenv('MONGODB_DATABASE') ?: 'phplib_test';
    }

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    /**
     * Return a list of invalid array values.
     *
     * @return array
     */
    protected function getInvalidArrayValues()
    {
        return [123, 3.14, 'foo', true, new stdClass];
    }

    /**
     * Return a list of invalid boolean values.
     *
     * @return array
     */
    protected function getInvalidBooleanValues()
    {
        return [123, 3.14, 'foo', [], new stdClass];
    }

    /**
     * Return a list of invalid document values.
     *
     * @return array
     */
    protected function getInvalidDocumentValues()
    {
        return [123, 3.14, 'foo', true];
    }

    /**
     * Return a list of invalid integer values.
     *
     * @return array
     */
    protected function getInvalidIntegerValues()
    {
        return [3.14, 'foo', true, [], new stdClass];
    }

80 81 82 83 84 85 86 87 88 89
    /**
     * Return a list of invalid ReadPreference values.
     *
     * @return array
     */
    protected function getInvalidReadConcernValues()
    {
        return [123, 3.14, 'foo', true, [], new stdClass, new ReadPreference(ReadPreference::RP_PRIMARY), new WriteConcern(1)];
    }

90 91 92 93 94
    /**
     * Return a list of invalid ReadPreference values.
     *
     * @return array
     */
95 96
    protected function getInvalidReadPreferenceValues()
    {
97
        return [123, 3.14, 'foo', true, [], new stdClass, new ReadConcern, new WriteConcern(1)];
98 99
    }

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    /**
     * Return a list of invalid string values.
     *
     * @return array
     */
    protected function getInvalidStringValues()
    {
        return [123, 3.14, true, [], new stdClass];
    }

    /**
     * Return a list of invalid WriteConcern values.
     *
     * @return array
     */
115 116
    protected function getInvalidWriteConcernValues()
    {
117
        return [123, 3.14, 'foo', true, [], new stdClass, new ReadConcern, new ReadPreference(ReadPreference::RP_PRIMARY)];
118 119
    }

120 121 122 123 124
    /**
     * Return the test namespace.
     *
     * @return string
     */
125
    protected function getNamespace()
126 127 128 129 130 131 132 133 134
    {
         return sprintf('%s.%s', $this->getDatabaseName(), $this->getCollectionName());
    }

    /**
     * Return the connection URI.
     *
     * @return string
     */
135
    protected function getUri()
136
    {
137
        return getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017';
138
    }
139 140 141 142 143 144 145 146 147 148 149

    /**
     * Wrap a list of values for use as a single-argument data provider.
     *
     * @param array $values List of values
     * @return array
     */
    protected function wrapValuesForDataProvider(array $values)
    {
        return array_map(function($value) { return [$value]; }, $values);
    }
150
}