Giter Club home page Giter Club logo

database-seeder's Introduction

Database Seeder

PHP Latest Version on Packagist GitHub Tests Action Status Total Downloads

When you build apps that use databases, it's really important to make sure your database works right. This means checking if it stores, changes, and gives back data the way it's supposed to. But sometimes, testing databases can be tricky and a bit boring. You might have to write a lot of complicated commands and be very careful about how data is added or removed.

What the package offers

  1. Easy Testing: With Spiral, you don't need to deal with complex commands. The tools are simple to use, which means your tests are easier to write and understand.

  2. Different Ways to Reset Your Database: After you test something, you need to make your database clean again for the next test. Spiral has different ways to do this, like the Transaction, Migration, Refresh, and SqlFile methods. Each one has its own way of working, so you can choose what fits best for your test.

  3. Seeders and Factories: These are like shortcuts to fill your database with test data. This data looks like the real data you would use in your app. You can quickly set up the data you need for testing with these tools.

  4. Checking Your Database: After you do something in your database, you want to make sure it worked right. Spiral's tools let you check if the data is there or not, and if your database structure is correct.

It's a great for any developer, no matter how much experience you have. They help make sure your database is doing what it should, which is really important for your app to work well.

Requirements

Make sure that your server is configured with following PHP version and extensions:

  • PHP 8.1+
  • Spiral framework 3.7+

Documentation, Installation, and Usage Instructions

See the documentation for detailed installation and usage instructions.

License

The MIT License (MIT). Please see License File for more information.

database-seeder's People

Contributors

alexey-shapilov avatar aquaminer avatar butschster avatar dependabot[bot] avatar markinigor avatar msmakouz avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

database-seeder's Issues

The error is thrown when calling ->createOne on factory instance. Spiral\DatabaseSeeder\Factory\Exception\OutsideScopeException: The container is not available.

Here is an example of usage

final class LoginControllerTest extends TestCase
{
    private readonly FakeHttp $http;
    private readonly UserFactory $userFactory;

    protected function setUp(): void
    {
        parent::setUp();

        $this->http = $this->fakeHttp();

        $this->userFactory = UserFactory::new();
    }

    public function testLogin(): void
    {
        $user = $this->userFactory->createOne();
     
        $response = $this->http
            ->postJson(
                uri: '/api/auth/login',
                data: [
                    'email' => '[email protected]',
                    'password' => 'supersecret'
                ],
                headers: ['Content-type' => 'application/json']
            );

        $response->assertStatus(200);
        $response->assertBodyContains('all good');
    }
}

Help? `db:seed` command isn't working

I'm following the tutorial (https://spiral.dev/docs/cookbook-quick-start/current/en#blogseeder) and I'm trying to set up seed data.
I'm using SQLite, and all the other DB operations in the tutorial work so far. However, when I run the seed command, nothing happens.

... $ php app.php db:seed -vvv
[SQLiteDriver] SELECT COUNT('sql') FROM 'sqlite_master' WHERE type = 'table' and name = ?
[SQLiteDriver] SELECT sql FROM sqlite_master WHERE type = 'table' and name = ?
[SQLiteDriver] PRAGMA TABLE_INFO('comments')
[SQLiteDriver] PRAGMA TABLE_INFO('comments')
[SQLiteDriver] PRAGMA index_list('comments')
[SQLiteDriver] PRAGMA INDEX_XINFO('comments_index_post_id_65a2e63080553')
[SQLiteDriver] PRAGMA INDEX_INFO('comments_index_post_id_65a2e63080553')
[SQLiteDriver] PRAGMA INDEX_XINFO('comments_index_author_id_65a2e6307c085')
[SQLiteDriver] PRAGMA INDEX_INFO('comments_index_author_id_65a2e6307c085')
[SQLiteDriver] PRAGMA foreign_key_list('comments')
[SQLiteDriver] PRAGMA TABLE_INFO('comments')
[SQLiteDriver] SELECT COUNT('sql') FROM 'sqlite_master' WHERE type = 'table' and name = ?
[SQLiteDriver] SELECT sql FROM sqlite_master WHERE type = 'table' and name = ?
[SQLiteDriver] PRAGMA TABLE_INFO('posts')
[SQLiteDriver] PRAGMA TABLE_INFO('posts')
[SQLiteDriver] PRAGMA index_list('posts')
[SQLiteDriver] PRAGMA INDEX_XINFO('posts_index_author_id_65a2e6308077f')
[SQLiteDriver] PRAGMA INDEX_INFO('posts_index_author_id_65a2e6308077f')
[SQLiteDriver] PRAGMA foreign_key_list('posts')
[SQLiteDriver] PRAGMA TABLE_INFO('posts')
[SQLiteDriver] SELECT COUNT('sql') FROM 'sqlite_master' WHERE type = 'table' and name = ?
[SQLiteDriver] SELECT sql FROM sqlite_master WHERE type = 'table' and name = ?
[SQLiteDriver] PRAGMA TABLE_INFO('users')
[SQLiteDriver] PRAGMA TABLE_INFO('users')
[SQLiteDriver] PRAGMA index_list('users')
[SQLiteDriver] PRAGMA foreign_key_list('users')
[SQLiteDriver] PRAGMA TABLE_INFO('users')
[SQLiteDriver] SELECT COUNT('sql') FROM 'sqlite_master' WHERE type = 'table' and name = ?
[SQLiteDriver] SELECT sql FROM sqlite_master WHERE type = 'table' and name = ?
[SQLiteDriver] PRAGMA TABLE_INFO('comments')
[SQLiteDriver] PRAGMA TABLE_INFO('comments')
[SQLiteDriver] PRAGMA index_list('comments')
[SQLiteDriver] PRAGMA INDEX_XINFO('comments_index_post_id_65a2e63080553')
[SQLiteDriver] PRAGMA INDEX_INFO('comments_index_post_id_65a2e63080553')
[SQLiteDriver] PRAGMA INDEX_XINFO('comments_index_author_id_65a2e6307c085')
[SQLiteDriver] PRAGMA INDEX_INFO('comments_index_author_id_65a2e6307c085')
[SQLiteDriver] PRAGMA foreign_key_list('comments')
[SQLiteDriver] PRAGMA TABLE_INFO('comments')
[SQLiteDriver] SELECT COUNT('sql') FROM 'sqlite_master' WHERE type = 'table' and name = ?
[SQLiteDriver] SELECT sql FROM sqlite_master WHERE type = 'table' and name = ?
[SQLiteDriver] PRAGMA TABLE_INFO('posts')
[SQLiteDriver] PRAGMA TABLE_INFO('posts')
[SQLiteDriver] PRAGMA index_list('posts')
[SQLiteDriver] PRAGMA INDEX_XINFO('posts_index_author_id_65a2e6308077f')
[SQLiteDriver] PRAGMA INDEX_INFO('posts_index_author_id_65a2e6308077f')
[SQLiteDriver] PRAGMA foreign_key_list('posts')
[SQLiteDriver] PRAGMA TABLE_INFO('posts')
[SQLiteDriver] SELECT COUNT('sql') FROM 'sqlite_master' WHERE type = 'table' and name = ?
[SQLiteDriver] SELECT sql FROM sqlite_master WHERE type = 'table' and name = ?
[SQLiteDriver] PRAGMA TABLE_INFO('users')
[SQLiteDriver] PRAGMA TABLE_INFO('users')
[SQLiteDriver] PRAGMA index_list('users')
[SQLiteDriver] PRAGMA foreign_key_list('users')
[SQLiteDriver] PRAGMA TABLE_INFO('users')
    0 [▓░░░░░░░░░░░░░░░░░░░░░░░░░░░] < 1 sec 20.0 MiB

