Skip to content

Commit

Permalink
paso 1.3 dos
Browse files Browse the repository at this point in the history
  • Loading branch information
gerMdz committed Sep 11, 2022
1 parent 45efa8b commit 0779c78
Show file tree
Hide file tree
Showing 12 changed files with 392 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
19 changes: 19 additions & 0 deletions bin/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env php
<?php

if (!ini_get('date.timezone')) {
ini_set('date.timezone', 'UTC');
}

if (is_file(dirname(__DIR__).'/vendor/phpunit/phpunit/phpunit')) {
define('PHPUNIT_COMPOSER_INSTALL', dirname(__DIR__).'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
PHPUnit\TextUI\Command::main();
} else {
if (!is_file(dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php')) {
echo "Unable to find the `simple-phpunit.php` script in `vendor/symfony/phpunit-bridge/bin/`.\n";
exit(1);
}

require dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php';
}
42 changes: 42 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
convertDeprecationsToExceptions="false"
>
<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>

<!-- Run `composer require symfony/panther` before enabling this extension -->
<!--
<extensions>
<extension class="Symfony\Component\Panther\ServerExtension" />
</extensions>
-->
</phpunit>
32 changes: 32 additions & 0 deletions src/Form/UserType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('email')
->add('roles')
->add('password')
->add('apiToken')
->add('FirstName')
->add('isVerified')
->add('isActive')
->add('organization')
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
4 changes: 4 additions & 0 deletions templates/admin_user/_delete_form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<form method="post" action="{{ path('app_admin_user_delete', {'id': user.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ user.id) }}">
<button class="btn">Delete</button>
</form>
4 changes: 4 additions & 0 deletions templates/admin_user/_form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
13 changes: 13 additions & 0 deletions templates/admin_user/edit.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}

{% block title %}Edit User{% endblock %}

{% block body %}
<h1>Edit User</h1>

{{ include('admin_user/_form.html.twig', {'button_label': 'Update'}) }}

<a href="{{ path('app_admin_user_index') }}">back to list</a>

{{ include('admin_user/_delete_form.html.twig') }}
{% endblock %}
47 changes: 47 additions & 0 deletions templates/admin_user/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% extends 'base.html.twig' %}

{% block title %}User index{% endblock %}

{% block body %}
<h1>User index</h1>

<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Email</th>
<th>Roles</th>
<th>Password</th>
<th>ApiToken</th>
<th>FirstName</th>
<th>IsVerified</th>
<th>IsActive</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.email }}</td>
<td>{{ user.roles ? user.roles|json_encode : '' }}</td>
<td>{{ user.password }}</td>
<td>{{ user.apiToken }}</td>
<td>{{ user.FirstName }}</td>
<td>{{ user.isVerified ? 'Yes' : 'No' }}</td>
<td>{{ user.isActive ? 'Yes' : 'No' }}</td>
<td>
<a href="{{ path('app_admin_user_show', {'id': user.id}) }}">show</a>
<a href="{{ path('app_admin_user_edit', {'id': user.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="9">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>

<a href="{{ path('app_admin_user_new') }}">Create new</a>
{% endblock %}
11 changes: 11 additions & 0 deletions templates/admin_user/new.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}

{% block title %}New User{% endblock %}

{% block body %}
<h1>Create new User</h1>

{{ include('admin_user/_form.html.twig') }}

<a href="{{ path('app_admin_user_index') }}">back to list</a>
{% endblock %}
50 changes: 50 additions & 0 deletions templates/admin_user/show.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{% extends 'base.html.twig' %}

{% block title %}User{% endblock %}

{% block body %}
<h1>User</h1>

<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ user.id }}</td>
</tr>
<tr>
<th>Email</th>
<td>{{ user.email }}</td>
</tr>
<tr>
<th>Roles</th>
<td>{{ user.roles ? user.roles|json_encode : '' }}</td>
</tr>
<tr>
<th>Password</th>
<td>{{ user.password }}</td>
</tr>
<tr>
<th>ApiToken</th>
<td>{{ user.apiToken }}</td>
</tr>
<tr>
<th>FirstName</th>
<td>{{ user.FirstName }}</td>
</tr>
<tr>
<th>IsVerified</th>
<td>{{ user.isVerified ? 'Yes' : 'No' }}</td>
</tr>
<tr>
<th>IsActive</th>
<td>{{ user.isActive ? 'Yes' : 'No' }}</td>
</tr>
</tbody>
</table>

<a href="{{ path('app_admin_user_index') }}">back to list</a>

<a href="{{ path('app_admin_user_edit', {'id': user.id}) }}">edit</a>

