196 search results

…and it is a great experience! However, I noticed a memory issue whlie using Foundry factories and `doctrine:fixtures:load`. I'm trying to use these to setup testing data for my application. While preparing basic config like some users, organization and permissions everything was…
…thread is about. Namely: why in the tutorial Ryan was able to use `browser->actingAs($user)` with `$user` created with Foundry factory and when I try to do this I get the 500 error mentioned by |mention:81069| at the beginning of this thread? And…
…that's actually on purpose, entity factories from the Foundry lib need that to add more magic in tests - those proxy objects helps you to do not worry about the problem with refreshing the entity in tests. But that AppEntityUserProxy actually extends your User class…
37 lines | tests/Functional/VoyageControllerTest.php
// ... lines 1 - 2
namespace App\Tests\Functional;
use App\Factory\PlanetFactory;
use App\Factory\VoyageFactory;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Zenstruck\Browser\Test\HasBrowser;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
class VoyageControllerTest extends WebTestCase
{
use ResetDatabase;
use Factories;
use HasBrowser;
public function testCreateVoyage()
{
PlanetFactory::createOne([
'name' => 'Earth',
]);
VoyageFactory::createOne();
$this->browser()
->visit('/')
->click('Voyages')
->click('New Voyage')
->fillField('Purpose', 'Test voyage')
->selectFieldOption('Planet', 'Earth')
->click('Save')
->assertElementCount('table tbody tr', 2)
->assertSee('Bon voyage')
;
}
}
See Code Block in Script
…I have no idea tbh. I've never used MSSQL, and I don't think the creator of Foundry has either, so it's quite possible that it's not coded correctly for that. You could try using https://github.com/dmaicher/doctrine-test-bundle…
weaverryan
weaverryan
Read Full Comment
Hey Cameron, This is actually from doctrine/orm package. This might be an indirect deprecation for other packages that uses doctrine/orm in their dependecies including Foundry like you said. You can just ignore it for now and it should be fixed by package maintainers…
Awesome! And thanks for sharing that this worked. I couldn't think of any reason why simply "renaming the namespace" would cause any issues, but I had never tried it before with Foundry!
weaverryan
weaverryan
Read Full Comment
…I changed RoleFixture by using the factory of Foundry... Yes, I agree: I think the mixture of the DependentFixtures system and Foundry won't play nicely together. So if you're heavily dependent on `DependentFixtures`, stay with that. But eventually, using Foundry would be nice :)…
weaverryan
weaverryan
Read Full Comment
89 lines | src/Factory/UserFactory.php
// ... lines 1 - 2
namespace App\Factory;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
/**
* @extends ModelFactory<User>
// ... lines 14 - 29
*/
final class UserFactory extends ModelFactory
{
const USERNAMES = [
'FlamingInferno',
'ScaleSorcerer',
'TheDragonWithBadBreath',
'BurnedOut',
'ForgotMyOwnName',
'ClumsyClaws',
'HoarderOfUselessTrinkets',
];
// ... lines 42 - 47
public function __construct(
private UserPasswordHasherInterface $passwordHasher
)
{
parent::__construct();
}
// ... lines 54 - 59
protected function getDefaults(): array
{
return [
'email' => self::faker()->email(),
'password' => 'password',
'username' => self::faker()->randomElement(self::USERNAMES) . self::faker()->randomNumber(3),
];
}
// ... lines 68 - 71
protected function initialize(): self
{
return $this
->afterInstantiate(function(User $user): void {
$user->setPassword($this->passwordHasher->hashPassword(
$user,
$user->getPassword()
));
})
;
}
protected static function getClass(): string
{
return User::class;
}
}
See Code Block in Script
74 lines | src/Factory/DragonTreasureFactory.php
// ... lines 1 - 2
namespace App\Factory;
use App\Entity\DragonTreasure;
use App\Repository\DragonTreasureRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;
/**
* @extends ModelFactory<DragonTreasure>
*
* @method DragonTreasure|Proxy create(array|callable $attributes = [])
* @method static DragonTreasure|Proxy createOne(array $attributes = [])
* @method static DragonTreasure|Proxy find(object|array|mixed $criteria)
* @method static DragonTreasure|Proxy findOrCreate(array $attributes)
* @method static DragonTreasure|Proxy first(string $sortedField = 'id')
* @method static DragonTreasure|Proxy last(string $sortedField = 'id')
* @method static DragonTreasure|Proxy random(array $attributes = [])
* @method static DragonTreasure|Proxy randomOrCreate(array $attributes = [])
* @method static DragonTreasureRepository|RepositoryProxy repository()
* @method static DragonTreasure[]|Proxy[] all()
* @method static DragonTreasure[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static DragonTreasure[]|Proxy[] createSequence(array|callable $sequence)
* @method static DragonTreasure[]|Proxy[] findBy(array $attributes)
* @method static DragonTreasure[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static DragonTreasure[]|Proxy[] randomSet(int $number, array $attributes = [])
*/
final class DragonTreasureFactory extends ModelFactory
{
// ... lines 32 - 36
public function __construct()
{
parent::__construct();
}
// ... lines 41 - 46
protected function getDefaults(): array
{
return [
'coolFactor' => self::faker()->randomNumber(),
'description' => self::faker()->text(),
'isPublished' => self::faker()->boolean(),
'name' => self::faker()->text(255),
'plunderedAt' => \DateTimeImmutable::createFromMutable(self::faker()->dateTime()),
'value' => self::faker()->randomNumber(),
];
}
// ... lines 58 - 61
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(DragonTreasure $dragonTreasure): void {})
;
}
protected static function getClass(): string
{
return DragonTreasure::class;
}
}
See Code Block in Script
Hello, Can we make fixtures for [Embeddables] class ? For example "User Entity" use "Adress embeddable class" who store the field City, Town, Street How we can use Foundry to generate many User ? Thank you.
…generate some text but text are in Latin I added configuration in file config/package/zenstruck_foundry.yaml ``` when@dev: &dev # See full configuration: https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#full-default-bundle-configuration zenstruck_foundry: # Whether to auto-refresh proxies by default…
Hey gazzatav! Great catch on that error message - both the mistake and the suggestion! I've created a pull request for it - https://github.com/zenstruck/foundry/pull/289 Cheers!
weaverryan
weaverryan
Read Full Comment
Hey Gary, First of all, I'm not sure what you're doing, I don't see the course code has any "Zenstruck\Foundry\Proxy" use statements in the code related to this video, or am I missing it? Could you point to the exact…
hi victor , looks like you missed my reply to my own question below and yes I had 'Zenstruck\Foundry\Proxy' in the use statements. Would still like to know what 'chain configured namespaces' are.
Hey Gary, It sounds like you missed to use the namespace, i.e. make sure you have that "Zenstruck\Foundry\Proxy" namespace in the use statements in whatever class where you use that "Proxy". Please, double check it first. Cheers!
…read this and didn't get a new `make:factory` command. I think the new recipes (v1.10+) are not compatible with Symfony 5.1.x. Make sure you register the bundle manually. `# config/bundles.php return [ // ... Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true…
julien_bonnier
julien_bonnier
Read Full Comment
Hello. In some previous tutorials, you presented us other library for the fixture called Alice Bundle. I wonder beacause this tuto is newer (Symfony 5) so this bundle zenstruck/foundry is better and I should use this instead for new project? Thanks
…symfony 5.0 and php 7.3. I've worked through some issues with Foundry (had to update to php74 to get a version that actually has the make:factory command) and at the point where AsciiSlugger() was used, I had to `composer require symfony…
61 lines | src/Factory/UserFactory.php
// ... lines 1 - 2
namespace App\Factory;
use App\Entity\User;
use App\Repository\UserRepository;
use Zenstruck\Foundry\RepositoryProxy;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
// ... lines 10 - 28
final class UserFactory extends ModelFactory
{
public function __construct()
{
parent::__construct();
// TODO inject services if required (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services)
}
protected function getDefaults(): array
{
return [
// TODO add your default values here (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories)
'email' => self::faker()->text(),
'roles' => [],
'firstName' => self::faker()->text(),
];
}
protected function initialize(): self
{
// see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
return $this
// ->afterInstantiate(function(User $user) {})
;
}
protected static function getClass(): string
{
return User::class;
}
}
See Code Block in Script