Skip to content

Commit c424a07

Browse files
committed
Make tests more resilient to change
1 parent 8c776b5 commit c424a07

File tree

2 files changed

+49
-273
lines changed

2 files changed

+49
-273
lines changed

tests/CasterTest.php

Lines changed: 15 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,13 @@ public function test_carbon_date(): void
1515
{
1616
$now = Carbon::parse('2022-01-18 19:44:02.572622', 'America/New_York');
1717

18-
$expected = <<<EOD
19-
Carbon\Carbon @%d {
20-
date: 2022-01-18 19:44:02.572622 America/New_York (-05:00)
21-
#endOfTime: false
22-
#startOfTime: false
23-
#constructedObjectId: "%s"
24-
#dumpProperties: array:3 [
25-
0 => "date"
26-
1 => "timezone_type"
27-
2 => "timezone"
28-
]
29-
…%d
30-
}
31-
EOD;
18+
$dump = $this->getDump($now);
3219

33-
$this->assertDumpMatchesFormat($expected, $now);
20+
$this->assertStringStartsWith('Carbon\\Carbon', $dump);
21+
$this->assertStringContainsString('date: 2022-01-18 19:44:02.572622 America/New_York (-05:00)', $dump);;
22+
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);
23+
24+
$this->assertStringNotContainsString('localMacros', $dump);
3425
}
3526

3627
public function test_package_can_be_disabled(): void
@@ -63,42 +54,17 @@ public function test_container(): void
6354
$container->extend('bar', fn() => $this);
6455
$container->make('bar');
6556

66-
$fqcn = static::class;
57+
$dump = $this->getDump($container);
6758

68-
$expected = <<<EOD
69-
Illuminate\Container\Container {
70-
#bindings: array:1 [
71-
"{$fqcn}" => array:2 [
72-
"concrete" => Closure() {
73-
class: "{$fqcn}"
74-
this: {$fqcn} {#1 …}
75-
file: "%s"
76-
line: "%d to %d"
77-
}
78-
"shared" => false
79-
]
80-
]
81-
#aliases: array:1 [
82-
"bar" => "{$fqcn}"
83-
]
84-
#resolved: array:1 [
85-
"{$fqcn}" => true
86-
]
87-
#extenders: array:1 [
88-
"{$fqcn}" => array:1 [
89-
0 => Closure() {
90-
class: "{$fqcn}"
91-
this: Glhd\LaravelDumper\Tests\CasterTest {#1 …}
92-
file: "%s"
93-
line: "%d to %d"
94-
}
95-
]
96-
]
97-
…%d
98-
}
99-
EOD;
59+
$this->assertStringStartsWith('Illuminate\\Container\\Container', $dump);
60+
$this->assertStringContainsString('#bindings', $dump);;
61+
$this->assertStringContainsString('#aliases', $dump);;
62+
$this->assertStringContainsString('#resolved', $dump);;
63+
$this->assertStringContainsString('#extenders', $dump);;
64+
$this->assertStringContainsString(static::class, $dump);;
65+
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);
10066

101-
$this->assertDumpMatchesFormat($expected, $container);
67+
$this->assertStringNotContainsString('#globalBeforeResolvingCallbacks', $dump);
10268
}
10369

