Hoa central
Documentation.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Ruler\Test\Unit;
38 
39 use Hoa\Ruler as LUT;
40 use Hoa\Test;
41 
51 {
52  public function case_classical()
53  {
54  $this
55  ->given(
56  $ruler = new LUT(),
57  $rule = 'group in ["customer", "guest"] and points > 30'
58  );
59 
60  $this->next_case_classical($ruler, $rule);
61  }
62 
63  public function next_case_classical($ruler, $rule)
64  {
65  $this
66  ->given(
67  $context = new LUT\Context(),
68  $context['group'] = $this->sample(
69  $this->realdom->regex('/customer|guest/')
70  ),
71  $context['points'] = function () {
72  return 42;
73  }
74  )
75  ->when($result = $ruler->assert($rule, $context))
76  ->then
77  ->boolean($result)
78  ->isTrue()
79 
80  ->given($context['points'] = 29)
81  ->when($result = $ruler->assert($rule, $context))
82  ->then
83  ->boolean($result)
84  ->isFalse();
85  }
86 
87  public function case_new_operators()
88  {
89  $this
90  ->given(
91  $user = new \Mock\StdClass(),
92  $user->group = 'customer',
93  $user->points = 42,
94  $user->status = true,
95  $ruler = new LUT(),
96  $rule = 'logged(user) and group in ["customer", "guest"] and points > 30',
97  $context = new LUT\Context(),
98  $context['user'] = function () use ($user, $context) {
99  $context['group'] = $user->group;
100  $context['points'] = $user->points;
101 
102  return $user;
103  }
104  )
105  ->when(
106  $ruler->getDefaultAsserter()->setOperator('logged', function ($user) {
107  return $user->status;
108  }),
109  $result = $ruler->assert($rule, $context)
110  )
111  ->then
112  ->boolean($result)
113  ->isTrue()
114 
115  ->given($user->status = false)
116  ->when($result = $ruler->assert($rule, $context))
117  ->then
118  ->boolean($result)
119  ->isFalse();
120  }
121 
122  public function case_interpret()
123  {
124  $this
125  ->given(
126  $model = LUT::interpret('group in ["customer", "guest"] and points > 30')
127  )
128  ->when($ledom = unserialize(serialize($model)))
129  ->then
130  ->object($model)
131  ->isEqualTo($ledom);
132 
133  $this->next_case_classical(new LUT(), $model);
134  }
135 
136  public function case_compile()
137  {
138  $expectedResult = <<<'RESULT'
139 $model = new \Hoa\Ruler\Model();
140 $model->expression =
141  $model->and(
142  $model->func(
143  'logged',
144  $model->variable('user')
145  ),
146  $model->and(
147  $model->in(
148  $model->variable('group'),
149  [
150  'customer',
151  'guest'
152  ]
153  ),
154  $model->{'>'}(
155  $model->variable('points'),
156  30
157  )
158  )
159  );
160 RESULT;
161 
162  $this
163  ->when($result = LUT::interpret(
164  'logged(user) and group in ["customer", "guest"] and points > 30'
165  ) . '')
166  ->then
167  ->string($result)
168  ->isEqualTo($expectedResult);
169  }
170 }