Skip to content

Commit f09c564

Browse files
committed
Tweaked the new blog post tags feature
1 parent 963c260 commit f09c564

File tree

13 files changed

+80
-38
lines changed

13 files changed

+80
-38
lines changed

app/Resources/translations/validators.en.xlf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
<source>post.too_short_content</source>
1515
<target>Post content is too short ({{ limit }} characters minimum)</target>
1616
</trans-unit>
17-
<trans-unit id="post.too_much_tags">
18-
<source>post.too_much_tags</source>
19-
<target>Too much tags ({{ limit }} maximum)</target>
17+
<trans-unit id="post.too_many_tags">
18+
<source>post.too_many_tags</source>
19+
<target>Too many tags (add {{ limit }} tags or less)</target>
2020
</trans-unit>
2121
<trans-unit id="comment.blank">
2222
<source>comment.blank</source>

app/Resources/translations/validators.es.xlf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
<source>post.too_short_content</source>
1515
<target>El contenido del artículo es demasiado corto ({{ limit }} caracteres como mínimo)</target>
1616
</trans-unit>
17-
<trans-unit id="post.too_much_tags">
18-
<source>post.too_much_tags</source>
19-
<target>Demasiadas etiquetas ({{ limit }} como máximo)</target>
17+
<trans-unit id="post.too_many_tags">
18+
<source>post.too_many_tags</source>
19+
<target>Demasiadas etiquetas (añade {{ limit }} como máximo)</target>
2020
</trans-unit>
2121
<trans-unit id="comment.blank">
2222
<source>comment.blank</source>

app/Resources/views/blog/_post_tags.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% if not post.tags.empty %}
22
<p class="post-tags">
33
{% for tag in post.tags %}
4-
<span class="label label-success">
4+
<span class="label label-default">
55
<i class="fa fa-tag"></i> {{ tag.name }}
66
</span>
77
{% endfor %}

app/Resources/views/blog/index.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
</a>
1212
</h2>
1313

14-
{{ include('blog/_post_tags.html.twig') }}
15-
1614
<p class="post-metadata">
1715
<span class="metadata"><i class="fa fa-calendar"></i> {{ post.publishedAt|localizeddate('long', 'medium', null, 'UTC') }}</span>
1816
<span class="metadata"><i class="fa fa-user"></i> {{ post.author.email }}</span>
1917
</p>
2018

2119
{{ post.summary|md2html }}
20+
21+
{{ include('blog/_post_tags.html.twig') }}
2222
</article>
2323
{% else %}
2424
<div class="well">{{ 'post.no_posts_found'|trans }}</div>

app/config/services.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ services:
2222
tags:
2323
- { name: twig.extension }
2424

25-
# This is only needed if your form type requires some dependencies to be injected
26-
# by the container, otherwise it is unnecessary overhead and therefore not recommended
27-
# to do this for all form type classes.
25+
# Defining a form type as a service is only required when the form type
26+
# needs to use some other services, such as the entity manager.
2827
# See http://symfony.com/doc/current/best_practices/forms.html
2928
app.form.type.tagsinput:
3029
class: AppBundle\Form\Type\TagsInputType

src/AppBundle/DataFixtures/ORM/PostFixtures.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
2222

