Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.29% covered (success)
94.29%
33 / 35
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
container_cache_directory_test
94.29% covered (success)
94.29%
33 / 35
50.00% covered (danger)
50.00%
2 / 4
7.01
0.00% covered (danger)
0.00%
0 / 1
 setUp
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 test_cache_directory_can_be_overridden
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 test_container_and_autoload_cache
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
2.00
 test_autoload_and_container_cache_are_written_to_overriden_cache_directory
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
2.00
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace
15{
16    class container_cache_directory_test extends \phpbb_test_case //phpbb_di_container_test
17    {
18        protected $config_php;
19
20        /**
21        * @var \phpbb\di\container_builder
22        */
23        protected $builder;
24        protected $phpbb_root_path;
25        protected $filename;
26
27        public function setUp(): void
28        {
29            $this->phpbb_root_path = __DIR__ . '/';
30            $this->config_php = new \phpbb\config_php_file($this->phpbb_root_path . 'fixtures/', 'php');
31
32            $this->filename = $this->phpbb_root_path . '../tmp/container.php';
33            if (is_file($this->filename))
34            {
35                unlink($this->filename);
36            }
37
38            parent::setUp();
39        }
40
41        public function test_cache_directory_can_be_overridden()
42        {
43            $new_cache_directory = $this->phpbb_root_path . 'fixtures/overwrite-cache-directory/test/';
44
45            // This is how one overrides the cache directory.
46            // The file cache driver will now write to a new directory.
47            $_SERVER['PHPBB____core__cache_dir'] = $new_cache_directory;
48
49            $this->builder = new phpbb_mock_phpbb_di_container_builder($this->phpbb_root_path . 'fixtures/', 'php');
50            $this->builder->with_config($this->config_php);
51            $container = $this->builder->get_container();
52
53            $this->assertEquals($container->getParameter('core.cache_dir'), $new_cache_directory);
54        }
55
56        /**
57         * By default autoload_xxx.php and container_xxx.php files
58         * will also be written to the default cache directory.
59         * This test demonstrates the default behavior.
60         */
61        public function test_container_and_autoload_cache()
62        {
63            $default_cache_directory = $this->phpbb_root_path . 'fixtures/cache/test/';
64
65            // Make sure our test directory will be empty.
66            if (is_dir($default_cache_directory))
67            {
68                array_map('unlink', glob($default_cache_directory . '/*'));
69            }
70            else
71            {
72                mkdir($default_cache_directory, 0777, true);
73            }
74
75            // Use the normal container_builder
76            $builder = new \phpbb\di\container_builder($this->phpbb_root_path . 'fixtures/', 'php');
77            $builder->with_config($this->config_php);
78
79            $container = $builder->get_container();
80
81            $files_written_to_cache = array_map('basename', glob($default_cache_directory . '/*'));
82
83            $this->assertNotEmpty(preg_grep('/autoload_.+.php/', $files_written_to_cache), 'There should be an autoload file in the cache directory.');
84            $this->assertNotEmpty(preg_grep('/container_.+.php/', $files_written_to_cache), 'There should be an container file in the cache directory.');
85
86            // Cleanup the cache directory to prevent class redeclaration errors.
87            array_map('unlink', glob($default_cache_directory . '/*'));
88        }
89
90        /**
91         * The desired behavior: When we have a custom cache directory
92         * the autoload and container cache files are also written to the custom cache directory.
93         */
94        public function test_autoload_and_container_cache_are_written_to_overriden_cache_directory()
95        {
96            $new_cache_directory = $this->phpbb_root_path . 'fixtures/overwrite-cache-directory/test/';
97
98            $_SERVER['PHPBB____core__cache_dir'] = $new_cache_directory;
99
100            // Make sure our test directory will be empty.
101            if (is_dir($new_cache_directory))
102            {
103                array_map('unlink', glob($new_cache_directory . '/*'));
104            }
105            else
106            {
107                mkdir($new_cache_directory, 0777, true);
108            }
109
110            // Use the normal container_builder
111            $builder = new \phpbb\di\container_builder($this->phpbb_root_path . 'fixtures/', 'php');
112            $builder->with_config($this->config_php);
113
114            $container = $builder->get_container();
115
116            $files_written_to_cache = array_map('basename', glob($new_cache_directory."/*"));
117
118            $this->assertNotEmpty(preg_grep('/autoload_.+.php/', $files_written_to_cache), 'There should be an autoload file in the cache directory.');
119            $this->assertNotEmpty(preg_grep('/container_.+.php/', $files_written_to_cache), 'There should be an container file in the cache directory.');
120
121            array_map('unlink', glob($new_cache_directory . '/*'));
122
123        }
124    }
125}