{{ include('admin_user/_delete_form.html.twig') }}
{% endblock %}
153 changes: 153 additions & 0 deletions tests/Controller/UserControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace App\Test\Controller;

use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class UserControllerTest extends WebTestCase
{
private KernelBrowser $client;
private UserRepository $repository;
private string $path = '/admin/user/';

protected function setUp(): void
{
$this->client = static::createClient();
$this->repository = (static::getContainer()->get('doctrine'))->getRepository(User::class);

foreach ($this->repository->findAll() as $object) {
$this->repository->remove($object, true);
}
}

public function testIndex(): void
{
$crawler = $this->client->request('GET', $this->path);

self::assertResponseStatusCodeSame(200);
self::assertPageTitleContains('User index');

// Use the $crawler to perform additional assertions e.g.
// self::assertSame('Some text on the page', $crawler->filter('.p')->first());
}

public function testNew(): void
{
$originalNumObjectsInRepository = count($this->repository->findAll());

$this->markTestIncomplete();
$this->client->request('GET', sprintf('%snew', $this->path));

self::assertResponseStatusCodeSame(200);

$this->client->submitForm('Save', [
'user[email]' => 'Testing',
'user[roles]' => 'Testing',
'user[password]' => 'Testing',
'user[apiToken]' => 'Testing',
'user[FirstName]' => 'Testing',
'user[isVerified]' => 'Testing',
'user[isActive]' => 'Testing',
'user[organization]' => 'Testing',
]);

self::assertResponseRedirects('/admin/user/');

self::assertSame($originalNumObjectsInRepository + 1, count($this->repository->findAll()));
}

public function testShow(): void
{
$this->markTestIncomplete();
$fixture = new User();
$fixture->setEmail('My Title');
$fixture->setRoles('My Title');
$fixture->setPassword('My Title');
$fixture->setApiToken('My Title');
$fixture->setFirstName('My Title');
$fixture->setIsVerified('My Title');
$fixture->setIsActive('My Title');
$fixture->setOrganization('My Title');

$this->repository->add($fixture, true);

$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));

self::assertResponseStatusCodeSame(200);
self::assertPageTitleContains('User');

// Use assertions to check that the properties are properly displayed.
}

public function testEdit(): void
{
$this->markTestIncomplete();
$fixture = new User();
$fixture->setEmail('My Title');
$fixture->setRoles('My Title');
$fixture->setPassword('My Title');
$fixture->setApiToken('My Title');
$fixture->setFirstName('My Title');
$fixture->setIsVerified('My Title');
$fixture->setIsActive('My Title');
$fixture->setOrganization('My Title');

$this->repository->add($fixture, true);

$this->client->request('GET', sprintf('%s%s/edit', $this->path, $fixture->getId()));

$this->client->submitForm('Update', [
'user[email]' => 'Something New',
'user[roles]' => 'Something New',
'user[password]' => 'Something New',
'user[apiToken]' => 'Something New',
'user[FirstName]' => 'Something New',
'user[isVerified]' => 'Something New',
'user[isActive]' => 'Something New',
'user[organization]' => 'Something New',
]);

self::assertResponseRedirects('/admin/user/');

$fixture = $this->repository->findAll();

self::assertSame('Something New', $fixture[0]->getEmail());
self::assertSame('Something New', $fixture[0]->getRoles());
self::assertSame('Something New', $fixture[0]->getPassword());
self::assertSame('Something New', $fixture[0]->getApiToken());
self::assertSame('Something New', $fixture[0]->getFirstName());
self::assertSame('Something New', $fixture[0]->getIsVerified());
self::assertSame('Something New', $fixture[0]->getIsActive());
self::assertSame('Something New', $fixture[0]->getOrganization());
}

public function testRemove(): void
{
$this->markTestIncomplete();

$originalNumObjectsInRepository = count($this->repository->findAll());

$fixture = new User();
$fixture->setEmail('My Title');
$fixture->setRoles('My Title');
$fixture->setPassword('My Title');
$fixture->setApiToken('My Title');
$fixture->setFirstName('My Title');
$fixture->setIsVerified('My Title');
$fixture->setIsActive('My Title');
$fixture->setOrganization('My Title');

$this->repository->add($fixture, true);

self::assertSame($originalNumObjectsInRepository + 1, count($this->repository->findAll()));

$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
$this->client->submitForm('Delete');

self::assertSame($originalNumObjectsInRepository, count($this->repository->findAll()));
self::assertResponseRedirects('/admin/user/');
}
}
Loading

0 comments on commit 0779c78

Please sign in to comment.