FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DbViewProxyTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Proxy;
9 
11 use Mockery as M;
12 
13 class DbViewProxyTest extends \PHPUnit\Framework\TestCase
14 {
15  private $dbViewName = "foo";
16  private $dbViewQuery = "select 3.14";
18  private $dbViewDao;
19  private $dbManagerMock;
20 
21  protected function setUp() : void
22  {
23  $this->dbViewDao = new DbViewProxy($this->dbViewQuery, $this->dbViewName);
24  global $container;
25  $container = M::mock('ContainerBuilder');
26  $this->dbManagerMock = M::mock(DbManager::class);
27  $container->shouldReceive('get')->withArgs(array('db.manager'))->andReturn($this->dbManagerMock);
28  }
29 
30  protected function tearDown() : void
31  {
32  M::close();
33  }
34 
35  public function testGetDbViewName()
36  {
37  assertThat($this->dbViewDao->getDbViewName(),is($this->dbViewName));
38  }
39 
40  public function testMaterialize()
41  {
42  $this->dbManagerMock->shouldReceive('queryOnce')->with("CREATE TEMPORARY TABLE $this->dbViewName AS $this->dbViewQuery", M::any());
43  $this->dbViewDao->materialize();
44  }
45 
46  public function testMaterializeTwice()
47  {
48  $this->dbManagerMock->shouldReceive('queryOnce')->once();
49  $this->dbViewDao->materialize();
50  $this->dbViewDao->materialize();
51  }
52 
53  public function testUnmaterializeAfterMaterialize()
54  {
55  $this->dbManagerMock->shouldReceive('queryOnce');
56  $this->dbViewDao->materialize();
57  $this->dbManagerMock->shouldReceive('queryOnce')->with("DROP TABLE $this->dbViewName");
58  $this->dbViewDao->unmaterialize();
59  }
60 
61  public function testUnmaterializeWithoutMaterialize()
62  {
63  $this->dbManagerMock->shouldReceive('queryOnce')->never();
64  $this->dbViewDao->unmaterialize();
65  }
66 
67  public function testAsCTE()
68  {
69  assertThat($this->dbViewDao->asCTE(),is("WITH $this->dbViewName AS (".$this->dbViewQuery.")"));
70  }
71 }