Completed
Push — 1.10 ( 3f8f95...f007bc )
by
unknown
09:16
created

LoadForecastWidgetFixtures::createOpportunity()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 2
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Tests\Functional\Fixture;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\Persistence\ObjectManager;
7
8
use Oro\Bundle\DashboardBundle\Entity\Dashboard;
9
use Oro\Bundle\DashboardBundle\Entity\Widget;
10
use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper;
11
use OroCRM\Bundle\SalesBundle\Entity\Opportunity;
12
13
class LoadForecastWidgetFixtures extends AbstractFixture
14
{
15
    private $organization;
16
17
    /**
18
     * @inheritDoc
19
     */
20
    public function load(ObjectManager $manager)
21
    {
22
        $this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
23
24
        $this->addWidget($manager);
25
        $this->createOpportunity($manager);
26
    }
27
28
    /**
29
     * @param ObjectManager $manager
30
     */
31
    private function addWidget(ObjectManager $manager)
32
    {
33
        $dashboard = new Dashboard();
34
        $dashboard->setName('Test dashboard');
35
36
        $leadStaticsWidget = new Widget();
37
        $leadStaticsWidget
38
            ->setDashboard($dashboard)
39
            ->setName('forecast_of_opportunities')
40
            ->setLayoutPosition([1, 1]);
41
42
        $dashboard->addWidget($leadStaticsWidget);
43
44
        if (!$this->hasReference('widget_forecast')) {
45
            $this->setReference('widget_forecast', $leadStaticsWidget);
46
        }
47
        $manager->persist($dashboard);
48
        $manager->flush();
49
    }
50
51
    /**
52
     * @param ObjectManager $manager
53
     */
54
    private function createOpportunity(ObjectManager $manager)
55
    {
56
        $opportunityList = [
57
            [
58
                'status' => 'in_progress',
59
                'close_date' => null,
60
                'probability' => 10, //percents
61
                'budget_amount' => 100, //USD
62
            ],
63
            [
64
                'status' => 'in_progress',
65
                'close_date' => new \DateTime('now'),
66
                'probability' => 10, //percents
67
                'budget_amount' => 100, //USD
68
            ],
69
            [
70
                'status' => 'in_progress',
71
                'close_date' => new \DateTime('now'),
72
                'probability' => 100, //percents
73
                'budget_amount' => 100, //USD
74
            ],
75
        ];
76
77
        foreach ($opportunityList as $opportunityName => $opportunityData) {
78
            $opportunity = new Opportunity();
79
            $opportunity->setName(sprintf('test_opportunity_%s', $opportunityName));
80
            $opportunity->setBudgetAmount($opportunityData['budget_amount']);
81
            $opportunity->setProbability($opportunityData['probability']);
82
            $opportunity->setOrganization($this->organization);
83
            $opportunity->setCloseDate($opportunityData['close_date']);
84
85
            $enumClass = ExtendHelper::buildEnumValueClassName(Opportunity::INTERNAL_STATUS_CODE);
86
            $opportunity->setStatus($manager->getReference($enumClass, $opportunityData['status']));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getReference() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager, Oro\Bundle\EntityBundle\ORM\OroEntityManager, Oro\Bundle\TestFramework...Mocks\EntityManagerMock, Oro\Component\TestUtils\...Mocks\EntityManagerMock.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
87
88
            $manager->persist($opportunity);
89
            $manager->flush();
90
        }
91
    }
92
}
93