atlas/testing
Atlas.Testing provides lightweight helpers and utilities for testing Atlas packages, aimed at simplifying test setup and improving consistency across Atlas-related projects. Suitable for package authors who need reusable testing support.
Installation:
composer require atlas/testing --dev
Ensure it’s added to require-dev in composer.json to avoid production bloat.
First Use Case:
Extend AtlasTestCase to test Atlas-specific functionality. Example: Testing a many-to-many relationship:
use Atlas\Testing\AtlasTestCase;
class PostCategoryTest extends AtlasTestCase
{
public function test_many_to_many_relationship()
{
$post = $this->createPost(['title' => 'Atlas Testing']);
$category = $this->createCategory(['name' => 'Laravel']);
// Attach and assert relationship
$post->categories()->attach($category);
$this->assertCount(1, $post->categories);
$this->assertCount(1, $category->posts);
}
}
Where to Look First:
AtlasTestCase: Base class with helper methods like create{Model}, assertTableSchema, and assertCascadeDelete.1.1.0 (many-to-many support) and 1.2.0 (tables without primary keys) for edge-case testing.tests directory for real-world usage patterns.Model and Relationship Testing:
create{Model} helpers (e.g., createUser(), createPost()) to scaffold test data with relationships.
$user = $this->createUser(['name' => 'Test User']);
$this->createPost(['user_id' => $user->id, 'title' => 'Test Post']);
$this->assertRelatedTo($post, $user, 'user'); // 1:1 or 1:N
$this->assertManyToManyRelation($post, $category); // M:N
Schema Validation:
$this->assertTableSchema('posts', [
'columns' => ['id', 'user_id', 'title', 'created_at'],
'primaryKey' => 'id', // or null for no PK
]);
Cascade Operations:
$this->assertCascadeDelete($parent, $child); // Child deleted when parent is deleted
Dynamic Data Manipulation:
AtlasTestCase’s query builders to simulate complex operations:
$result = $this->query('SELECT * FROM posts WHERE user_id = ?', [$user->id]);
$this->assertCount(1, $result);
Test-Driven Development (TDD) for Atlas:
public function test_polymorphic_relation()
{
$commentable = $this->createPost(['title' => 'Polymorphic Test']);
$comment = $this->createComment(['commentable_id' => $commentable->id, 'commentable_type' => Post::class]);
$this->assertInstanceOf(Post::class, $comment->commentable);
}
Regression Testing:
public function test_schema_migration()
{
$this->assertTableExists('posts');
$this->assertColumnExists('posts', 'user_id');
$this->assertColumnDoesNotExist('posts', 'old_column');
}
Integration with Laravel Testing:
RefreshDatabase or DatabaseMigrations for full-stack tests:
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserTest extends AtlasTestCase
{
use RefreshDatabase;
public function test_user_creation()
{
$user = $this->createUser(['email' => 'test@example.com']);
$this->assertDatabaseHas('users', ['email' => 'test@example.com']);
}
}
Custom Helpers:
AtlasTestCase to add project-specific helpers:
class CustomAtlasTestCase extends AtlasTestCase
{
protected function createAdminUser(array $attributes = [])
{
return $this->createUser(array_merge(['role' => 'admin'], $attributes));
}
}
Mocking Atlas:
$mockAtlas = $this->mockAtlas();
$mockAtlas->shouldReceive('query')->once()->andReturn([$expectedRecord]);
CI/CD Pipeline:
phpunit.xml or pest.php:
<testsuites>
<testsuite name="Atlas Tests">
<directory>./tests/Atlas</directory>
</testsuite>
</testsuites>
Stagnant Package:
Atlas Version Lock:
composer.json to match the package’s compatibility:
"require": {
"atlasphp/atlas": "1.2.*"
}
Limited Documentation:
Database State Leaks:
AtlasTestCase’s transactions or Laravel’s RefreshDatabase:
use Illuminate\Foundation\Testing\RefreshDatabase;
class PostTest extends AtlasTestCase
{
use RefreshDatabase;
}
Assertion Overhead:
assertDatabaseHas) where possible.Enable Query Logging:
$this->app['db']->enableQueryLog();
$this->createUser(['name' => 'Test']);
dd($this->app['db']->getQueryLog());
Inspect Test Data:
$user = $this->createUser(['name' => 'Debug User']);
dd($user->fresh()->toArray());
Isolate Flaky Tests:
$this->retry(3, function () {
$this->assertCount(1, $user->posts);
}, 100); // Retry 3 times with 100ms delay
Database Configuration:
.env.testing or phpunit.xml points to a clean test database:
<env name="DB_DATABASE" value="atlas_testing"/>
Timezone Handling:
$this->app->setTimezone('UTC');
Primary Key Assumptions:
1.2.0), but some assertions may assume PKs exist. Explicitly handle edge cases:
$this->assertTableSchema('log_entries', [
'columns' => ['message', 'created_at'],
'primaryKey' => null, // No PK
]);
class CustomAssertions extends AtlasTestCase
{
public function assertSoftDeletes($model, $deletedAt = null)
{
$deletedAt = $deletedAt ?? now();
$this->assertSoftDeleted($model, $deletedAt);
$this->assertNull($
How can I help you explore Laravel packages today?