10470
public function test_container_nested(): void
@@ -118,53 +84,6 @@ public function test_request(): void
11884
{
11985
$request = Request::create('/1');
12086

121-
$expected = <<<EOD
122-
Illuminate\Http\Request {
123-
+attributes: Symfony\Component\HttpFoundation\ParameterBag {}
124-
+request: Symfony\Component\HttpFoundation\InputBag {}
125-
+query: Symfony\Component\HttpFoundation\InputBag {}
126-
+server: Symfony\Component\HttpFoundation\ServerBag {
127-
SERVER_NAME: "localhost"
128-
SERVER_PORT: 80
129-
HTTP_HOST: "localhost"
130-
HTTP_USER_AGENT: "Symfony"
131-
HTTP_ACCEPT: "%s"
132-
HTTP_ACCEPT_LANGUAGE: "%s"
133-
HTTP_ACCEPT_CHARSET: "%s"
134-
REMOTE_ADDR: "%s"
135-
SCRIPT_NAME: ""
136-
SCRIPT_FILENAME: ""
137-
SERVER_PROTOCOL: "HTTP/1.1"
138-
REQUEST_TIME: %d
139-
REQUEST_TIME_FLOAT: %d.%d
140-
PATH_INFO: ""
141-
REQUEST_METHOD: "GET"
142-
REQUEST_URI: "/1"
143-
QUERY_STRING: ""
144-
}
145-
+files: Symfony\Component\HttpFoundation\FileBag {}
146-
+cookies: Symfony\Component\HttpFoundation\InputBag {}
147-
+headers: Symfony\Component\HttpFoundation\HeaderBag {
148-
host: "localhost"
149-
user-agent: "Symfony"
150-
accept: "%s"
151-
accept-language: "%s"
152-
accept-charset: "%s"
153-
#cacheControl: []
154-
}
155-
#defaultLocale: "en"
156-
-isHostValid: true
157-
-isForwardedValid: true
158-
pathInfo: "/1"
159-
requestUri: "/1"
160-
baseUrl: ""
161-
basePath: ""
162-
method: "GET"
163-
format: "html"
164-
…%d
165-
}
166-
EOD;
167-
16887
$dump = $this->getDump($request);
16988

17089
$this->assertStringStartsWith('Illuminate\\Http\\Request {', $dump);

tests/DatabaseCasterTest.php

Lines changed: 34 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,20 @@ public function test_eloquent_model(): void
2424

2525
$timestamp = $now->format('Y-m-d H:i:s');
2626

27-
$expected = <<<EOD
28-
Glhd\LaravelDumper\Tests\User {
29-
+"id": %d
30-
+"company_id": %d
31-
+"email": "[email protected]"
32-
+"name": "Chris"
33-
+"created_at": "{$timestamp}"
34-
+"updated_at": "{$timestamp}"
35-
isDirty(): true
36-
+exists: true
37-
+wasRecentlyCreated: true
38-
#relations: array:1 [
39-
"company" => Glhd\LaravelDumper\Tests\Company {
40-
+"id": %d
41-
+"name": "Galahad"
42-
+"created_at": "{$timestamp}"
43-
+"updated_at": "{$timestamp}"
44-
isDirty(): false
45-
+exists: true
46-
+wasRecentlyCreated: true
47-
#relations: []
48-
…%d
49-
}
50-
]
51-
#connection: "testing"
52-
#table: "users"
53-
#original: array:6 [
54-
"name" => "John"
55-
"email" => "[email protected]"
56-
"company_id" => %d
57-
"updated_at" => "{$timestamp}"
58-
"created_at" => "{$timestamp}"
59-
"id" => %d
60-
]
61-
#changes: []
62-
…%d
63-
}
64-
EOD;
27+
$dump = $this->getDump($user);
6528

66-
$this->assertDumpMatchesFormat($expected, $user);
29+
$this->assertStringStartsWith(User::class, $dump);
30+
$this->assertStringContainsString('id', $dump);
31+
$this->assertStringContainsString('company_id', $dump);
32+
$this->assertStringContainsString('email', $dump);
33+
$this->assertStringContainsString('name', $dump);
34+
$this->assertStringContainsString('isDirty()', $dump);
35+
$this->assertStringContainsString('exists', $dump);
36+
$this->assertStringContainsString('wasRecentlyCreated', $dump);
37+
$this->assertStringContainsString($timestamp, $dump);
38+
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);
39+
40+
$this->assertStringNotContainsString('escapeWhenCastingToString', $dump);
6741
}
6842

6943
public function test_query_builder(): void
@@ -72,20 +46,12 @@ public function test_query_builder(): void
7246
->where('email', '[email protected]')
7347
->limit(10);
7448

75-
$expected = <<<EOD
76-
Illuminate\Database\Query\Builder {
77-
sql: "select * from "users" where "email" = '[email protected]' limit 10"
78-
#connection: Illuminate\Database\SQLiteConnection {
79-
name: "testing"
80-
database: ":memory:"
81-
driver: "sqlite"
82-
…%d
83-
}
84-
…%d
85-
}
86-
EOD;
87-
88-
$this->assertDumpMatchesFormat($expected, $builder);
49+
$dump = $this->getDump($builder);
50+
51+
$this->assertStringStartsWith('Illuminate\\Database\\Query\\Builder {', $dump);
52+
$this->assertStringContainsString('select * from "users" where "email" = \'[email protected]\' limit 10', $dump);
53+
$this->assertStringContainsString('#connection', $dump);
54+
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);
8955
}
9056

9157
/** @see https://github.com/glhd/laravel-dumper/issues/6 */
@@ -94,22 +60,14 @@ public function test_where_between_statement(): void
9460
$builder = User::where('name', 'test')
9561
->whereBetween('id', [1, 2]);
9662