2323
/**
24-
* Defines the sample data to load in the database when running the unit and
25-
* functional tests.
26-
*
27-
* Execute this command to load the data:
24+
* Defines the sample blog posts to load in the database before running the unit
25+
* and functional tests. Execute this command to load the data.
2826
*
2927
* $ php bin/console doctrine:fixtures:load
3028
*
@@ -53,8 +51,9 @@ public function load(ObjectManager $manager)
5351
$post->setSummary($this->getRandomPostSummary());
5452
$post->setSlug($this->container->get('slugger')->slugify($post->getTitle()));
5553
$post->setContent($this->getPostContent());
56-
// This reference has been added in UserFixtures class and contains
57-
// an instance of User entity.
54+
// "References" are the way to share objects between fixtures defined
55+
// in different files. This reference has been added in the UserFixtures
56+
// file and it contains an instance of the User entity.
5857
$post->setAuthor($this->getReference('anna-admin'));
5958
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
6059

@@ -79,8 +78,9 @@ public function load(ObjectManager $manager)
7978
}
8079

8180
/**
82-
* This method must return an array of fixtures classes
83-
* on which the implementing class depends on.
81+
* Instead of defining the exact order in which the fixtures files must be loaded,
82+
* this method defines which other fixtures this file depends on. Then, Doctrine
83+
* will figure out the best order to fit all the dependencies.
8484
*
8585
* @return array
8686
*/
@@ -170,14 +170,14 @@ private function getPhrases()
170170
'Eposs sunt solems de superbus fortis',
171171
'Vae humani generis',
172172
'Diatrias tolerare tanquam noster caesium',
173-
'Teres talis orgias saepe tractare de camerarius flavum sensorem',
173+
'Teres talis saepe tractare de camerarius flavum sensorem',
174174
'Silva de secundus galatae demitto quadra',
175175
'Sunt accentores vitare salvus flavum parses',
176-
'Potus sensim ducunt ad ferox abnoba',
176+
'Potus sensim ad ferox abnoba',
177177
'Sunt seculaes transferre talis camerarius fluctuies',
178178
'Era brevis ratione est',
179179
'Sunt torquises imitari velox mirabilis medicinaes',
180-
'Cum mineralis persuadere omnes finises desiderium bi-color',
180+
'Mineralis persuadere omnes finises desiderium',
181181
'Bassus fatalis classiss virtualiter transferre de flavum',
182182
];
183183
}

src/AppBundle/DataFixtures/ORM/TagFixtures.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
use Doctrine\Common\Persistence\ObjectManager;
1717

1818
/**
19-
* Defines the sample data to load in the database when running the unit and
20-
* functional tests.
21-
*
22-
* Execute this command to load the data:
19+
* Defines the sample blog tags to load in the database before running the unit
20+
* and functional tests. Execute this command to load the data.
2321
*
2422
* $ php bin/console doctrine:fixtures:load
2523
*
@@ -30,7 +28,7 @@
3028
class TagFixtures extends AbstractFixture
3129
{
3230
public static $names = [
33-
'Lorem',
31+
'lorem',
3432
'ipsum',
3533
'consectetur',
3634
'adipiscing',

src/AppBundle/DataFixtures/ORM/UserFixtures.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1919

2020
/**
21-
* Defines the sample data to load in the database when running the unit and
22-
* functional tests.
23-
*
24-
* Execute this command to load the data:
21+
* Defines the sample users to load in the database befre running the unit and
22+
* functional tests. Execute this command to load the data.
2523
*
2624
* $ php bin/console doctrine:fixtures:load
2725
*

src/AppBundle/Entity/Post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Post
105105
*
106106
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Tag", cascade={"persist"})
107107
* @ORM\JoinTable(name="symfony_demo_post_tag")
108-
* @Assert\Count(max="4", maxMessage="post.too_much_tags")
108+
* @Assert\Count(max="4", maxMessage="post.too_many_tags")
109109
*/
110110
private $tags;
111111

tests/AppBundle/Controller/DefaultControllerTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Tests\AppBundle\Controller;
1313

14+
use AppBundle\Entity\Post;
1415
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1516

1617
/**
@@ -43,6 +44,23 @@ public function testPublicUrls($url)
4344
);
4445
}
4546

47+
/**
48+
* A good practice for tests is to not use the service container, to make
49+
* them more robust. However, in this example we must access to the container
50+
* to get the entity manager and make a database query. The reason is that
51+
* blog post fixtures are randomly generated and there's no guarantee that
52+
* a given blog post slug will be available.
53+
*/
54+
public function testPublicBlogPost()
55+
{
56+
// the service container is always available via the client
57+
$client = self::createClient();
58+
$blogPost = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
59+
$client->request('GET', sprintf('/en/blog/posts/%s', $blogPost->getSlug()));
60+
61+
$this->assertTrue($client->getResponse()->isSuccessful());
62+
}
63+
4664
/**
4765
* The application contains a lot of secure URLs which shouldn't be
4866
* publicly accessible. This tests ensures that whenever a user tries to
@@ -68,7 +86,6 @@ public function getPublicUrls()
6886
{
6987
yield ['/'];
7088
yield ['/en/blog/'];
71-
yield ['/en/blog/posts/morbi-tempus-commodo-mattis'];
7289
yield ['/en/login'];
7390
}
7491

0 commit comments

Comments
 (0)