1<?php
2
3/* Copyright (c) 2018 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4require_once("libs/composer/vendor/autoload.php");
5
6use ILIAS\Validation;
7use ILIAS\Data;
8
9class IsStringConstraintTest extends PHPUnit_Framework_TestCase
10{
11    public function setUp()
12    {
13        $this->df = new Data\Factory();
14        $this->lng = $this->createMock(\ilLanguage::class);
15        $this->f = new Validation\Factory($this->df, $this->lng);
16
17        $this->c = $this->f->isString();
18    }
19
20    public function testAccepts()
21    {
22        $this->assertTrue($this->c->accepts("string"));
23    }
24
25    public function testNotAccepts()
26    {
27        $this->assertFalse($this->c->accepts(2));
28    }
29
30    public function testCheckSucceed()
31    {
32        $this->c->check("string");
33        $this->assertTrue(true); // does not throw
34    }
35
36    public function testCheckFails()
37    {
38        $this->expectException(\UnexpectedValueException::class);
39        $this->c->check(2);
40    }
41
42    public function testNoProblemWith()
43    {
44        $this->assertNull($this->c->problemWith("string"));
45    }
46
47    public function testProblemWith()
48    {
49        $this->lng
50            ->expects($this->once())
51            ->method("txt")
52            ->with("not_a_string")
53            ->willReturn("-%s-");
54
55        $this->assertEquals("-double-", $this->c->problemWith(2.2));
56    }
57
58    public function testRestrictOk()
59    {
60        $ok = $this->df->ok("string");
61
62        $res = $this->c->restrict($ok);
63        $this->assertTrue($res->isOk());
64    }
65
66    public function testRestrictNotOk()
67    {
68        $not_ok = $this->df->ok(2);
69
70        $res = $this->c->restrict($not_ok);
71        $this->assertFalse($res->isOk());
72    }
73
74    public function testRestrictError()
75    {
76        $error = $this->df->error("error");
77
78        $res = $this->c->restrict($error);
79        $this->assertSame($error, $res);
80    }
81
82    public function testWithProblemBuilder()
83    {
84        $new_c = $this->c->withProblemBuilder(function () {
85            return "This was a fault";
86        });
87        $this->assertEquals("This was a fault", $new_c->problemWith(2.2));
88    }
89}
90