Add basic process tests
[friendica.git/.git] / tests / src / Model / ProcessTest.php
1 <?php
2
3 namespace Friendica\Test\src\Model;
4
5 use Friendica\Factory\ConfigFactory;
6 use Friendica\Model\Process;
7 use Friendica\Test\DatabaseTest;
8 use Friendica\Test\Util\Database\StaticDatabase;
9 use Friendica\Test\Util\VFSTrait;
10 use Friendica\Util\ConfigFileLoader;
11 use Friendica\Util\Profiler;
12 use Psr\Log\NullLogger;
13
14 class ProcessTest extends DatabaseTest
15 {
16         use VFSTrait;
17
18         /** @var StaticDatabase */
19         private $dba;
20
21         protected function setUp()
22         {
23                 parent::setUp();
24
25                 $this->setUpVfsDir();
26
27                 $this->logger = new NullLogger();
28
29                 $profiler = \Mockery::mock(Profiler::class);
30                 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
31
32                 // load real config to avoid mocking every config-entry which is related to the Database class
33                 $configFactory = new ConfigFactory();
34                 $loader        = new ConfigFileLoader($this->root->url());
35                 $configCache   = $configFactory->createCache($loader);
36
37                 $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
38         }
39
40         public function testInsertDelete()
41         {
42                 $process = new Process($this->dba);
43
44                 $this->assertEquals(0, $this->dba->count('process'));
45                 $process->insert('test', 1);
46                 $process->insert('test2', 2);
47                 $process->insert('test3', 3);
48
49                 $this->assertEquals(3, $this->dba->count('process'));
50
51                 $this->assertEquals([
52                         ['command' => 'test']
53                 ], $this->dba->selectToArray('process', ['command'], ['pid' => 1]));
54
55                 $process->deleteByPid(1);
56
57                 $this->assertEmpty($this->dba->selectToArray('process', ['command'], ['pid' => 1]));
58
59                 $this->assertEquals(2, $this->dba->count('process'));
60         }
61
62         public function testDoubleInsert()
63         {
64                 $process = new Process($this->dba);
65
66                 $process->insert('test', 1);
67
68                 // double insert doesn't work
69                 $process->insert('test23', 1);
70
71                 $this->assertEquals([['command' => 'test']], $this->dba->selectToArray('process', ['command'], ['pid' => 1]));
72         }
73
74         public function testWrongDelete()
75         {
76                 $process = new Process($this->dba);
77
78                 // Just ignore wrong deletes, no execution is thrown
79                 $process->deleteByPid(-1);
80         }
81 }