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