ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
HasMaxLengthConstraintTest.php
Go to the documentation of this file.
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
7use ILIAS\Data;
8use PHPUnit\Framework\TestCase;
9
10class HasMaxLengthConstraintTest extends TestCase
11{
15 private $df;
16
20 private $lng;
21
25 private $max_length;
26
30 private $c;
31
32 public function setUp() : void
33 {
34 $this->df = new Data\Factory();
35 $this->lng = $this->createMock(\ilLanguage::class);
36
37 $this->max_length = 2;
38
39 $this->c = new \ILIAS\Refinery\String\HasMaxLength(
40 $this->max_length,
41 $this->df,
42 $this->lng
43 );
44 }
45
46 public function testAccepts1()
47 {
48 $this->assertTrue($this->c->accepts("12"));
49 }
50
51 public function testAccepts2()
52 {
53 $this->assertTrue($this->c->accepts("1"));
54 }
55
56 public function testNotAccepts()
57 {
58 $this->assertFalse($this->c->accepts("123"));
59 }
60
61 public function testCheckSucceed()
62 {
63 $this->c->check("12");
64 $this->assertTrue(true); // does not throw
65 }
66
67 public function testCheckFails()
68 {
69 $this->expectException(\UnexpectedValueException::class);
70 $this->c->check("123");
71 }
72
73 public function testNoProblemWith()
74 {
75 $this->assertNull($this->c->problemWith("12"));
76 }
77
78 public function testProblemWith()
79 {
80 $this->lng
81 ->expects($this->once())
82 ->method("txt")
83 ->with("not_max_length")
84 ->willReturn("-%s-");
85
86 $this->assertEquals("-2-", $this->c->problemWith("123"));
87 }
88
89 public function testRestrictOk()
90 {
91 $ok = $this->df->ok("12");
92
93 $res = $this->c->applyTo($ok);
94 $this->assertTrue($res->isOk());
95 }
96
97 public function testRestrictNotOk()
98 {
99 $not_ok = $this->df->ok("123");
100
101 $res = $this->c->applyTo($not_ok);
102 $this->assertFalse($res->isOk());
103 }
104
105 public function testRestrictError()
106 {
107 $error = $this->df->error("error");
108
109 $res = $this->c->applyTo($error);
110 $this->assertSame($error, $res);
111 }
112
113 public function testWithProblemBuilder()
114 {
115 $new_c = $this->c->withProblemBuilder(function () {
116 return "This was a fault";
117 });
118 $this->assertEquals("This was a fault", $new_c->problemWith("123"));
119 }
120}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
foreach($_POST as $key=> $value) $res