Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"symfony/phpunit-bridge": "^5.2",
"symfony/stopwatch": "^5.2",
"symfony/var-dumper": "5.2.*",
"symfony/web-profiler-bundle": "^5.2"
"symfony/web-profiler-bundle": "^5.2",
"zenstruck/foundry": "^1.10"
},
"config": {
"optimize-autoloader": true,
Expand Down
179 changes: 178 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'test' => true],
];
4 changes: 4 additions & 0 deletions config/packages/dev/zenstruck_foundry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# See full configuration: https://github.com/zenstruck/foundry#full-default-bundle-configuration
zenstruck_foundry:
# Whether to auto-refresh proxies by default (https://github.com/zenstruck/foundry#auto-refresh)
auto_refresh_proxies: true
5 changes: 5 additions & 0 deletions config/packages/test/zenstruck_foundry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Unless you want different configuration for test/dev environments,
# add configuration to config/packages/dev/zenstruck_foundry.yml
# and this will be synced to your test environment.
imports:
- { resource: ../dev/zenstruck_foundry.yaml }
7 changes: 6 additions & 1 deletion src/Test/CustomApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ class CustomApiTestCase extends ApiTestCase
const RESOURCE_NOT_FOUND_404 = 404;
const UNAUTHORIZED_401 = 401;

public function setUp(): void
{
self::bootKernel();
}

protected function getEntityManager(): EntityManagerInterface
{
return self::$container->get('doctrine')->getManager();
}

protected function getPasswordEncoder()
public function getPasswordEncoder()
{
return self::$container->get('security.user_password_encoder.generic');
}
Expand Down
19 changes: 19 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
"egulias/email-validator": {
"version": "2.1.24"
},
"fakerphp/faker": {
"version": "v1.14.1"
},
"fig/link-util": {
"version": "1.1.1"
},
Expand Down Expand Up @@ -532,5 +535,21 @@
},
"willdurand/negotiation": {
"version": "v2.3.1"
},
"zenstruck/callback": {
"version": "v1.2.0"
},
"zenstruck/foundry": {
"version": "1.9",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "1.9",
"ref": "e03ffeeb52fd76e99f2c527ecddc9c986cb194e5"
},
"files": [
"./config/packages/dev/zenstruck_foundry.yaml",
"./config/packages/test/zenstruck_foundry.yaml"
]
}
}
61 changes: 61 additions & 0 deletions tests/Factory/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Tests\Factory;

use App\Entity\User;
use App\Repository\UserRepository;
use Zenstruck\Foundry\RepositoryProxy;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use function Zenstruck\Foundry\faker;

/**
* @method static User|Proxy createOne(array $attributes = [])
* @method static User[]|Proxy[] createMany(int $number, $attributes = [])
* @method static User|Proxy find($criteria)
* @method static User|Proxy findOrCreate(array $attributes)
* @method static User|Proxy first(string $sortedField = 'id')
* @method static User|Proxy last(string $sortedField = 'id')
* @method static User|Proxy random(array $attributes = [])
* @method static User|Proxy randomOrCreate(array $attributes = [])
* @method static User[]|Proxy[] all()
* @method static User[]|Proxy[] findBy(array $attributes)
* @method static User[]|Proxy[] randomSet(int $number, array $attributes = [])
* @method static User[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static UserRepository|RepositoryProxy repository()
* @method User|Proxy create($attributes = [])
*/
final class UserFactory extends ModelFactory
{
public function __construct()
{
parent::__construct();

// TODO inject services if required (https://github.com/zenstruck/foundry#factories-as-services)
}

protected function getDefaults(): array
{
return [
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories)
'email' => faker()->email,
'password' => faker()->password,
'roles' => ['ROLE_USER'],
'apiToken' => uniqid(),
'enabled' => true,
];
}

protected function initialize(): self
{
// see https://github.com/zenstruck/foundry#initialization
return $this
// ->afterInstantiate(function(User $user) {})
;
}

protected static function getClass(): string
{
return User::class;
}
}
31 changes: 24 additions & 7 deletions tests/Functional/Activity/ActivityCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,47 @@

namespace App\Tests\Functional\Activity;

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
use App\Entity\User;
use App\Test\CustomApiTestCase;
use App\Tests\Factory\UserFactory;
use DateTime;
use Zenstruck\Foundry\Proxy;

class ActivityCreateTest extends CustomApiTestCase
{
private Client $client;
/**
* @var User|Proxy
*/
private Proxy $user;

public function setUp(): void
{
parent::setUp();
$this->user = UserFactory::createOne();
$this->client = self::createClient();
$this->client->setDefaultOptions([
'headers' => [
'X-AUTH-TOKEN' => $this->user->getApiToken()
]
]);
}

/**
* @dataProvider activityDataSets
*/
public function testCreateActivity(float $performendTime, string $description, string $activityDate)
{
$client = self::createClient();
$user = $this->createUser();
$d = new DateTime();

$client->request('POST', '/api/activities', [
$this->client->request('POST', '/api/activities', [
'json' => [
'activityDate' => $activityDate,
'performendTime' => $performendTime,
'description' => $description,
'user' => '/api/users/' . $user->getId()
'user' => '/api/users/' . $this->user->getId()
],
'headers' => [
'X-AUTH-TOKEN' => $user->getApiToken()
]
]);

$this->assertResponseHeaderSame('Content-Type', 'application/ld+json; charset=utf-8');
Expand Down