1<?php
2
3/* Copyright (c) 2017, 2018, Stefan Hecken <stefan.hecken@concepts-and-training.de>, Richard Klees <richard.klees@concepts-and-training.de, Extended GPL, see docs/LICENSE */
4require_once("libs/composer/vendor/autoload.php");
5
6use ILIAS\Refinery;
7use ILIAS\Data;
8use PHPUnit\Framework\TestCase;
9
10class NotTest extends TestCase
11{
12    /**
13     * @var Data\Factory
14     */
15    private $df;
16
17    /**
18     * @var \ilLanguage
19     */
20    private $lng;
21
22    /**
23     * @var \ILIAS\Refinery\Factory
24     */
25    private $refinery;
26
27    /**
28     * @var
29     */
30    private $not_true;
31
32    /**
33     * @var
34     */
35    private $not_false;
36
37    public function setUp() : void
38    {
39        $this->df = new Data\Factory();
40        $this->lng = $this->createMock(\ilLanguage::class);
41        $this->refinery = new \ILIAS\Refinery\Factory($this->df, $this->lng);
42
43        $group = $this->refinery->custom();
44
45        $this->not_true = $this->refinery->logical()->not($group->constraint(
46            function ($v) {
47                return true;
48            },
49            "not_true"
50        ));
51
52        $this->not_false = $this->refinery->logical()->not($group->constraint(
53            function ($v) {
54                return false;
55            },
56            "not_false"
57        ));
58    }
59
60    public function testAccepts()
61    {
62        $this->assertTrue($this->not_false->accepts(null));
63    }
64
65    public function testNotAccepts()
66    {
67        $this->assertFalse($this->not_true->accepts(null));
68    }
69
70    public function testCheckSucceed()
71    {
72        $this->not_false->check(null);
73        $this->assertTrue(true); // does not throw
74    }
75
76    public function testCheckFails()
77    {
78        $this->expectException(\UnexpectedValueException::class);
79        $this->not_true->check(null);
80    }
81
82    public function testNoProblemWith()
83    {
84        $this->assertNull($this->not_false->problemWith(null));
85    }
86
87    public function testProblemWith()
88    {
89        $this->lng
90            ->expects($this->once())
91            ->method("txt")
92            ->with("not_generic")
93            ->willReturn("-%s-");
94
95        $this->assertEquals("-not_true-", $this->not_true->problemWith(null));
96    }
97
98    public function testRestrictOk()
99    {
100        $ok = $this->df->ok(null);
101
102        $res = $this->not_false->applyTo($ok);
103        $this->assertTrue($res->isOk());
104    }
105
106    public function testRestrictNotOk()
107    {
108        $not_ok = $this->df->ok(null);
109
110        $res = $this->not_true->applyTo($not_ok);
111        $this->assertFalse($res->isOk());
112    }
113
114    public function testRestrictError()
115    {
116        $error = $this->df->error("error");
117
118        $res = $this->not_false->applyTo($error);
119        $this->assertSame($error, $res);
120    }
121
122    public function testWithProblemBuilder()
123    {
124        $new_c = $this->not_true->withProblemBuilder(function () {
125            return "This was a fault";
126        });
127        $this->assertEquals("This was a fault", $new_c->problemWith(null));
128    }
129}
130