Skip to content

Commit ea615b3

Browse files
authored
Merge pull request #1734 from antograssiot/bugfix-object-state
BugFix: Fix object state inconsistency after persitence
2 parents 0eb8d76 + 71e37cb commit ea615b3

File tree

11 files changed

+140
-11
lines changed

11 files changed

+140
-11
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ install:
5151

5252
script:
5353
- vendor/bin/phpunit
54-
- if [[ $APP_ENV != 'postgres' ]]; then vendor/bin/behat --format=progress; fi
55-
- if [[ $APP_ENV = 'postgres' ]]; then vendor/bin/behat --tags='~@sqlite' --format=progress; fi
54+
- if [[ $APP_ENV != 'postgres' ]]; then vendor/bin/behat --suite=default --format=progress; fi
55+
- if [[ $APP_ENV = 'postgres' ]]; then vendor/bin/behat --suite=postgres --format=progress; fi
5656
- tests/Fixtures/app/console api:swagger:export > swagger.json && swagger-cli validate swagger.json && rm swagger.json
5757
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
5858
- if [[ $lint = 1 ]]; then phpstan analyse -c phpstan.neon -l5 --ansi src tests; fi

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Coverage will be available in `coverage/index.html`.
9090
The command to launch Behat tests is:
9191

9292
```
93-
./vendor/bin/behat --stop-on-failure -vvv
93+
./vendor/bin/behat --suite=default --stop-on-failure -vvv
9494
```
9595

9696
You may need to clear the cache manually before running behat tests because of the temporary sql database. To do so, just remove the `test` cache directory:

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ install:
2222
- echo extension=php_mbstring.dll >> php.ini
2323
- echo extension=php_intl.dll >> php.ini
2424
- echo extension=php_pdo_sqlite.dll >> php.ini
25-
- echo memory_limit=1G >> php.ini
25+
- echo memory_limit=3G >> php.ini
2626
- cd %APPVEYOR_BUILD_FOLDER%
2727
- php -r "readfile('http://getcomposer.org/installer');" | php
2828
- php composer.phar install --no-interaction --no-progress
2929

3030
test_script:
3131
- cd %APPVEYOR_BUILD_FOLDER%
32-
- php vendor\behat\behat\bin\behat --format=progress --tags='~@postgres'
32+
- php vendor\behat\behat\bin\behat --format=progress --suite=default
3333
- php vendor\phpunit\phpunit\phpunit

behat.yml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ default:
1313
- 'Behatch\Context\RestContext'
1414
filters:
1515
tags: ~@postgres
16+
postgres:
17+
contexts:
18+
- 'FeatureContext': { doctrine: '@doctrine' }
19+
- 'GraphqlContext'
20+
- 'JsonContext'
21+
- 'HydraContext'
22+
- 'SwaggerContext'
23+
- 'HttpCacheContext'
24+
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
25+
- 'Behat\MinkExtension\Context\MinkContext'
26+
- 'Behatch\Context\RestContext'
27+
filters:
28+
tags: ~@sqlite
1629
extensions:
1730
'Behat\Symfony2Extension':
1831
kernel:

