forked from spatie/laravel-view-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewModelTest.php
115 lines (86 loc) · 2.83 KB
/
ViewModelTest.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Spatie\ViewModels\Tests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
class ViewModelTest extends TestCase
{
/** @var \Spatie\ViewModels\ViewModel */
protected $viewModel;
protected function setUp(): void
{
parent::setUp();
$this->viewModel = new DummyViewModel();
}
/** @test */
public function public_methods_are_listed()
{
$array = $this->viewModel->toArray();
$this->assertArrayHasKey('post', $array);
$this->assertArrayHasKey('categories', $array);
}
/** @test */
public function public_properties_are_listed()
{
$array = $this->viewModel->toArray();
$this->assertArrayHasKey('property', $array);
}
/** @test */
public function values_are_kept_as_they_are()
{
$array = $this->viewModel->toArray();
$this->assertEquals('title', $array['post']->title);
}
/** @test */
public function callables_can_be_stored()
{
$array = $this->viewModel->toArray();
$this->assertEquals('foo', $array['callableMethod']('foo'));
}
/** @test */
public function ignored_methods_are_not_listed()
{
$array = $this->viewModel->toArray();
$this->assertArrayNotHasKey('ignoredMethod', $array);
}
/** @test */
public function to_array_is_not_listed()
{
$array = $this->viewModel->toArray();
$this->assertArrayNotHasKey('toArray', $array);
}
/** @test */
public function to_response_is_not_listed()
{
$array = $this->viewModel->toArray();
$this->assertArrayNotHasKey('toResponse', $array);
}
/** @test */
public function magic_methods_are_not_listed()
{
$array = $this->viewModel->toArray();
$this->assertArrayNotHasKey('__construct', $array);
}
/** @test */
public function to_response_returns_json_by_default()
{
$response = $this->viewModel->toResponse($this->createRequest());
$this->assertInstanceOf(JsonResponse::class, $response);
$array = $this->getResponseBody($response);
$this->assertArrayHasKey('post', $array);
$this->assertArrayHasKey('categories', $array);
}
/** @test */
public function it_will_return_a_regular_view_when_a_view_is_set_and_a_json_response_is_not_requested()
{
$response = $this->viewModel->view('test')->toResponse($this->createRequest());
$this->assertInstanceOf(Response::class, $response);
}
/** @test */
public function it_will_return_a_json_response_if_a_json_response_is_requested_even_if_a_view_is_set()
{
$response = $this->viewModel->view('test')->toResponse($this->createRequest([
'Accept' => 'application/json',
]));
$this->assertInstanceOf(JsonResponse::class, $response);
}
}