Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Test Pack Laravel Package

symfony/test-pack

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require --dev symfony/test-pack
    

    Add the TestPack to your composer.json under extra:

    "extra": {
        "symfony": {
            "pack": ["symfony/test-pack"]
        }
    }
    
  2. First Use Case: Functional Test Create a basic test class in tests/Functional/:

    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    
    class ExampleTest extends WebTestCase
    {
        public function testHomepage(): void
        {
            $client = static::createClient();
            $client->request('GET', '/');
            $this->assertResponseIsSuccessful();
        }
    }
    
  3. Key Files to Explore

    • config/packages/test.php (Symfony’s built-in test config)
    • tests/Functional/ (Default test directory structure)
    • var/cache/test/ (Test cache location)

Implementation Patterns

Common Workflows

  1. Database Testing Use DatabaseTestCase for isolated DB tests:

    use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
    
    class UserTest extends KernelTestCase
    {
        protected function setUp(): void
        {
            self::bootKernel();
            $this->loadFixtures([UserFixtures::class]);
        }
    }
    
  2. Client-Based Testing Leverage WebTestCase for HTTP interactions:

    $client = static::createClient();
    $client->loginUser($user); // Auth helper
    $client->request('POST', '/api/users', ['name' => 'John']);
    $this->assertJson($client->getResponse()->getContent());
    
  3. Integration with PHPUnit Annotate tests with @group integration for CI filtering:

    /**
     * @group integration
     */
    class PaymentTest extends WebTestCase { ... }
    

Integration Tips

  • Fixtures: Use DoctrineFixturesBundle for reusable test data.
  • Caching: Clear test cache with php bin/console cache:clear --env=test.
  • Parallel Tests: Enable PHPUnit’s --parallel flag for faster execution.

Gotchas and Tips

Pitfalls

  1. Environment Mismatch Ensure APP_ENV=test and APP_DEBUG=1 in .env.test. Fix: Run php bin/console config:dump --env=test to debug.

  2. Static Analysis Conflicts TestPack may trigger static analyzers (e.g., Psalm) due to static:: calls. Fix: Add @mixin annotations or exclude test files.

  3. Slow Tests Avoid self::bootKernel() in every test; reuse the kernel where possible.

Debugging Tips

  • Dumping Responses
    $this->assertResponseStatusCodeSame(200);
    $this->assertSelectorTextContains('h1', 'Welcome');
    
  • Logging Enable debug logging in config/packages/dev/test.php:
    monolog:
        handlers:
            test:
                type: stream
                path: "%kernel.logs_dir%/%kernel.environment%.log"
                level: debug
    

Extension Points

  1. Custom Test Cases Extend WebTestCase for shared setup:

    abstract class ApiTestCase extends WebTestCase
    {
        protected function createAuthenticatedClient(): Client
        {
            $client = static::createClient();
            $client->loginUser($this->createTestUser());
            return $client;
        }
    }
    
  2. Test Utilities Create a tests/_utils/ directory for helper classes (e.g., TestClientFactory).

  3. Custom Assertions Use PHPUnit’s assertThat() with custom matchers:

    $this->assertThat(
        $client->getResponse()->getContent(),
        $this->stringContains('Expected text')
    );
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware