Skip to content

Commit

Permalink
Make tests more resilient to change
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Dec 16, 2023
1 parent 8c776b5 commit c424a07
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 273 deletions.
111 changes: 15 additions & 96 deletions tests/CasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@ public function test_carbon_date(): void
{
$now = Carbon::parse('2022-01-18 19:44:02.572622', 'America/New_York');

$expected = <<<EOD
Carbon\Carbon @%d {
date: 2022-01-18 19:44:02.572622 America/New_York (-05:00)
#endOfTime: false
#startOfTime: false
#constructedObjectId: "%s"
#dumpProperties: array:3 [
0 => "date"
1 => "timezone_type"
2 => "timezone"
]
…%d
}
EOD;
$dump = $this->getDump($now);

$this->assertDumpMatchesFormat($expected, $now);
$this->assertStringStartsWith('Carbon\\Carbon', $dump);
$this->assertStringContainsString('date: 2022-01-18 19:44:02.572622 America/New_York (-05:00)', $dump);;
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);

$this->assertStringNotContainsString('localMacros', $dump);
}

public function test_package_can_be_disabled(): void
Expand Down Expand Up @@ -63,42 +54,17 @@ public function test_container(): void
$container->extend('bar', fn() => $this);
$container->make('bar');

$fqcn = static::class;
$dump = $this->getDump($container);

$expected = <<<EOD
Illuminate\Container\Container {
#bindings: array:1 [
"{$fqcn}" => array:2 [
"concrete" => Closure() {
class: "{$fqcn}"
this: {$fqcn} {#1 …}
file: "%s"
line: "%d to %d"
}
"shared" => false
]
]
#aliases: array:1 [
"bar" => "{$fqcn}"
]
#resolved: array:1 [
"{$fqcn}" => true
]
#extenders: array:1 [
"{$fqcn}" => array:1 [
0 => Closure() {
class: "{$fqcn}"
this: Glhd\LaravelDumper\Tests\CasterTest {#1 …}
file: "%s"
line: "%d to %d"
}
]
]
…%d
}
EOD;
$this->assertStringStartsWith('Illuminate\\Container\\Container', $dump);
$this->assertStringContainsString('#bindings', $dump);;
$this->assertStringContainsString('#aliases', $dump);;
$this->assertStringContainsString('#resolved', $dump);;
$this->assertStringContainsString('#extenders', $dump);;
$this->assertStringContainsString(static::class, $dump);;
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);

$this->assertDumpMatchesFormat($expected, $container);
$this->assertStringNotContainsString('#globalBeforeResolvingCallbacks', $dump);
}

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

$expected = <<<EOD
Illuminate\Http\Request {
+attributes: Symfony\Component\HttpFoundation\ParameterBag {}
+request: Symfony\Component\HttpFoundation\InputBag {}
+query: Symfony\Component\HttpFoundation\InputBag {}
+server: Symfony\Component\HttpFoundation\ServerBag {
SERVER_NAME: "localhost"
SERVER_PORT: 80
HTTP_HOST: "localhost"
HTTP_USER_AGENT: "Symfony"
HTTP_ACCEPT: "%s"
HTTP_ACCEPT_LANGUAGE: "%s"
HTTP_ACCEPT_CHARSET: "%s"
REMOTE_ADDR: "%s"
SCRIPT_NAME: ""
SCRIPT_FILENAME: ""
SERVER_PROTOCOL: "HTTP/1.1"
REQUEST_TIME: %d
REQUEST_TIME_FLOAT: %d.%d
PATH_INFO: ""
REQUEST_METHOD: "GET"
REQUEST_URI: "/1"
QUERY_STRING: ""
}
+files: Symfony\Component\HttpFoundation\FileBag {}
+cookies: Symfony\Component\HttpFoundation\InputBag {}
+headers: Symfony\Component\HttpFoundation\HeaderBag {
host: "localhost"
user-agent: "Symfony"
accept: "%s"
accept-language: "%s"
accept-charset: "%s"
#cacheControl: []
}
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
pathInfo: "/1"
requestUri: "/1"
baseUrl: ""
basePath: ""
method: "GET"
format: "html"
…%d
}
EOD;

$dump = $this->getDump($request);

$this->assertStringStartsWith('Illuminate\\Http\\Request {', $dump);
Expand Down
211 changes: 34 additions & 177 deletions tests/DatabaseCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,20 @@ public function test_eloquent_model(): void

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