features/doctrine/order_filter.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ Feature: Order filter on collections
305305
Given there are 30 dummy objects
306306
When I send a "GET" request to "/dummies?order[name]=desc&order[id]=desc"
307307
Then the response status code should be 200
308-
Then print last JSON response
309308
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
310309
And the JSON should be valid according to this schema:
311310
"""

features/main/exposed_state.feature

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@postgres
2+
Feature: Expose persisted object state
3+
In order to use an hypermedia API
4+
As a client software developer
5+
I need to be able to retrieve the exact state of resources after persistence.
6+
7+
@createSchema
8+
Scenario: Create a resource with truncable value should return the correct object state
9+
When I add "Content-Type" header equal to "application/ld+json"
10+
And I send a "POST" request to "/truncated_dummies" with body:
11+
"""
12+
{
13+
"value": "20.3325"
14+
}
15+
"""
16+
Then the response status code should be 201
17+
And the JSON should be equal to:
18+
"""
19+
{
20+
"@context": "/contexts/TruncatedDummy",
21+
"@id": "/truncated_dummies/1",
22+
"@type": "TruncatedDummy",
23+
"value": "20.3",
24+
"id": 1
25+
}
26+
"""
27+
28+
@dropSchema
29+
Scenario: Update a resource with truncable value value should return the correct object state
30+
When I add "Content-Type" header equal to "application/ld+json"
31+
And I send a "PUT" request to "/truncated_dummies/1" with body:
32+
"""
33+
{
34+
"value": "42.42"
35+
}
36+
"""
37+
Then the response status code should be 200
38+
And the JSON should be equal to:
39+
"""
40+
{
41+
"@context": "/contexts/TruncatedDummy",
42+
"@id": "/truncated_dummies/1",
43+
"@type": "TruncatedDummy",
44+
"value": "42.4",
45+
"id": 1
46+
}
47+
"""

features/main/relation.feature

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ Feature: Relations support
7272
},
7373
"relatedToDummyFriend": [],
7474
"dummyBoolean": null,
75-
"embeddedDummy": null,
76-
"id": 1,
77-
"symfony": "symfony",
75+
"embeddedDummy": [],
7876
"age": null
7977
}
8078
"""
@@ -476,8 +474,7 @@ Feature: Relations support
476474
},
477475
"relatedToDummyFriend": [],
478476
"dummyBoolean": null,
479-
"embeddedDummy": null,
480-
"symfony": "symfony",
477+
"embeddedDummy": [],
481478
"age": null
482479
}
483480
"""

src/Bridge/Doctrine/Common/DataPersister.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function persist($data)
5353

5454
$manager->persist($data);
5555
$manager->flush();
56+
$manager->refresh($data);
5657
}
5758

5859
/**

tests/Bridge/Doctrine/Common/DataPersisterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function testPersist()
5050
$objectManagerProphecy = $this->prophesize(ObjectManager::class);
5151
$objectManagerProphecy->persist($dummy)->shouldBeCalled();
5252
$objectManagerProphecy->flush()->shouldBeCalled();
53+
$objectManagerProphecy->refresh($dummy)->shouldBeCalled();
5354

5455
$managerRegistryProphecy = $this->prophesize(ManagerRegistry::class);
5556
$managerRegistryProphecy->getManagerForClass(Dummy::class)->willReturn($objectManagerProphecy->reveal())->shouldBeCalled();

tests/Fixtures/TestBundle/Entity/DummyTableInheritanceRelated.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,29 @@ public function setChildren($children)
7878

7979
return $this;
8080
}
81+
82+
/**
83+
* @param $child
84+
*
85+
* @return $this
86+
*/
87+
public function addChild($child)
88+
{
89+
$this->children->add($child);
90+
$child->setParent($this);
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* @param $child
97+
*
98+
* @return $this
99+
*/
100+
public function removeChild($child)
101+
{
102+
$this->children->remove($child);
103+
104+
return $this;
105+
}
81106
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
19+
/**
20+
* @ORM\Entity
21+
* @ApiResource()
22+
*/
23+
class TruncatedDummy
24+
{
25+
/**
26+
* @ORM\Column(type="integer")
27+
* @ORM\Id
28+
* @ORM\GeneratedValue(strategy="AUTO")
29+
*/
30+
private $id;
31+
32+
/**
33+
* @ORM\Column(type="decimal", precision=4, scale=1, nullable=false)
34+
*/
35+
public $value;
36+
37+
/**
38+
* Get id.
39+
*
40+
* @return string
41+
*/
42+
public function getId()
43+
{
44+
return $this->id;
45+
}
46+
}

0 commit comments

Comments
 (0)