[OK] Database seeding completed successfully.

... $ sqlite3 runtime/db.sqlite
SQLite version 3.44.2 2023-11-24 11:41:44
Enter ".help" for usage hints.
sqlite> .tables
comments    migrations  posts       users
sqlite> select * from users;
sqlite>

I'm pretty sure I've got all the code correct (Entities, Seed, etc.) as no errors are being thrown.

Ideas?

`@note` annotation causes error from Doctrine

Among the changes in this commit are some annotations in the PhpDoc comments. The @note annotation is throwing errors during configure:

With:

/**
 * Create many entities with persisting them to the database.
 *
 * @param bool|null $cleanHeap Clean the heap after creating entities.
 *
 * @note To change the default value use {@see static::$cleanHeap} property.
 */

produces:

[Spiral\Attributes\Exception\SemanticAttributeException]
[Semantical Error] The annotation "@note" in method Spiral\DatabaseSeeder\Factory\AbstractFactory::create() was never imported. Did you maybe forget to add a "use" statement for this annotation?
in vendor/spiral/attributes/src/Internal/DoctrineAnnotationReader.php:87

Previous: [Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "@note" in method Spiral\DatabaseSeeder\Factory\AbstractFactory::create() was never imported. Did you maybe forget to add a "use" statement for this annotation?
in vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:36

Removing @note:

/**
 * Create many entities with persisting them to the database.
 *
 * @param bool|null $cleanHeap Clean the heap after creating entities.
 *
 * To change the default value use {@see static::$cleanHeap} property.
 */

stops the error from occurring.


i presume the same will be true of the createOne() method.

i'm happy to submit a change for these, if someone wants to offer an alternate annotation, or make the call to just remove @note from the remark...

Suggestions?

Compatibility issue with spiral/testing 2.6.2 causing fatal error in database-seeder

Description

A fatal error occurs when using spiral-packages/database-seeder at version 2.4.0 with spiral/testing at version 2.6.2. The error does not occur when spiral/testing is downgraded to version 2.5.0.

Error Message

Fatal error: Access level to Spiral\DatabaseSeeder\TestCase::setUpTraits() must be protected (as in class Spiral\Testing\TestCase) or weaker in /app/vendor/spiral-packages/database-seeder/src/TestCase.php on line 27

Environment

  • Operating System: WSL
  • PHP Version: 8.2
  • spiral-packages/database-seeder Version: 2.4.0
  • spiral/testing Version: 2.6.2

Steps to Reproduce

  1. Install spiral-packages/database-seeder at version 2.4.0.
  2. Install spiral/testing at version 2.6.2.
  3. Run any test that utilizes the Spiral\DatabaseSeeder\TestCase.
  4. Observe the fatal error.

Expected Behavior

The spiral-packages/database-seeder should work seamlessly with spiral/testing at any version, including 2.6.2, without causing a fatal error.

Actual Behavior

A fatal error occurs when using the specified versions together, which does not happen with the older version of spiral/testing (2.5.0).

Additional Information

  • The issue seems to be related to the visibility of setUpTraits method in TestCase.php.
  • This issue is blocking the upgrade to the latest spiral/testing version.

Please let me know if there is any additional information I can provide to help resolve this issue. Thank you for your attention to this matter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.