|
2 | 2 |
|
3 | 3 | namespace Illuminate\Testing;
|
4 | 4 |
|
5 |
| -use Illuminate\Testing\TestView; |
| 5 | +use Illuminate\Testing\Assert as PHPUnit; |
| 6 | +use Illuminate\Testing\Constraints\SeeInOrder; |
6 | 7 | use Illuminate\View\Component;
|
7 | 8 |
|
8 |
| - |
9 |
| -class TestComponent extends TestView |
| 9 | +class TestComponent |
10 | 10 | {
|
11 |
| - /** |
12 |
| - * The original view. |
13 |
| - * |
14 |
| - * @var \Illuminate\View\Component |
15 |
| - */ |
16 |
| - protected $component; |
17 |
| - |
18 |
| - /** |
19 |
| - * The rendered view contents. |
20 |
| - * |
21 |
| - * @var string |
22 |
| - */ |
23 |
| - protected $rendered; |
24 |
| - |
25 |
| - /** |
26 |
| - * Create a new test view instance. |
27 |
| - * |
28 |
| - * @param \Illuminate\View\Component $view |
29 |
| - * @return void |
30 |
| - */ |
31 |
| - public function __construct(Component $component, $view) |
32 |
| - { |
33 |
| - $this->component = $component; |
34 |
| - $this->rendered = $view->render(); |
35 |
| - } |
36 |
| - |
37 |
| - public function __get($attribute) |
38 |
| - { |
39 |
| - return $this->component->{$attribute}; |
40 |
| - } |
| 11 | + /** |
| 12 | + * The original component. |
| 13 | + * |
| 14 | + * @var \Illuminate\View\Component |
| 15 | + */ |
| 16 | + public $component; |
| 17 | + |
| 18 | + /** |
| 19 | + * The rendered component contents. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $rendered; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new test component instance. |
| 27 | + * |
| 28 | + * @param \Illuminate\View\Component $component |
| 29 | + * @param \Illuminate\View\View $view |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function __construct($component, $view) |
| 33 | + { |
| 34 | + $this->component = $component; |
| 35 | + |
| 36 | + $this->rendered = $view->render(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Assert that the given string is contained within the rendered component. |
| 41 | + * |
| 42 | + * @param string $value |
| 43 | + * @param bool $escape |
| 44 | + * @return $this |
| 45 | + */ |
| 46 | + public function assertSee($value, $escape = true) |
| 47 | + { |
| 48 | + $value = $escape ? e($value) : $value; |
| 49 | + |
| 50 | + PHPUnit::assertStringContainsString((string) $value, $this->rendered); |
| 51 | + |
| 52 | + return $this; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Assert that the given strings are contained in order within the rendered component. |
| 57 | + * |
| 58 | + * @param array $values |
| 59 | + * @param bool $escape |
| 60 | + * @return $this |
| 61 | + */ |
| 62 | + public function assertSeeInOrder(array $values, $escape = true) |
| 63 | + { |
| 64 | + $values = $escape ? array_map('e', ($values)) : $values; |
| 65 | + |
| 66 | + PHPUnit::assertThat($values, new SeeInOrder($this->rendered)); |
| 67 | + |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Assert that the given string is contained within the rendered component text. |
| 73 | + * |
| 74 | + * @param string $value |
| 75 | + * @param bool $escape |
| 76 | + * @return $this |
| 77 | + */ |
| 78 | + public function assertSeeText($value, $escape = true) |
| 79 | + { |
| 80 | + $value = $escape ? e($value) : $value; |
| 81 | + |
| 82 | + PHPUnit::assertStringContainsString((string) $value, strip_tags($this->rendered)); |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Assert that the given strings are contained in order within the rendered component text. |
| 89 | + * |
| 90 | + * @param array $values |
| 91 | + * @param bool $escape |
| 92 | + * @return $this |
| 93 | + */ |
| 94 | + public function assertSeeTextInOrder(array $values, $escape = true) |
| 95 | + { |
| 96 | + $values = $escape ? array_map('e', ($values)) : $values; |
| 97 | + |
| 98 | + PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->rendered))); |
| 99 | + |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Assert that the given string is not contained within the rendered component. |
| 105 | + * |
| 106 | + * @param string $value |
| 107 | + * @param bool $escape |
| 108 | + * @return $this |
| 109 | + */ |
| 110 | + public function assertDontSee($value, $escape = true) |
| 111 | + { |
| 112 | + $value = $escape ? e($value) : $value; |
| 113 | + |
| 114 | + PHPUnit::assertStringNotContainsString((string) $value, $this->rendered); |
| 115 | + |
| 116 | + return $this; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Assert that the given string is not contained within the rendered component text. |
| 121 | + * |
| 122 | + * @param string $value |
| 123 | + * @param bool $escape |
| 124 | + * @return $this |
| 125 | + */ |
| 126 | + public function assertDontSeeText($value, $escape = true) |
| 127 | + { |
| 128 | + $value = $escape ? e($value) : $value; |
| 129 | + |
| 130 | + PHPUnit::assertStringNotContainsString((string) $value, strip_tags($this->rendered)); |
| 131 | + |
| 132 | + return $this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get the string contents of the rendered component. |
| 137 | + * |
| 138 | + * @return string |
| 139 | + */ |
| 140 | + public function __toString() |
| 141 | + { |
| 142 | + return $this->rendered; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Dynamically access properties on the underlying component. |
| 147 | + * |
| 148 | + * @param string $attribute |
| 149 | + * @return mixed |
| 150 | + */ |
| 151 | + public function __get($attribute) |
| 152 | + { |
| 153 | + return $this->component->{$attribute}; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Dynamically call methods on the underlying component. |
| 158 | + * |
| 159 | + * @param string $method |
| 160 | + * @param array $parameters |
| 161 | + * @return mixed |
| 162 | + */ |
| 163 | + public function __call($method, $parameters) |
| 164 | + { |
| 165 | + return $this->component->{$method}(...$parameters); |
| 166 | + } |
41 | 167 | }
|
0 commit comments