Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdatingServiceFactoryTest.php
Go to the documentation of this file.
1 <?php
7 
16 use PHPUnit_Framework_MockObject_MockObject as MockObject;
17 
21 class UpdatingServiceFactoryTest extends \PHPUnit\Framework\TestCase
22 {
26  private $factory;
27 
31  private $config;
32 
36  private $fakeObjectManager;
37 
41  private $generatorFactory;
42 
46  protected function setUp()
47  {
48  $this->config = $this->getMockBuilder(Config::class)
49  ->disableOriginalConstructor()
50  ->setMethods(['isActive'])
51  ->getMock();
52 
53  $this->fakeObjectManager = $this->getMockBuilder(ObjectManagerInterface::class)
54  ->disableOriginalConstructor()
55  ->setMethods(['create'])
56  ->getMockForAbstractClass();
57 
58  $this->generatorFactory = $this->getMockBuilder(GeneratorFactory::class)
59  ->disableOriginalConstructor()
60  ->setMethods(['create'])
61  ->getMock();
62 
63  $objectManager = new ObjectManager($this);
64  $this->factory = $objectManager->getObject(UpdatingServiceFactory::class, [
65  'objectManager' => $this->fakeObjectManager,
66  'generatorFactory' => $this->generatorFactory,
67  'config' => $this->config
68  ]);
69  }
70 
76  public function testCreateWithInactiveConfig()
77  {
78  $type = 'cases/creation';
79  $this->config->expects(self::once())
80  ->method('isActive')
81  ->willReturn(false);
82 
83  $this->fakeObjectManager->expects(self::once())
84  ->method('create')
85  ->with(StubUpdatingService::class)
86  ->willReturn(new StubUpdatingService());
87 
88  $instance = $this->factory->create($type);
89  static::assertInstanceOf(StubUpdatingService::class, $instance);
90  }
91 
97  public function testCreateWithTestType()
98  {
99  $type = 'cases/test';
100  $this->config->expects(self::once())
101  ->method('isActive')
102  ->willReturn(true);
103 
104  $this->fakeObjectManager->expects(self::once())
105  ->method('create')
106  ->with(StubUpdatingService::class)
107  ->willReturn(new StubUpdatingService());
108 
109  $instance = $this->factory->create($type);
110  static::assertInstanceOf(StubUpdatingService::class, $instance);
111  }
112 
120  public function testCreateWithException()
121  {
122  $type = 'cases/unknown';
123  $this->config->expects(self::once())
124  ->method('isActive')
125  ->willReturn(true);
126 
127  $this->generatorFactory->expects(self::once())
128  ->method('create')
129  ->with($type)
130  ->willThrowException(new \InvalidArgumentException('Specified message type does not supported.'));
131 
132  $this->factory->create($type);
133  }
134 
140  public function testCreate()
141  {
142  $type = 'case/creation';
143  $this->config->expects(self::once())
144  ->method('isActive')
145  ->willReturn(true);
146 
147  $messageGenerator = $this->getMockBuilder(GeneratorInterface::class)
148  ->disableOriginalConstructor()
149  ->getMock();
150  $this->generatorFactory->expects(self::once())
151  ->method('create')
152  ->with($type)
153  ->willReturn($messageGenerator);
154 
155  $service = $this->getMockBuilder(UpdatingService::class)
156  ->disableOriginalConstructor()
157  ->getMock();
158 
159  $this->fakeObjectManager->expects(self::once())
160  ->method('create')
161  ->with(UpdatingService::class, ['messageGenerator' => $messageGenerator])
162  ->willReturn($service);
163 
164  $result = $this->factory->create($type);
165  static::assertInstanceOf(UpdatingService::class, $result);
166  }
167 }
$objectManager
Definition: bootstrap.php:17
$type
Definition: item.phtml:13