$expected = <<<EOD
Glhd\LaravelDumper\Tests\User {
+"id": %d
+"company_id": %d
+"email": "[email protected]"
+"name": "Chris"
+"created_at": "{$timestamp}"
+"updated_at": "{$timestamp}"
isDirty(): true
+exists: true
+wasRecentlyCreated: true
#relations: array:1 [
"company" => Glhd\LaravelDumper\Tests\Company {
+"id": %d
+"name": "Galahad"
+"created_at": "{$timestamp}"
+"updated_at": "{$timestamp}"
isDirty(): false
+exists: true
+wasRecentlyCreated: true
#relations: []
…%d
}
]
#connection: "testing"
#table: "users"
#original: array:6 [
"name" => "John"
"email" => "[email protected]"
"company_id" => %d
"updated_at" => "{$timestamp}"
"created_at" => "{$timestamp}"
"id" => %d
]
#changes: []
…%d
}
EOD;
$dump = $this->getDump($user);

$this->assertDumpMatchesFormat($expected, $user);
$this->assertStringStartsWith(User::class, $dump);
$this->assertStringContainsString('id', $dump);
$this->assertStringContainsString('company_id', $dump);
$this->assertStringContainsString('email', $dump);
$this->assertStringContainsString('name', $dump);
$this->assertStringContainsString('isDirty()', $dump);
$this->assertStringContainsString('exists', $dump);
$this->assertStringContainsString('wasRecentlyCreated', $dump);
$this->assertStringContainsString($timestamp, $dump);
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);

$this->assertStringNotContainsString('escapeWhenCastingToString', $dump);
}

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

$expected = <<<EOD
Illuminate\Database\Query\Builder {
sql: "select * from "users" where "email" = '[email protected]' limit 10"
#connection: Illuminate\Database\SQLiteConnection {
name: "testing"
database: ":memory:"
driver: "sqlite"
…%d
}
…%d
}
EOD;

$this->assertDumpMatchesFormat($expected, $builder);
$dump = $this->getDump($builder);

$this->assertStringStartsWith('Illuminate\\Database\\Query\\Builder {', $dump);
$this->assertStringContainsString('select * from "users" where "email" = \'[email protected]\' limit 10', $dump);
$this->assertStringContainsString('#connection', $dump);
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);
}

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

$expected = <<<EOD
Illuminate\Database\Eloquent\Builder {
sql: "select * from "users" where "name" = 'test' and "id" between '1' and '2'"
#connection: Illuminate\Database\SQLiteConnection {
name: "testing"
database: ":memory:"
driver: "sqlite"
…%d
}
#model: Glhd\LaravelDumper\Tests\User { …}
#eagerLoad: []
…%d
}
EOD;
$dump = $this->getDump($builder);

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

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

$expected = <<<EOD
Illuminate\Database\Eloquent\Relations\BelongsTo {
sql: "select * from "companies" where "companies"."id" = '1'"
#connection: Illuminate\Database\SQLiteConnection {
name: "testing"
database: ":memory:"
driver: "sqlite"
…%d
}
#parent: Glhd\LaravelDumper\Tests\User { …}
#related: Glhd\LaravelDumper\Tests\Company { …}
…%d
}
EOD;
$dump = $this->getDump($user->company());

$this->assertDumpMatchesFormat($expected, $user->company());
}

public function test_unexpected_database_connections(): void
{
// Database connections don't necessarily need to have a $config
// array. If they don't, we just return the original.

$conn = new class() implements ConnectionInterface {
public $foo = 'bar';

public function table($table, $as = null)
{
}

public function raw($value)
{
}

public function selectOne($query, $bindings = [], $useReadPdo = true)
{
}

public function select($query, $bindings = [], $useReadPdo = true)
{
}

public function cursor($query, $bindings = [], $useReadPdo = true)
{
}

public function insert($query, $bindings = [])
{
}

public function update($query, $bindings = [])
{
}

public function delete($query, $bindings = [])
{
}

public function statement($query, $bindings = [])
{
}

public function affectingStatement($query, $bindings = [])
{
}

public function unprepared($query)
{
}

public function prepareBindings(array $bindings)
{
}

public function transaction(Closure $callback, $attempts = 1)
{
}

public function beginTransaction()
{
}

public function commit()
{
}

public function rollBack()
{
}

public function transactionLevel()
{
}

public function pretend(Closure $callback)
{
}

public function getDatabaseName()
{
}
};

$expected = <<<EOD
Illuminate\Database\ConnectionInterface@anonymous {
+foo: "bar"
}
EOD;

$this->assertDumpEquals($expected, $conn);
$this->assertStringStartsWith('Illuminate\\Database\\Eloquent\\Relations\\BelongsTo {', $dump);
$this->assertStringContainsString('select * from "companies" where "companies"."id" = \'1\'', $dump);
$this->assertStringContainsString('#parent', $dump);
$this->assertStringContainsString('#related', $dump);
$this->assertStringContainsString(User::class, $dump);
$this->assertStringContainsString(Company::class, $dump);
$this->assertMatchesRegularExpression('/\s*…\d+\n}$/', $dump);
}

protected function defineDatabaseMigrations()
Expand Down

0 comments on commit c424a07

Please sign in to comment.