interface FileOpenerInterface {
public function openFile($path, $mode);
}
final class FileOpener implements FileOpenerInterface{
public function openFile($path, $mode){
return fopen($path, $mode);
}
}
class FileProcessor{
public function __construct(private FileOpenerInterface $fileOpener) { }
public function readFileContent($filePath) {
$fileHandle = $this->fileOpener->openFile($filePath, 'r');
if ($fileHandle) {
$content = fread($fileHandle, filesize($filePath));
fclose($fileHandle);
return $content;
} else {
return false;
}
}
}
class FileProcessorTest extends PHPUnit\Framework\TestCase {
public function testReadFileContent() {
// Создаем мок для FileOpenerInterface
$fileOpenerMock = $this->createMock(FileOpenerInterface::class);
$fileOpenerMock->expects($this->once())
->method('openFile')
->willReturn(fopen('php://memory', 'r'));
$fileProcessor = new FileProcessor($fileOpenerMock);
$content = $fileProcessor->readFileContent('fakefile://path/to/your/file.txt');
// Проверяем ожидаемый результат
$this->assertEquals("", $content);
}
}export const createLoggerMiddleware = (someType: string): Middleware => {
return () => next => action => {
if (action.type === someType) {
alert('blabla');
}
return next(action);
};
};export const setupStore = () => configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(createLoggerMiddleware('someType')),
});// appContext.js
import React from 'react';
export const AppContext = React.createContext({});// App.js
import { AppContext } from './appContext';
function App() {
const [store, setStore] = useState(initialState); // начальное состояние Redux store
return (
<AppContext.Provider value={{ store, setStore }}>
<Routes>
<Route path='items' element={<Items />} />
</Routes>
</AppContext.Provider>
);
}// Dashboard.js
import React, { useContext } from 'react';
import { useSearchParams } from 'react-router-dom';
import { AppContext } from '../appContext';
function Dashboard() {
const { store } = useContext(AppContext);
const [searchParams] = useSearchParams();
// получаем данные из текущего состояния Redux store
const items = store.items;
// обновляем данные в Redux store, если это необходимо
if (searchParams.get('update')) {
const newItems = await fetchItems(); // получаем новые данные
setStore(prevState => ({ ...prevState, items: newItems })); // обновляем состояние Redux store
}
return (
<div>
{items.map(item => (
<div key={item.id}>{item.name}</div>
))}
</div>
);
}env()If you execute theconfig:cachecommand during your deployment process, you should be sure that you are only calling theenvfunction from within your configuration files. Once the configuration has been cached, the.envfile will not be loaded and all calls to theenvfunction will returnnull.
'name' => env('APP_NAME', 'Laravel'),config('app.name') где-нибудь в коде, чтобы получить данные из этой настройки. Вот только если вы вызовите env('APP_NAME') вы, возможно, получите ошибку, в случае, если эта переменная не задана в переменных среды.'endpoint' => 'https://' . env('API_DOMAIN') . '/some/subroute',
type FilterOptions<T> = {
[K in keyof T]?: T[K] extends object ? FilterOptions<T[K]> : T[K];
};
const isOverlaps = <T>(entry: T, options: FilterOptions<T>): boolean => {
const keys = Object.keys(options) as Array<keyof T>;
return keys.every((key) => {
if (typeof options[key] === 'object') {
return isOverlaps(entry[key], options[key]!);
}
return entry[key] === options[key];
});
};
const filterBy = <T>(collection: T[], options: FilterOptions<T>) => {
return collection.filter((entry) => isOverlaps(entry, options));
};