CollectionSnapshotTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 11
c 3
b 0
f 2
lcom 0
cbo 1
dl 0
loc 98
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testSnapshotNotArray() 0 4 1
A testSnapshotClassNotLoadable() 0 4 1
A testSnapshotWrongReflection() 0 4 1
A snapshotClassWrongReflectionProvider() 0 5 1
A testNonCollection() 0 4 1
A testKeyNotReadable() 0 4 1
A testAllValid() 0 18 2
A allValidProvider() 0 5 1
A testOriginalKeyNotFound() 0 5 1
A testOriginalKey() 0 6 1
1
<?php
2
/**
3
 * This file is part of the Totem package
4
 *
5
 * For the full copyright and license information, please view the LICENSE file
6
 * that was distributed with this source code.
7
 *
8
 * @copyright Baptiste Clavié <[email protected]>
9
 * @license   http://www.opensource.org/licenses/MIT-License MIT License
10
 */
11
12
namespace Totem\Snapshot;
13
14
use ArrayObject;
15
use ReflectionProperty;
16
17
use PHPUnit_Framework_TestCase;
18
19
class CollectionSnapshotTest extends PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @expectedException        InvalidArgumentException
23
     * @expectedExceptionMessage An array or a Traversable was expected to take a snapshot of a collection, "string" given
24
     */
25
    public function testSnapshotNotArray()
26
    {
27
        new CollectionSnapshot('foo', 'bar', ['snapshotClass' => 'Totem\\Snapshot']);
28
    }
29
30
    /**
31
     * @expectedException        InvalidArgumentException
32
     * @expectedExceptionMessage The snapshot class "Totem\Fubar" does not seem to be loadable
33
     */
34
    public function testSnapshotClassNotLoadable()
35
    {
36
        new CollectionSnapshot('foo', 'bar', ['snapshotClass' => 'Totem\\Fubar']);
37
    }
38
39
    /**
40
     * @dataProvider snapshotClassWrongReflectionProvider
41
     *
42
     * @expectedException        InvalidArgumentException
43
     * @expectedExceptionMessage A Snapshot Class should be instantiable and extends abstract class Totem\AbstractSnapshot
44
     */
45
    public function testSnapshotWrongReflection($class)
46
    {
47
        new CollectionSnapshot('foo', 'bar', ['snapshotClass' => $class]);
48
    }
49
50
    public function snapshotClassWrongReflectionProvider()
51
    {
52
        return [['Totem\\AbstractSnapshot'],
53
                ['stdClass']];
54
    }
55
56
    /**
57
     * @expectedException        InvalidArgumentException
58
     * @expectedExceptionMessage The given array / Traversable is not a collection as it contains non numeric keys
59
     */
60
    public function testNonCollection()
61
    {
62
        new CollectionSnapshot(new ArrayObject(['foo' => 'bar']), 'bar');
63
    }
64
65
    /**
66
     * @expectedException        InvalidArgumentException
67
     * @expectedExceptionMessage The key "baz" is not defined or readable in one of the elements of the collection
68
     */
69
    public function testKeyNotReadable()
70
    {
71
        new CollectionSnapshot([['foo' => 'bar']], 'baz');
72
    }
73
74
    /** @dataProvider allValidProvider */
75
    public function testAllValid($hasSnapshot)
76
    {
77
        $options = [];
78
        $class   = 'Totem\\Snapshot\\ArraySnapshot';
79
80
        if (true === $hasSnapshot) {
81
            $class                    = 'Totem\\Snapshot';
82
            $options['snapshotClass'] = 'Totem\\Snapshot';
83
        }
84
85
        $snapshot = new CollectionSnapshot([['foo' => 'bar', 'baz' => 'fubar']], '[foo]', $options);
86
87
        $refl = new ReflectionProperty('Totem\\AbstractSnapshot', 'data');
88
        $refl->setAccessible(true);
89
90
        $this->assertArrayHasKey('bar', $refl->getValue($snapshot));
91
        $this->assertInstanceOf($class, $refl->getValue($snapshot)['bar']);
92
    }
93
94
    public function allValidProvider()
95
    {
96
        return [[true],
97
                [false]];
98
    }
99
100
    /**
101
     * @expectedException        InvalidArgumentException
102
     * @expectedExceptionMessage The primary key "baz" is not in the computed dataset
103
     */
104
    public function testOriginalKeyNotFound()
105
    {
106
        $snapshot = new CollectionSnapshot([['foo' => 'bar']], '[foo]');
107
        $snapshot->getOriginalKey('baz');
108
    }
109
110
    public function testOriginalKey()
111
    {
112
        $snapshot = new CollectionSnapshot([['foo' => 'bar']], '[foo]');
113
114
        $this->assertSame(0, $snapshot->getOriginalKey('bar'));
115
    }
116
}
117
118