summaryrefslogtreecommitdiff
path: root/tests/BouncerTest.php
blob: 473b4761c3008f9e37aa90482796276d91934e63 (plain)
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
<?php

use Askonomm\Bouncer\Bouncer;

test('Validate a correct e-mail address', function () {
    $fields = ['email' => 'asko@bien.ee'];
    $rules = ['email' => 'email'];
    $bouncer = new Bouncer($fields, $rules);

    expect($bouncer->fails())->toBeFalse();
});

test('Validate an incorrect e-mail address', function () {
    $fields = ['email' => 'this-is-not-right'];
    $rules = ['email' => 'email'];
    $bouncer = new Bouncer($fields, $rules);

    expect($bouncer->errors())->toBe([
        'email is not a valid e-mail address.'
    ]);
});

test('Validate a correct length of string', function () {
    $fields = ['string' => 'i-am-fine-as-i-am-long'];
    $rules = ['string' => 'len:8'];
    $bouncer = new Bouncer($fields, $rules);

    expect($bouncer->fails())->toBeFalse();
});

test('Validate an incorrect length of string', function () {
    $fields = ['string' => 'i-am-short'];
    $rules = ['string' => 'len:15'];
    $bouncer = new Bouncer($fields, $rules);

    expect($bouncer->errors())->toBe([
        'string is shorter than the required 15 characters.'
    ]);
});

test('Validate a correct required string', function () {
    $fields = ['string' => 'i-am-required'];
    $rules = ['string' => 'required'];
    $bouncer = new Bouncer($fields, $rules);

    expect($bouncer->fails())->toBeFalse();
});

test('Validate an incorrect required string', function () {
    $fields = [
        'empty-string' => '',
        'null-value' => null,
        'false-value' => false,
    ];

    $rules = [
        'empty-string' => 'required',
        'null-value' => 'required',
        'false-value' => 'required',
    ];

    $bouncer = new Bouncer($fields, $rules);

    expect($bouncer->errors())->toBe([
        'empty-string is required.',
        'null-value is required.',
    ]);
});