File: ExactValueTokenTest.php

package info (click to toggle)
php-phpspec-prophecy 1.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,376 kB
  • sloc: php: 9,909; makefile: 16
file content (104 lines) | stat: -rw-r--r-- 2,733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

namespace Tests\Prophecy\Argument\Token;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument\Token\ExactValueToken;

class ExactValueTokenTest extends TestCase
{
    /**
     * @see https://github.com/phpspec/prophecy/issues/268
     * @see https://stackoverflow.com/a/19097159/2424814
     */
    #[Test]
    public function does_not_trigger_nesting_error()
    {
        $child1 = new ChildClass('A', new ParentClass());
        $child2 = new ChildClass('B', new ParentClass());

        $exactValueToken = new ExactValueToken($child1);
        self::assertEquals(false, $exactValueToken->scoreArgument($child2));
    }

    #[Test]
    public function scores_10_for_objects_with_same_fields()
    {
        $child1 = new ChildClass('A', new ParentClass());
        $child2 = new ChildClass('A', new ParentClass());

        $exactValueToken = new ExactValueToken($child1);
        self::assertEquals(10, $exactValueToken->scoreArgument($child2));
    }

    #[Test]
    public function scores_10_for_matching_callables()
    {
        $callable = function () {};

        $exactValueToken = new ExactValueToken($callable);
        self::assertEquals(10, $exactValueToken->scoreArgument($callable));
    }

    #[Test]
    public function scores_false_for_object_and_string()
    {
        $child1 = new ChildClass('A', new ParentClass());

        $exactValueToken = new ExactValueToken($child1);
        self::assertEquals(false, $exactValueToken->scoreArgument("A"));
    }

    #[Test]
    public function scores_false_for_object_and_int()
    {
        $child1 = new ChildClass('A', new ParentClass());

        $exactValueToken = new ExactValueToken($child1);
        self::assertEquals(false, $exactValueToken->scoreArgument(100));
    }

    #[Test]
    public function scores_false_for_object_and_stdclass()
    {
        $child1 = new ChildClass('A', new ParentClass());

        $exactValueToken = new ExactValueToken($child1);
        self::assertEquals(false, $exactValueToken->scoreArgument(new \stdClass()));
    }

    #[Test]
    public function scores_false_for_object_and_null()
    {
        $child1 = new ChildClass('A', new ParentClass());

        $exactValueToken = new ExactValueToken($child1);
        self::assertEquals(false, $exactValueToken->scoreArgument(null));
    }
}


class ParentClass
{
    public $children = array();

    public function addChild($child)
    {
        $this->children[] = $child;
    }
}

class ChildClass
{
    public $parent = null;
    public $name = null;

    public function __construct($name, $parent)
    {
        $this->name = $name;
        $this->parent = $parent;
        $this->parent->addChild($this);
    }

}