Skip to content

Commit 88c67f5

Browse files
committed
Split views methods
1 parent 892a77e commit 88c67f5

File tree

2 files changed

+127
-118
lines changed

2 files changed

+127
-118
lines changed

src/Codeception/Module/Laravel.php

Lines changed: 2 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,17 @@
1919
use Codeception\Module\Laravel\InteractsWithExceptionHandling;
2020
use Codeception\Module\Laravel\InteractsWithRouting;
2121
use Codeception\Module\Laravel\InteractsWithSession;
22+
use Codeception\Module\Laravel\InteractsWithViews;
2223
use Codeception\Subscriber\ErrorHandler;
2324
use Codeception\TestInterface;
2425
use Codeception\Util\ReflectionHelper;
2526
use Exception;
26-
use Illuminate\Contracts\Session\Session;
27-
use Illuminate\Contracts\View\Factory as ViewContract;
2827
use Illuminate\Database\Connection;
2928
use Illuminate\Database\DatabaseManager;
3029
use Illuminate\Database\Eloquent\Factory;
3130
use Illuminate\Foundation\Application;
3231
use Illuminate\Routing\Route;
33-
use Illuminate\Support\ViewErrorBag;
3432
use ReflectionException;
35-
use function is_array;
3633

3734
/**
3835
*
@@ -134,6 +131,7 @@ class Laravel extends Framework implements ActiveRecord, PartedModule
134131
use InteractsWithExceptionHandling;
135132
use InteractsWithRouting;
136133
use InteractsWithSession;
134+
use InteractsWithViews;
137135

138136
/**
139137
* @var Application
@@ -308,120 +306,6 @@ public function disableMiddleware()
308306
$this->client->disableMiddleware();
309307
}
310308

311-
/**
312-
* Assert that form errors are bound to the View.
313-
*
314-
* ``` php
315-
* <?php
316-
* $I->seeFormHasErrors();
317-
* ```
318-
*/
319-
public function seeFormHasErrors(): void
320-
{
321-
/** @var ViewContract $view */
322-
$view = $this->app->make('view');
323-
/** @var ViewErrorBag $viewErrorBag */
324-
$viewErrorBag = $view->shared('errors');
325-
326-
$this->assertGreaterThan(
327-
0,
328-
$viewErrorBag->count(),
329-
'Expecting that the form has errors, but there were none!'
330-
);
331-
}
332-
333-
/**
334-
* Assert that there are no form errors bound to the View.
335-
*
336-
* ``` php
337-
* <?php
338-
* $I->dontSeeFormErrors();
339-
* ```
340-
*/
341-
public function dontSeeFormErrors(): void
342-
{
343-
/** @var ViewContract $view */
344-
$view = $this->app->make('view');
345-
/** @var ViewErrorBag $viewErrorBag */
346-
$viewErrorBag = $view->shared('errors');
347-
348-
$this->assertEquals(
349-
0,
350-
$viewErrorBag->count(),
351-
'Expecting that the form does not have errors, but there were!'
352-
);
353-
}
354-
355-
/**
356-
* Verifies that multiple fields on a form have errors.
357-
*
358-
* This method will validate that the expected error message
359-
* is contained in the actual error message, that is,
360-
* you can specify either the entire error message or just a part of it:
361-
*
362-
* ``` php
363-
* <?php
364-
* $I->seeFormErrorMessages([
365-
* 'address' => 'The address is too long',
366-
* 'telephone' => 'too short' // the full error message is 'The telephone is too short'
367-
* ]);
368-
* ```
369-
*
370-
* If you don't want to specify the error message for some fields,
371-
* you can pass `null` as value instead of the message string.
372-
* If that is the case, it will be validated that
373-
* that field has at least one error of any type:
374-
*
375-
* ``` php
376-
* <?php
377-
* $I->seeFormErrorMessages([
378-
* 'telephone' => 'too short',
379-
* 'address' => null
380-
* ]);
381-
* ```
382-
*
383-
* @param array $expectedErrors
384-
*/
385-
public function seeFormErrorMessages(array $expectedErrors): void
386-
{
387-
foreach ($expectedErrors as $field => $message) {
388-
$this->seeFormErrorMessage($field, $message);
389-
}
390-
}
391-
392-
/**
393-
* Assert that a specific form error message is set in the view.
394-
*
395-
* If you want to assert that there is a form error message for a specific key
396-
* but don't care about the actual error message you can omit `$expectedErrorMessage`.
397-
*
398-
* If you do pass `$expectedErrorMessage`, this method checks if the actual error message for a key
399-
* contains `$expectedErrorMessage`.
400-
*
401-
* ``` php
402-
* <?php
403-
* $I->seeFormErrorMessage('username');
404-
* $I->seeFormErrorMessage('username', 'Invalid Username');
405-
* ```
406-
* @param string $field
407-
* @param string|null $errorMessage
408-
*/
409-
public function seeFormErrorMessage(string $field, $errorMessage = null): void
410-
{
411-
/** @var ViewContract $view */
412-
$view = $this->app['view'];
413-
/** @var ViewErrorBag $viewErrorBag */
414-
$viewErrorBag = $view->shared('errors');
415-
416-
if (!($viewErrorBag->has($field))) {
417-
$this->fail("No form error message for key '$field'\n");
418-
}
419-
420-
if (! is_null($errorMessage)) {
421-
$this->assertStringContainsString($errorMessage, $viewErrorBag->first($field));
422-
}
423-
}
424-
425309
/**
426310
* Returns a list of recognized domain names.
427311
* This elements of this list are regular expressions.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Laravel;
6+
7+
use Illuminate\Contracts\View\Factory as ViewContract;
8+
use Illuminate\Support\ViewErrorBag;
9+
10+
trait InteractsWithViews
11+
{
12+
/**
13+
* Assert that there are no form errors bound to the View.
14+
*
15+
* ``` php
16+
* <?php
17+
* $I->dontSeeFormErrors();
18+
* ```
19+
*/
20+
public function dontSeeFormErrors(): void
21+
{
22+
/** @var ViewContract $view */
23+
$view = $this->app->make('view');
24+
/** @var ViewErrorBag $viewErrorBag */
25+
$viewErrorBag = $view->shared('errors');
26+
27+
$this->assertEquals(
28+
0,
29+
$viewErrorBag->count(),
30+
'Expecting that the form does not have errors, but there were!'
31+
);
32+
}
33+
34+
/**
35+
* Assert that a specific form error message is set in the view.
36+
*
37+
* If you want to assert that there is a form error message for a specific key
38+
* but don't care about the actual error message you can omit `$expectedErrorMessage`.
39+
*
40+
* If you do pass `$expectedErrorMessage`, this method checks if the actual error message for a key
41+
* contains `$expectedErrorMessage`.
42+
*
43+
* ``` php
44+
* <?php
45+
* $I->seeFormErrorMessage('username');
46+
* $I->seeFormErrorMessage('username', 'Invalid Username');
47+
* ```
48+
* @param string $field
49+
* @param string|null $errorMessage
50+
*/
51+
public function seeFormErrorMessage(string $field, $errorMessage = null): void
52+
{
53+
/** @var ViewContract $view */
54+
$view = $this->app['view'];
55+
/** @var ViewErrorBag $viewErrorBag */
56+
$viewErrorBag = $view->shared('errors');
57+
58+
if (!($viewErrorBag->has($field))) {
59+
$this->fail("No form error message for key '$field'\n");
60+
}
61+
62+
if (! is_null($errorMessage)) {
63+
$this->assertStringContainsString($errorMessage, $viewErrorBag->first($field));
64+
}
65+
}
66+
67+
/**
68+
* Verifies that multiple fields on a form have errors.
69+
*
70+
* This method will validate that the expected error message
71+
* is contained in the actual error message, that is,
72+
* you can specify either the entire error message or just a part of it:
73+
*
74+
* ``` php
75+
* <?php
76+
* $I->seeFormErrorMessages([
77+
* 'address' => 'The address is too long',
78+
* 'telephone' => 'too short' // the full error message is 'The telephone is too short'
79+
* ]);
80+
* ```
81+
*
82+
* If you don't want to specify the error message for some fields,
83+
* you can pass `null` as value instead of the message string.
84+
* If that is the case, it will be validated that
85+
* that field has at least one error of any type:
86+
*
87+
* ``` php
88+
* <?php
89+
* $I->seeFormErrorMessages([
90+
* 'telephone' => 'too short',
91+
* 'address' => null
92+
* ]);
93+
* ```
94+
*
95+
* @param array $expectedErrors
96+
*/
97+
public function seeFormErrorMessages(array $expectedErrors): void
98+
{
99+
foreach ($expectedErrors as $field => $message) {
100+
$this->seeFormErrorMessage($field, $message);
101+
}
102+
}
103+
104+
/**
105+
* Assert that form errors are bound to the View.
106+
*
107+
* ``` php
108+
* <?php
109+
* $I->seeFormHasErrors();
110+
* ```
111+
*/
112+
public function seeFormHasErrors(): void
113+
{
114+
/** @var ViewContract $view */
115+
$view = $this->app->make('view');
116+
/** @var ViewErrorBag $viewErrorBag */
117+
$viewErrorBag = $view->shared('errors');
118+
119+
$this->assertGreaterThan(
120+
0,
121+
$viewErrorBag->count(),
122+
'Expecting that the form has errors, but there were none!'
123+
);
124+
}
125+
}

0 commit comments

Comments
 (0)