97-
$expected = <<<EOD
98-
Illuminate\Database\Eloquent\Builder {
99-
sql: "select * from "users" where "name" = 'test' and "id" between '1' and '2'"
100-
#connection: Illuminate\Database\SQLiteConnection {
101-
name: "testing"
102-
database: ":memory:"
103-
driver: "sqlite"
104-
…%d
105-
}
106-
#model: Glhd\LaravelDumper\Tests\User { …}
107-
#eagerLoad: []
108-
…%d
109-
}
110-
EOD;
63+
$dump = $this->getDump($builder);
11164

112-
$this->assertDumpMatchesFormat($expected, $builder);
65+
$this->assertStringStartsWith('Illuminate\\Database\\Eloquent\\Builder {', $dump);
66+
$this->assertStringContainsString('select * from "users" where "name" = \'test\' and "id" between \'1\' and \'2\'', $dump);
67+
$this->assertStringContainsString('#connection', $dump);
68+
$this->assertStringContainsString('#model', $dump);
69+
$this->assertStringContainsString(User::class, $dump);
70+
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);
11371
}
11472

11573
public function test_eloquent_builder(): void
@@ -133,116 +91,15 @@ public function test_eloquent_relation(): void
13391
Company::create(['id' => 1, 'name' => 'Galahad']);
13492
$user = User::create(['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'company_id' => 1]);
13593

136-
$expected = <<<EOD
137-
Illuminate\Database\Eloquent\Relations\BelongsTo {
138-
sql: "select * from "companies" where "companies"."id" = '1'"
139-
#connection: Illuminate\Database\SQLiteConnection {
140-
name: "testing"
141-
database: ":memory:"
142-
driver: "sqlite"
143-
…%d
144-
}
145-
#parent: Glhd\LaravelDumper\Tests\User { …}
146-
#related: Glhd\LaravelDumper\Tests\Company { …}
147-
…%d
148-
}
149-
EOD;
94+
$dump = $this->getDump($user->company());
15095

151-
$this->assertDumpMatchesFormat($expected, $user->company());
152-
}
153-
154-
public function test_unexpected_database_connections(): void
155-
{
156-
// Database connections don't necessarily need to have a $config
157-
// array. If they don't, we just return the original.
158-
159-
$conn = new class() implements ConnectionInterface {
160-
public $foo = 'bar';
161-
162-
public function table($table, $as = null)
163-
{
164-
}
165-
166-
public function raw($value)
167-
{
168-
}
169-
170-
public function selectOne($query, $bindings = [], $useReadPdo = true)
171-
{
172-
}
173-
174-
public function select($query, $bindings = [], $useReadPdo = true)
175-
{
176-
}
177-
178-
public function cursor($query, $bindings = [], $useReadPdo = true)
179-
{
180-
}
181-
182-
public function insert($query, $bindings = [])
183-
{
184-
}
185-
186-
public function update($query, $bindings = [])
187-
{
188-
}
189-
190-
public function delete($query, $bindings = [])
191-
{
192-
}
193-
194-
public function statement($query, $bindings = [])
195-
{
196-
}
197-
198-
public function affectingStatement($query, $bindings = [])
199-
{
200-
}
201-
202-
public function unprepared($query)
203-
{
204-
}
205-
206-
public function prepareBindings(array $bindings)
207-
{
208-
}
209-
210-
public function transaction(Closure $callback, $attempts = 1)
211-
{
212-
}
213-
214-
public function beginTransaction()
215-
{
216-
}
217-
218-
public function commit()
219-
{
220-
}
221-
222-
public function rollBack()
223-
{
224-
}
225-
226-
public function transactionLevel()
227-
{
228-
}
229-
230-
public function pretend(Closure $callback)
231-
{
232-
}
233-
234-
public function getDatabaseName()
235-
{
236-
}
237-
};
238-
239-
$expected = <<<EOD
240-
Illuminate\Database\ConnectionInterface@anonymous {
241-
+foo: "bar"
242-
}
243-
EOD;
244-
245-
$this->assertDumpEquals($expected, $conn);
96+
$this->assertStringStartsWith('Illuminate\\Database\\Eloquent\\Relations\\BelongsTo {', $dump);
97+
$this->assertStringContainsString('select * from "companies" where "companies"."id" = \'1\'', $dump);
98+
$this->assertStringContainsString('#parent', $dump);
99+
$this->assertStringContainsString('#related', $dump);
100+
$this->assertStringContainsString(User::class, $dump);
101+
$this->assertStringContainsString(Company::class, $dump);
102+
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);
246103
}
247104

248105
protected function defineDatabaseMigrations()

0 commit comments

Comments
 (0)